diff options
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | examples/add.reg | 2 | ||||
-rw-r--r-- | examples/add.urm | 11 | ||||
-rw-r--r-- | main.ml | 19 | ||||
-rw-r--r-- | makefile | 3 | ||||
-rw-r--r-- | parser.ml | 12 | ||||
-rw-r--r-- | parser.mli | 7 | ||||
-rw-r--r-- | reg.ml | 3 | ||||
-rw-r--r-- | reg.mli | 4 |
9 files changed, 62 insertions, 5 deletions
@@ -19,7 +19,11 @@ Unlimited Register Machine in OCaml. | |||
19 | 19 | ||
20 | ## Usage | 20 | ## Usage |
21 | 21 | ||
22 | TODO: describe usage of the `urm` program. | 22 | ``` |
23 | ./urm <run | trace> <program file> <initial register state file> | ||
24 | ``` | ||
25 | |||
26 | Examples programs are provided in the `examples` folder. | ||
23 | 27 | ||
24 | 28 | ||
25 | ## Authors | 29 | ## Authors |
diff --git a/examples/add.reg b/examples/add.reg new file mode 100644 index 0000000..c24e1f9 --- /dev/null +++ b/examples/add.reg | |||
@@ -0,0 +1,2 @@ | |||
1 | 1 2 | ||
2 | 2 3 | ||
diff --git a/examples/add.urm b/examples/add.urm new file mode 100644 index 0000000..d11eff7 --- /dev/null +++ b/examples/add.urm | |||
@@ -0,0 +1,11 @@ | |||
1 | ZERO 0 | ||
2 | ZERO 3 | ||
3 | JUMP 1 3 6 | ||
4 | SUCC 0 | ||
5 | SUCC 3 | ||
6 | JUMP 3 3 2 | ||
7 | ZERO 3 | ||
8 | JUMP 2 3 11 | ||
9 | SUCC 0 | ||
10 | SUCC 3 | ||
11 | JUMP 3 3 7 | ||
@@ -9,3 +9,22 @@ open Instptr | |||
9 | open Reg | 9 | open Reg |
10 | open Urm | 10 | open Urm |
11 | 11 | ||
12 | let exec_with_resource func filename = | ||
13 | let file = open_in filename in | ||
14 | let res = func file in | ||
15 | close_in file; res | ||
16 | |||
17 | let read_prgm = exec_with_resource (fun f -> string_of_file f |> program_of_string) | ||
18 | let read_regs = exec_with_resource (fun f -> string_of_file f |> regs_of_string) | ||
19 | let run run_func prgm regs = urm_mk prgm regs |> run_func |> regs_string |> print_endline | ||
20 | |||
21 | let run_mode_of_string = function | ||
22 | | "run" -> urm_run | ||
23 | | "trace" -> urm_run_trace | ||
24 | | _ -> failwith "Invalid run mode" | ||
25 | |||
26 | let () = match Sys.argv with | ||
27 | | [| _; "run-tests" |] -> () (* handled in test files *) | ||
28 | | [| _; mode; prgm; regs |] -> run (run_mode_of_string mode) (read_prgm prgm) (read_regs regs) | ||
29 | | _ -> print_endline "Usage: urm <run-tests | run <prgmfile> <regfile> | trace <prgmfile> <regfile>>" | ||
30 | |||
@@ -6,7 +6,8 @@ SOURCES = \ | |||
6 | parser.mli parser.ml \ | 6 | parser.mli parser.ml \ |
7 | instptr.mli instptr.ml \ | 7 | instptr.mli instptr.ml \ |
8 | reg.mli reg.ml \ | 8 | reg.mli reg.ml \ |
9 | urm.mli urm.ml urm_test.ml | 9 | urm.mli urm.ml urm_test.ml \ |
10 | main.ml | ||
10 | 11 | ||
11 | OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile | 12 | OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile |
12 | include $(OCAMLMAKEFILE) | 13 | include $(OCAMLMAKEFILE) |
@@ -20,7 +20,13 @@ let rec program_of_lex = function | |||
20 | | "jump" :: arg_1 :: arg_2 :: arg_3 :: tail -> (Jump ((int_of_string arg_1), (int_of_string arg_2), (int_of_string arg_3))) :: (program_of_lex tail) | 20 | | "jump" :: arg_1 :: arg_2 :: arg_3 :: tail -> (Jump ((int_of_string arg_1), (int_of_string arg_2), (int_of_string arg_3))) :: (program_of_lex tail) |
21 | | _ -> raise Syntax_error | 21 | | _ -> raise Syntax_error |
22 | 22 | ||
23 | let program_of_string str = | 23 | (* FIXME: reject multiple definition of a single register *) |
24 | let lex = Str.split (Str.regexp "[\t\n(),]+") str | 24 | let rec regs_of_lex = function |
25 | in List.iter (fun s -> print_string s; print_newline ()) lex; program_of_lex lex | 25 | | [] -> [] |
26 | | regnum :: regvalue :: tail -> Reg (int_of_string regnum, int_of_string regvalue) :: (regs_of_lex tail) | ||
27 | | _ -> raise Syntax_error | ||
28 | |||
29 | let seq_from_string lexer_func str = Str.split (Str.regexp "[\t\n(), ]+") str |> lexer_func | ||
30 | let program_of_string = seq_from_string program_of_lex | ||
31 | let regs_of_string = seq_from_string regs_of_lex | ||
26 | 32 | ||
@@ -11,5 +11,12 @@ val string_of_file : in_channel -> string | |||
11 | (* Converts lexemes into instructions. *) | 11 | (* Converts lexemes into instructions. *) |
12 | val program_of_lex : string list -> urmcmd list | 12 | val program_of_lex : string list -> urmcmd list |
13 | 13 | ||
14 | (* Converts lexemes into registers. *) | ||
15 | val regs_of_lex : string list -> reg list | ||
16 | |||
14 | (* Parses the string representation of a program. *) | 17 | (* Parses the string representation of a program. *) |
15 | val program_of_string : string -> urmcmd list | 18 | val program_of_string : string -> urmcmd list |
19 | |||
20 | (* Parses the string representation of serialized registers. *) | ||
21 | val regs_of_string : string -> reg list | ||
22 | |||
@@ -8,6 +8,7 @@ open Common | |||
8 | let reg_idx (Reg(idx, _)) = idx | 8 | let reg_idx (Reg(idx, _)) = idx |
9 | let reg_val (Reg(_, value)) = value | 9 | let reg_val (Reg(_, value)) = value |
10 | let reg_compar l r = (reg_val l) - (reg_val r) | 10 | let reg_compar l r = (reg_val l) - (reg_val r) |
11 | let reg_string (Reg (index, value)) = (string_of_int index) ^ ": " ^ (string_of_int value) | ||
11 | 12 | ||
12 | let regs_get reglist index = | 13 | let regs_get reglist index = |
13 | List.find (fun (Reg(idx, _)) -> idx = index) reglist |> reg_val | 14 | List.find (fun (Reg(idx, _)) -> idx = index) reglist |> reg_val |
@@ -15,3 +16,5 @@ let regs_get reglist index = | |||
15 | let regs_set reglist index value = | 16 | let regs_set reglist index value = |
16 | Reg(index, value) :: List.filter (fun (Reg(idx, _)) -> idx != index) reglist | 17 | Reg(index, value) :: List.filter (fun (Reg(idx, _)) -> idx != index) reglist |
17 | 18 | ||
19 | let rec regs_string reglist = List.map (reg_string) reglist |> String.concat ", " | ||
20 | |||
@@ -21,3 +21,7 @@ val regs_get : reg list -> regidx -> regval | |||
21 | (* Set the value of the register to value, | 21 | (* Set the value of the register to value, |
22 | * or creates it to the value specified if it does not exist *) | 22 | * or creates it to the value specified if it does not exist *) |
23 | val regs_set : reg list -> regidx -> regval -> reg list | 23 | val regs_set : reg list -> regidx -> regval -> reg list |
24 | |||
25 | (* Returns the string representation of a register list. *) | ||
26 | val regs_string : reg list -> string | ||
27 | |||