mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-31 11:08:18 +00:00
External/public, better options
This commit is contained in:
parent
5a883d8fa9
commit
1a849e7a56
12 changed files with 294 additions and 93 deletions
105
src/Driver.ml
105
src/Driver.ml
|
|
@ -8,7 +8,7 @@ let parse infile =
|
|||
"while"; "do"; "od";
|
||||
"repeat"; "until";
|
||||
"for";
|
||||
"fun"; "local"; "return";
|
||||
"fun"; "local"; "public"; "external"; "return";
|
||||
"length";
|
||||
"string";
|
||||
"case"; "of"; "esac"; "when";
|
||||
|
|
@ -32,36 +32,91 @@ let parse infile =
|
|||
)
|
||||
(ostap (!(Language.parse Language.Infix.default) -EOF))
|
||||
|
||||
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)
|
||||
val paths = ref ([] : string list)
|
||||
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
|
||||
|
||||
let main =
|
||||
(* try*)
|
||||
let interpret = Sys.argv.(1) = "-i" in
|
||||
let cmd = new options Sys.argv in
|
||||
|
||||
(*let interpret = Sys.argv.(1) = "-i" 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 (try parse infile with Language.Semantic_error msg -> `Fail msg) with
|
||||
*)
|
||||
match (try parse cmd#get_infile with Language.Semantic_error msg -> `Fail msg) with
|
||||
| `Ok prog ->
|
||||
if to_compile
|
||||
then (
|
||||
let basename = Filename.chop_suffix infile ".expr" in
|
||||
ignore @@ X86.build prog basename
|
||||
)
|
||||
else (
|
||||
(* 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
|
||||
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
|
||||
(match cmd#get_mode with
|
||||
| `Default | `Compile ->
|
||||
ignore @@ X86.build cmd prog
|
||||
| _ ->
|
||||
(* 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
|
||||
in
|
||||
let input = read [] in
|
||||
let output =
|
||||
if cmd#get_mode = `Eval
|
||||
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 "Error: %s\n" er
|
||||
(* with Invalid_argument _ ->
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue