2018-02-25 14:48:13 +03:00
|
|
|
open Ostap
|
|
|
|
|
|
|
|
|
|
let parse infile =
|
|
|
|
|
let s = Util.read infile in
|
|
|
|
|
Util.parse
|
|
|
|
|
(object
|
|
|
|
|
inherit Matcher.t s
|
|
|
|
|
inherit Util.Lexers.decimal s
|
2018-02-25 17:39:54 +03:00
|
|
|
inherit Util.Lexers.ident ["read"; "write"] s
|
2018-02-25 14:48:13 +03:00
|
|
|
inherit Util.Lexers.skip [
|
|
|
|
|
Matcher.Skip.whitespaces " \t\n";
|
|
|
|
|
Matcher.Skip.lineComment "--";
|
|
|
|
|
Matcher.Skip.nestedComment "(*" "*)"
|
|
|
|
|
] s
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
(ostap (!(Language.parse) -EOF))
|
|
|
|
|
|
|
|
|
|
let main =
|
|
|
|
|
try
|
|
|
|
|
let interpret = Sys.argv.(1) = "-i" in
|
|
|
|
|
let infile = Sys.argv.(2) in
|
|
|
|
|
match parse infile with
|
2018-02-25 14:59:59 +03:00
|
|
|
| `Ok prog ->
|
2018-02-25 14:48:13 +03:00
|
|
|
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
|
2018-02-25 14:59:59 +03:00
|
|
|
else SM.run (SM.compile prog) input
|
2018-02-25 14:48:13 +03:00
|
|
|
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"
|