diff --git a/Makefile b/Makefile index 19a6c8037..466ce8eaa 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ SHELL := /bin/bash all: pushd src && make && popd + pushd runtime && make && popd install: ; @@ -12,5 +13,6 @@ regression: clean: pushd src && make clean && popd + pushd runtime && make clean && popd pushd regression && make clean && popd diff --git a/regression/Makefile b/regression/Makefile index a3f16f0ae..021bea2d6 100644 --- a/regression/Makefile +++ b/regression/Makefile @@ -7,6 +7,7 @@ RC=../src/rc.opt check: $(TESTS) $(TESTS): %: %.expr + $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log diff --git a/regression/deep-expressions/Makefile b/regression/deep-expressions/Makefile index 939e151d8..eb36d1702 100644 --- a/regression/deep-expressions/Makefile +++ b/regression/deep-expressions/Makefile @@ -7,6 +7,7 @@ RC = ../../src/rc.opt check: $(TESTS) $(TESTS): %: %.expr + RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log diff --git a/regression/expressions/Makefile b/regression/expressions/Makefile index 939e151d8..eb36d1702 100644 --- a/regression/expressions/Makefile +++ b/regression/expressions/Makefile @@ -7,6 +7,7 @@ RC = ../../src/rc.opt check: $(TESTS) $(TESTS): %: %.expr + RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log diff --git a/runtime/Makefile b/runtime/Makefile new file mode 100644 index 000000000..c4414a5c8 --- /dev/null +++ b/runtime/Makefile @@ -0,0 +1,6 @@ +all: + gcc -m32 -c runtime.c + +clean: + rm -f runtime.o *~ + diff --git a/runtime/runtime.c b/runtime/runtime.c new file mode 100644 index 000000000..c24283f99 --- /dev/null +++ b/runtime/runtime.c @@ -0,0 +1,22 @@ +/* Runtime library */ + +# include + +/* Lread is an implementation of the "read" construct */ +extern int Lread () { + int result; + + printf ("> "); + fflush (stdout); + scanf ("%d", &result); + + return result; +} + +/* Lwrite is an implementation of the "write" construct */ +extern int Lwrite (int n) { + printf ("%d\n", n); + fflush (stdout); + + return 0; +} diff --git a/src/Driver.ml b/src/Driver.ml index 67dd012d7..f500a3774 100644 --- a/src/Driver.ml +++ b/src/Driver.ml @@ -19,23 +19,30 @@ let parse infile = let main = try let interpret = Sys.argv.(1) = "-i" in - let infile = Sys.argv.(2) in + let stack = Sys.argv.(1) = "-s" in + let to_compile = not (interpret || stack) in + let infile = Sys.argv.(if not to_compile then 2 else 1) in match parse infile with | `Ok prog -> - let rec read acc = - try - let r = read_int () in - Printf.printf "> "; - read (acc @ [r]) - with End_of_file -> acc - in - let input = read [] in - let output = - if interpret - then Language.eval prog input - else SM.run (SM.compile prog) input - in - List.iter (fun i -> Printf.printf "%d\n" i) output + if to_compile + then + let basename = Filename.chop_suffix infile ".expr" in + ignore @@ X86.build prog basename + else + let rec read acc = + try + let r = read_int () in + Printf.printf "> "; + read (acc @ [r]) + with End_of_file -> acc + in + let input = read [] in + let output = + if interpret + then Language.eval prog input + else SM.run (SM.compile prog) input + in + List.iter (fun i -> Printf.printf "%d\n" i) output | `Fail er -> Printf.eprintf "Syntax error: %s\n" er with Invalid_argument _ -> - Printf.printf "Usage: rc [-i] \n" + Printf.printf "Usage: rc [-i | -s] \n" diff --git a/src/Makefile b/src/Makefile index 5f5d854be..8be318c26 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ TOPFILE = rc OCAMLC = ocamlc OCAMLOPT = ocamlopt OCAMLDEP = ocamldep -SOURCES = Language.ml SM.ml Driver.ml +SOURCES = Language.ml SM.ml X86.ml Driver.ml LIBS = GT.cma unix.cma re.cma re_emacs.cma re_str.cma CAMLP5 = -pp "camlp5o -I `ocamlfind -query GT.syntax` -I `ocamlfind -query ostap.syntax` pa_ostap.cmo pa_gt.cmo -L `ocamlfind -query GT.syntax`" PXFLAGS = $(CAMLP5) diff --git a/src/SM.ml b/src/SM.ml index c15137de3..98b00a9ba 100644 --- a/src/SM.ml +++ b/src/SM.ml @@ -7,7 +7,6 @@ open Language (* put a constant on the stack *) | CONST of int (* read to stack *) | READ (* write from stack *) | WRITE -(* put a constant of the stack *) | CONST of int (* load a variable to the stack *) | LD of string (* store a variable from the stack *) | ST of string with show