2018-02-25 14:48:13 +03:00
|
|
|
open Ostap
|
|
|
|
|
|
|
|
|
|
let parse infile =
|
2019-03-07 19:06:04 +03:00
|
|
|
let s = Util.read infile in
|
|
|
|
|
let kws = [
|
|
|
|
|
"skip";
|
|
|
|
|
"if"; "then"; "else"; "elif"; "fi";
|
|
|
|
|
"while"; "do"; "od";
|
|
|
|
|
"repeat"; "until";
|
|
|
|
|
"for";
|
2019-09-19 15:52:20 +03:00
|
|
|
"fun"; "local"; "return"; "global";
|
2019-03-07 19:06:04 +03:00
|
|
|
"length";
|
|
|
|
|
"string";
|
|
|
|
|
"case"; "of"; "esac"; "when";
|
2019-03-25 00:13:42 +03:00
|
|
|
"boxed"; "unboxed"; "string"; "sexp"; "array";
|
|
|
|
|
"infix"; "infixl"; "infixr"; "at"; "before"; "after"]
|
2019-03-07 19:06:04 +03:00
|
|
|
in
|
2018-02-25 14:48:13 +03:00
|
|
|
Util.parse
|
|
|
|
|
(object
|
2018-12-03 14:20:47 +03:00
|
|
|
inherit Matcher.t s
|
2018-02-25 14:48:13 +03:00
|
|
|
inherit Util.Lexers.decimal s
|
2018-04-25 01:06:18 +03:00
|
|
|
inherit Util.Lexers.string s
|
|
|
|
|
inherit Util.Lexers.char s
|
2019-03-07 19:06:04 +03:00
|
|
|
inherit Util.Lexers.lident kws s
|
|
|
|
|
inherit Util.Lexers.uident kws 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
|
|
|
|
|
)
|
2019-09-10 01:03:23 +03:00
|
|
|
(ostap (!(Language.parse Language.Infix.default) -EOF))
|
2018-02-25 14:48:13 +03:00
|
|
|
|
|
|
|
|
let main =
|
2019-09-19 00:15:02 +03:00
|
|
|
(* try*)
|
2018-02-25 14:48:13 +03:00
|
|
|
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
|
2019-08-14 01:59:44 +03:00
|
|
|
let infile = Sys.argv.(if not to_compile then 2 else 1) in
|
2019-03-25 00:13:42 +03:00
|
|
|
match (try parse infile with Language.Semantic_error msg -> `Fail msg) with
|
2018-02-25 14:59:59 +03:00
|
|
|
| `Ok prog ->
|
2019-03-25 00:13:42 +03:00
|
|
|
if to_compile
|
2019-08-14 01:59:44 +03:00
|
|
|
then
|
2019-03-25 00:13:42 +03:00
|
|
|
let basename = Filename.chop_suffix infile ".expr" in
|
2019-09-22 20:15:15 +03:00
|
|
|
(* ignore @@ X86.build prog basename *) (* TODO! *) ()
|
2019-08-14 01:59:44 +03:00
|
|
|
else
|
2019-03-25 00:13:42 +03:00
|
|
|
let rec read acc =
|
|
|
|
|
try
|
|
|
|
|
let r = read_int () in
|
|
|
|
|
Printf.printf "> ";
|
2019-08-14 01:59:44 +03:00
|
|
|
read (acc @ [r])
|
2019-03-25 00:13:42 +03:00
|
|
|
with End_of_file -> acc
|
|
|
|
|
in
|
2019-08-14 01:59:44 +03:00
|
|
|
let input = read [] in
|
|
|
|
|
let output =
|
|
|
|
|
if interpret
|
|
|
|
|
then Language.eval prog input
|
2019-09-29 02:35:04 +03:00
|
|
|
else SM.run (SM.compile prog) input
|
2019-03-25 00:13:42 +03:00
|
|
|
in
|
|
|
|
|
List.iter (fun i -> Printf.printf "%d\n" i) output
|
|
|
|
|
| `Fail er -> Printf.eprintf "Error: %s\n" er
|
2019-09-19 00:15:02 +03:00
|
|
|
(* with Invalid_argument _ ->
|
2018-03-04 23:13:08 +03:00
|
|
|
Printf.printf "Usage: rc [-i | -s] <input file.expr>\n"
|
2019-09-19 00:15:02 +03:00
|
|
|
*)
|