Added X86 codegeneration interface and tests

This commit is contained in:
Dmitry Boulytchev 2018-03-07 10:18:30 +03:00
parent 77ec064c5c
commit de018e76aa
12 changed files with 320 additions and 34 deletions

View file

@ -14,28 +14,35 @@ let parse infile =
] s
end
)
(ostap (!(Language.Stmt.parse) -EOF))
(ostap (!(Language.parse) -EOF))
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"