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-04-25 01:06:18 +03:00
|
|
|
inherit Util.Lexers.string s
|
|
|
|
|
inherit Util.Lexers.char s
|
2018-05-01 02:57:09 +03:00
|
|
|
inherit Util.Lexers.ident ["skip";
|
|
|
|
|
"if"; "then"; "else"; "elif"; "fi";
|
|
|
|
|
"while"; "do"; "od";
|
|
|
|
|
"repeat"; "until";
|
|
|
|
|
"for";
|
|
|
|
|
"fun"; "local"; "return";
|
|
|
|
|
"length";
|
|
|
|
|
"case"; "of"; "esac"; "when"] 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
|
2018-03-04 23:13:08 +03:00
|
|
|
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
|
2018-02-25 14:48:13 +03:00
|
|
|
match parse infile with
|
2018-02-25 14:59:59 +03:00
|
|
|
| `Ok prog ->
|
2018-03-04 23:13:08 +03:00
|
|
|
if to_compile
|
2018-04-11 00:47:46 +03:00
|
|
|
then
|
2018-03-04 23:13:08 +03:00
|
|
|
let basename = Filename.chop_suffix infile ".expr" in
|
2018-04-11 00:47:46 +03:00
|
|
|
ignore @@ X86.build prog basename
|
2018-03-04 23:13:08 +03:00
|
|
|
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
|
2018-03-27 03:13:00 +03:00
|
|
|
else SM.run (SM.compile prog) input
|
2018-03-04 23:13:08 +03:00
|
|
|
in
|
|
|
|
|
List.iter (fun i -> Printf.printf "%d\n" i) output
|
2018-02-25 14:48:13 +03:00
|
|
|
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
|
|
|
|
|
with Invalid_argument _ ->
|
2018-03-04 23:13:08 +03:00
|
|
|
Printf.printf "Usage: rc [-i | -s] <input file.expr>\n"
|