x86 for the linear language

This commit is contained in:
Dmitry Boulytchev 2018-03-04 23:13:08 +03:00
parent 4ff1bf9858
commit 64172f66d3
9 changed files with 57 additions and 18 deletions

View file

@ -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] <input file.expr>\n"
Printf.printf "Usage: rc [-i | -s] <input file.expr>\n"

View file

@ -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)

View file

@ -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