···88 Printexc.record_backtrace true;
99 print_newline ();
1010 let vm = Vm.init ~debug:true () in
1111- let host = Host.create "home" 9 in
1111+ let home = Host.create "home" in
1212+ let inbox = Host.create "inbox" in
1313+ let outbox = Host.create "outbox" in
1414+ Vm.add_host vm home;
1515+ Vm.add_host vm inbox;
1616+ Vm.add_host vm outbox;
1717+ Vm.connect_hosts home [ (800, inbox, -1) ];
1818+ Vm.connect_hosts inbox [ (800, outbox, -1) ];
1219 let file = File.create "200" ~contents:[ Int 72; Int 52; Int 4; Int 60 ] in
1313- Vm.add_host vm host;
1414- Vm.place_file vm host file;
2020+ Vm.place_file vm inbox file;
1521 let code =
1622 {|
1717-GRAB 200
1818-COPY F X
1919-ADDI X F X
2020-MULI X F X
2121-SUBI X F X
2222-COPY X F
2323-SEEK -9999
2424-VOID F
2525-SEEK -3
2626-FILE T
2727- |}
2323+ LINK 800
2424+ GRAB 200
2525+ COPY F X
2626+ ADDI X F X
2727+ MULI X F X
2828+ SUBI X F X
2929+ COPY X F
3030+ LINK 800
3131+ |}
2832 in
2929- (match Vm.create_exa vm host "A" code with
3333+ (match Vm.create_exa vm home "A" code with
3034 | Error err -> print_string err
3135 | Ok _ ->
3236 Result.iter_error
3337 (fun (e : InstResult.error_t) -> print_string (InstResult.show (Error e)))
3438 (Vm.run vm));
3539 print_newline ();
3636-3737- let f2 = File.create "300" ~contents:[ Int 72; Int 52; Int 4; Int 60; Int 436 ] in
3838- Printf.printf "Results good? %b" (File.equal file f2);
3940 exit 0
+2
lib/Common.ml
···3535 | TEST_LT of r_n * r_n
3636 | TEST_GT of r_n * r_n
3737 (* Movement *)
3838+ | LINK of r_n
3839 | HOST of register
3940 (* File Manipulation *)
4041 | MAKE
···6566 name : string;
6667 grid_size : int;
6768 mutable grid : obj StringMap.t;
6969+ mutable links : host IntMap.t;
6870}
69717072and value =
+1
lib/Exa.ml
···33 file, You can obtain one at https://mozilla.org/MPL/2.0/. *)
4455open Common
66+67type t = Common.exa
7889let create ?(code = Dynarray.create ()) (name : string) (host : host) : t =
+1
lib/File.ml
···33 file, You can obtain one at https://mozilla.org/MPL/2.0/. *)
4455open Common
66+67type t = Common.file
7889let file_gensym = ref 0
···31313232let add_host (vm : t) (host : Host.t) : unit = StringMap.add vm.hosts host.name host
33333434+let connect_hosts (host : Host.t) links = Host.add_links host links
3535+3436let add_file (vm : t) (file : File.t) : unit = StringMap.add vm.files file.name file
35373638let place_exa (vm : t) (host : Host.t) (exa : Exa.t) : unit =
···259261let host (exa : Exa.t) (dest : register) : inst_result =
260262 set_register exa dest (Value.key exa.host.name)
261263264264+let link (_vm : t) (exa : Exa.t) (dest : r_n) : inst_result =
265265+ match value_of_r_n exa dest with
266266+ | Error _ as e -> e
267267+ | Ok (Key _) -> runtime_error "CANNOT LINK WITH KEYWORD"
268268+ | Ok (Int i) -> (
269269+ match Host.get_link exa.host i with
270270+ | None ->
271271+ Printf.printf "exa.host: %s\n" (Host.show exa.host);
272272+ runtime_error "NO MATCHING LINK"
273273+ | Some dest ->
274274+ if Host.add dest (E exa) then (
275275+ Host.remove exa.host exa.name;
276276+ exa.host <- dest);
277277+ pass ())
278278+262279let grab (exa : Exa.t) (src : r_n) : inst_result =
263280 match value_of_r_n exa src with
264281 | Error _ as e -> e
···311328 match exa.f with
312329 | None -> runtime_error "NO CURRENT FILE"
313330 | Some file ->
314314- if
315315- Host.add exa.host (F file)
316316- then (
331331+ if Host.add exa.host (F file) then (
317332 exa.f <- None;
318333 exa.f_pos <- 0;
319334 pass ())
···358373 | TEST_EQ (left, right) -> test_fn exa left test_eq right
359374 | TEST_LT (left, right) -> test_fn exa left test_lt right
360375 | TEST_GT (left, right) -> test_fn exa left test_gt right
376376+ | LINK dest -> link vm exa dest
361377 | HOST dest -> host exa dest
362378 | MAKE -> make vm exa
363379 | GRAB id -> grab exa id
+43
test/End_to_end_test.ml
···11+(* This Source Code Form is subject to the terms of the Mozilla Public
22+ License, v. 2.0. If a copy of the MPL was not distributed with this
33+ file, You can obtain one at https://mozilla.org/MPL/2.0/. *)
44+55+open Exapunks
66+open Helpers
77+88+let () = set_context __FILE__
99+1010+let () =
1111+ specify "tutorial 2" (fun () ->
1212+ let vm = Vm.init () in
1313+ let home = Host.create "home" in
1414+ let inbox = Host.create "inbox" in
1515+ let outbox = Host.create "outbox" in
1616+ Vm.add_host vm home;
1717+ Vm.add_host vm inbox;
1818+ Vm.add_host vm outbox;
1919+ Vm.connect_hosts home [ (800, inbox, -1) ];
2020+ Vm.connect_hosts inbox [ (800, outbox, -1) ];
2121+ let file = File.create "200" ~contents:[ Int 72; Int 52; Int 4; Int 60 ] in
2222+ Vm.place_file vm inbox file;
2323+ let code =
2424+ {|
2525+ LINK 800
2626+ GRAB 200
2727+ COPY F X
2828+ ADDI X F X
2929+ MULI X F X
3030+ SUBI X F X
3131+ COPY X F
3232+ LINK 800
3333+ |}
3434+ in
3535+ (match Vm.create_exa vm home "A" code with
3636+ | Error e -> Alcotest.(check string) "" e (string_of_int (Random.bits ()))
3737+ | Ok _exa ->
3838+ ignore (Vm.run vm);
3939+ let f2 =
4040+ File.create "200" ~contents:[ Int 72; Int 52; Int 4; Int 60; Int 436 ]
4141+ in
4242+ Alcotest.check t_file "file has been changed" f2 file);
4343+ ())
+1
test/Exapunks_test.ml
···22 License, v. 2.0. If a copy of the MPL was not distributed with this
33 file, You can obtain one at https://mozilla.org/MPL/2.0/. *)
4455+open End_to_end_test
56open File_test
67open Reader_test
78open Vm_test
+1-1
test/Helpers.ml
···45454646let setup ?(hostsize = 9) (hostname : string) =
4747 let vm = Vm.init () in
4848- let host = Host.create hostname hostsize in
4848+ let host = Host.create ~size:hostsize hostname in
4949 Vm.add_host vm host;
5050 (vm, host)
+12
test/Vm_test.ml
···4949 ignore (Vm.host exa X);
5050 Alcotest.check t_value "correct" exa.x (Value.key "home");
5151 ())
5252+5353+let () =
5454+ specify "#link" (fun () ->
5555+ let vm, host = setup "home" in
5656+ let dest = Host.create "other" in
5757+ Vm.add_host vm dest;
5858+ Vm.connect_hosts host [ (800, dest, -1) ];
5959+ match Vm.create_exa vm host "A" "LINK 800" with
6060+ | Error e -> Alcotest.(check string) "" e (string_of_int (Random.bits ()))
6161+ | Ok exa ->
6262+ ignore (Vm.tick vm);
6363+ Alcotest.(check string) "" exa.host.name "other")