aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-04-22 22:39:56 +0200
committerpacien2018-04-22 22:39:56 +0200
commit0027c7b647e1b345badbb01bcafd22feee075470 (patch)
tree8cf72c45c517b20c1adfe4eba25f8b3358115282
parentdb9de5bc717be46f0ca2dc1aa975c75adca6264d (diff)
downloadurm-0027c7b647e1b345badbb01bcafd22feee075470.tar.gz
Implement program execution from command line
-rw-r--r--README.md6
-rw-r--r--examples/add.reg2
-rw-r--r--examples/add.urm11
-rw-r--r--main.ml19
-rw-r--r--makefile3
-rw-r--r--parser.ml12
-rw-r--r--parser.mli7
-rw-r--r--reg.ml3
-rw-r--r--reg.mli4
9 files changed, 62 insertions, 5 deletions
diff --git a/README.md b/README.md
index 2971b4c..2cce7bf 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,11 @@ Unlimited Register Machine in OCaml.
19 19
20## Usage 20## Usage
21 21
22TODO: describe usage of the `urm` program. 22```
23./urm <run | trace> <program file> <initial register state file>
24```
25
26Examples 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 @@
11 2
22 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 @@
1ZERO 0
2ZERO 3
3JUMP 1 3 6
4SUCC 0
5SUCC 3
6JUMP 3 3 2
7ZERO 3
8JUMP 2 3 11
9SUCC 0
10SUCC 3
11JUMP 3 3 7
diff --git a/main.ml b/main.ml
index 1bb6704..2f8d57c 100644
--- a/main.ml
+++ b/main.ml
@@ -9,3 +9,22 @@ open Instptr
9open Reg 9open Reg
10open Urm 10open Urm
11 11
12let exec_with_resource func filename =
13 let file = open_in filename in
14 let res = func file in
15 close_in file; res
16
17let read_prgm = exec_with_resource (fun f -> string_of_file f |> program_of_string)
18let read_regs = exec_with_resource (fun f -> string_of_file f |> regs_of_string)
19let run run_func prgm regs = urm_mk prgm regs |> run_func |> regs_string |> print_endline
20
21let run_mode_of_string = function
22 | "run" -> urm_run
23 | "trace" -> urm_run_trace
24 | _ -> failwith "Invalid run mode"
25
26let () = 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
diff --git a/makefile b/makefile
index 2ccd250..6345a40 100644
--- a/makefile
+++ b/makefile
@@ -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
11OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile 12OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile
12include $(OCAMLMAKEFILE) 13include $(OCAMLMAKEFILE)
diff --git a/parser.ml b/parser.ml
index 72042d2..1f367d1 100644
--- a/parser.ml
+++ b/parser.ml
@@ -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
23let program_of_string str = 23(* FIXME: reject multiple definition of a single register *)
24 let lex = Str.split (Str.regexp "[\t\n(),]+") str 24let 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
29let seq_from_string lexer_func str = Str.split (Str.regexp "[\t\n(), ]+") str |> lexer_func
30let program_of_string = seq_from_string program_of_lex
31let regs_of_string = seq_from_string regs_of_lex
26 32
diff --git a/parser.mli b/parser.mli
index d210396..f7609f9 100644
--- a/parser.mli
+++ b/parser.mli
@@ -11,5 +11,12 @@ val string_of_file : in_channel -> string
11(* Converts lexemes into instructions. *) 11(* Converts lexemes into instructions. *)
12val program_of_lex : string list -> urmcmd list 12val program_of_lex : string list -> urmcmd list
13 13
14(* Converts lexemes into registers. *)
15val regs_of_lex : string list -> reg list
16
14(* Parses the string representation of a program. *) 17(* Parses the string representation of a program. *)
15val program_of_string : string -> urmcmd list 18val program_of_string : string -> urmcmd list
19
20(* Parses the string representation of serialized registers. *)
21val regs_of_string : string -> reg list
22
diff --git a/reg.ml b/reg.ml
index c452282..45bbb4b 100644
--- a/reg.ml
+++ b/reg.ml
@@ -8,6 +8,7 @@ open Common
8let reg_idx (Reg(idx, _)) = idx 8let reg_idx (Reg(idx, _)) = idx
9let reg_val (Reg(_, value)) = value 9let reg_val (Reg(_, value)) = value
10let reg_compar l r = (reg_val l) - (reg_val r) 10let reg_compar l r = (reg_val l) - (reg_val r)
11let reg_string (Reg (index, value)) = (string_of_int index) ^ ": " ^ (string_of_int value)
11 12
12let regs_get reglist index = 13let 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 =
15let regs_set reglist index value = 16let 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
19let rec regs_string reglist = List.map (reg_string) reglist |> String.concat ", "
20
diff --git a/reg.mli b/reg.mli
index 15d53de..f5e41a0 100644
--- a/reg.mli
+++ b/reg.mli
@@ -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 *)
23val regs_set : reg list -> regidx -> regval -> reg list 23val regs_set : reg list -> regidx -> regval -> reg list
24
25(* Returns the string representation of a register list. *)
26val regs_string : reg list -> string
27