2018-02-25 14:48:13 +03:00
|
|
|
open Ostap
|
|
|
|
|
|
2019-11-29 23:56:03 +03:00
|
|
|
let parse cmd =
|
|
|
|
|
let s = Util.read cmd#get_infile in
|
2019-03-07 19:06:04 +03:00
|
|
|
let kws = [
|
|
|
|
|
"skip";
|
|
|
|
|
"if"; "then"; "else"; "elif"; "fi";
|
|
|
|
|
"while"; "do"; "od";
|
|
|
|
|
"repeat"; "until";
|
|
|
|
|
"for";
|
2019-11-27 03:14:25 +03:00
|
|
|
"fun"; "local"; "public"; "external"; "return"; "import";
|
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-11-29 23:56:03 +03:00
|
|
|
(ostap (p:!(Language.parse cmd) -EOF))
|
2018-02-25 14:48:13 +03:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
exception Commandline_error of string
|
|
|
|
|
|
|
|
|
|
class options args =
|
|
|
|
|
let n = Array.length args in
|
|
|
|
|
let rec fix f = f (fix f) in
|
|
|
|
|
object (self)
|
|
|
|
|
val i = ref 1
|
|
|
|
|
val infile = ref (None : string option)
|
2019-11-27 03:14:25 +03:00
|
|
|
val paths = ref [try Sys.getenv "RC_RUNTIME" with _ -> "../runtime"]
|
2019-11-24 02:30:32 +03:00
|
|
|
val mode = ref (`Default : [`Default | `Eval | `SM | `Compile ])
|
|
|
|
|
val help = ref false
|
|
|
|
|
initializer
|
|
|
|
|
let rec loop () =
|
|
|
|
|
match self#peek with
|
|
|
|
|
| Some opt ->
|
|
|
|
|
(match opt with
|
|
|
|
|
| "-c" -> self#set_mode `Compile
|
|
|
|
|
| "-I" -> (match self#peek with None -> raise (Commandline_error "path expected after '-I' specifier") | Some path -> self#add_include_path path)
|
|
|
|
|
| "-s" -> self#set_mode `SM
|
|
|
|
|
| "-i" -> self#set_mode `Eval
|
|
|
|
|
| "-h" -> self#set_help
|
|
|
|
|
| _ ->
|
|
|
|
|
if opt.[0] = '-'
|
|
|
|
|
then raise (Commandline_error (Printf.sprintf "invalid command line specifier ('%s')" opt))
|
|
|
|
|
else self#set_infile opt
|
|
|
|
|
);
|
|
|
|
|
loop ()
|
|
|
|
|
| None -> ()
|
|
|
|
|
in loop ()
|
|
|
|
|
method private set_infile name =
|
|
|
|
|
match !infile with
|
|
|
|
|
| None -> infile := Some name
|
|
|
|
|
| Some name' -> raise (Commandline_error (Printf.sprintf "input file ('%s') already specified" name'))
|
|
|
|
|
method private add_include_path path =
|
|
|
|
|
paths := path :: !paths
|
|
|
|
|
method private set_mode s =
|
|
|
|
|
match !mode with
|
|
|
|
|
| `Default -> mode := s
|
|
|
|
|
| _ -> raise (Commandline_error "extra compilation mode specifier")
|
|
|
|
|
method private peek =
|
|
|
|
|
let j = !i in
|
|
|
|
|
if j < n
|
|
|
|
|
then (incr i; Some (args.(j)))
|
|
|
|
|
else None
|
|
|
|
|
method private set_help = help := true
|
|
|
|
|
method get_mode = !mode
|
|
|
|
|
method get_infile =
|
|
|
|
|
match !infile with
|
|
|
|
|
| None -> raise (Commandline_error "input file not specified")
|
|
|
|
|
| Some name -> name
|
|
|
|
|
method get_help = !help
|
|
|
|
|
method get_include_paths = !paths
|
|
|
|
|
end
|
|
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
let main =
|
2019-09-19 00:15:02 +03:00
|
|
|
(* try*)
|
2019-11-24 02:30:32 +03:00
|
|
|
let cmd = new options Sys.argv in
|
2019-11-29 23:56:03 +03:00
|
|
|
match (try parse cmd with Language.Semantic_error msg -> `Fail msg) with
|
2018-02-25 14:59:59 +03:00
|
|
|
| `Ok prog ->
|
2019-11-24 02:30:32 +03:00
|
|
|
(match cmd#get_mode with
|
|
|
|
|
| `Default | `Compile ->
|
2019-11-27 03:14:25 +03:00
|
|
|
ignore @@ X86.build cmd prog
|
|
|
|
|
| _ ->
|
2019-11-24 02:30:32 +03:00
|
|
|
(* Printf.printf "Program:\n%s\n" (GT.show(Language.Expr.t) prog);*)
|
|
|
|
|
(*Format.printf "Program\n%s\n%!" (HTML.toHTML ((GT.html(Language.Expr.t)) prog));*)
|
|
|
|
|
let rec read acc =
|
|
|
|
|
try
|
|
|
|
|
let r = read_int () in
|
|
|
|
|
Printf.printf "> ";
|
|
|
|
|
read (acc @ [r])
|
|
|
|
|
with End_of_file -> acc
|
2019-11-27 03:14:25 +03:00
|
|
|
in
|
2019-11-24 02:30:32 +03:00
|
|
|
let input = read [] in
|
|
|
|
|
let output =
|
|
|
|
|
if cmd#get_mode = `Eval
|
|
|
|
|
then Language.eval prog input
|
2019-11-27 03:14:25 +03:00
|
|
|
else SM.run (SM.compile cmd prog) input
|
2019-11-24 02:30:32 +03:00
|
|
|
in
|
2019-11-27 03:14:25 +03:00
|
|
|
List.iter (fun i -> Printf.printf "%d\n" i) output
|
2019-10-11 17:25:58 +03:00
|
|
|
)
|
2019-03-25 00:13:42 +03:00
|
|
|
| `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
|
|
|
*)
|