src: Adding new switch and moving code from Driver to Language

Signed-off-by: Kakadu <Kakadu@pm.me>
This commit is contained in:
Kakadu 2020-10-03 11:49:43 +03:00
parent 6a7ba9df5f
commit c74757cbb7
5 changed files with 268 additions and 61 deletions

View file

@ -1,60 +1,27 @@
open Ostap
let parse cmd =
let s = Util.read cmd#get_infile in
let kws = [
"skip";
"if"; "then"; "else"; "elif"; "fi";
"while"; "do"; "od";
"repeat"; "until";
"for";
"fun"; "local"; "public"; "external"; "return"; "import";
"length";
"string";
"case"; "of"; "esac"; "when";
"boxed"; "unboxed"; "string"; "sexp"; "array";
"infix"; "infixl"; "infixr"; "at"; "before"; "after";
"true"; "false"; "lazy"; "eta"; "syntax"]
in
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.decimal s
inherit Util.Lexers.string s
inherit Util.Lexers.char s
inherit Util.Lexers.infix s
inherit Util.Lexers.lident kws s
inherit Util.Lexers.uident kws s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n\r";
Matcher.Skip.lineComment "--";
Matcher.Skip.nestedComment "(*" "*)"
] s
end
)
(if cmd#is_workaround then ostap (p:!(Language.constparse cmd) -EOF) else ostap (p:!(Language.parse cmd) -EOF))
exception Commandline_error of string
class options args =
let n = Array.length args in
let dump_ast = 1 in
let dump_sm = 2 in
let dump_ast = 0b1 in
let dump_sm = 0b010 in
let dump_source = 0b100 in
(* Kakadu: binary masks are cool for C code, but for OCaml I don't see any reason to save memory like this *)
let help_string =
"Lama compiler. (C) JetBrains Reserach, 2017-2020.\n" ^
"Usage: lamac <options> <input file>\n\n" ^
"When no options specified, builds the source file into executable.\n" ^
"Options:\n" ^
" -c --- compile into object file\n" ^
" -o <file> --- write executable into file <file>\n" ^
" -o <file> --- write executable into file <file>\n" ^
" -I <path> --- add <path> into unit search path list\n" ^
" -i --- interpret on a source-level interpreter\n" ^
" -s --- compile into stack machine code and interpret on the stack machine initerpreter\n" ^
" -dp --- dump AST (the output will be written into .ast file)\n" ^
" -dsrc --- dump pretty-printed source code\n" ^
" -ds --- dump stack machine code (the output will be written into .sm file; has no\n" ^
" effect if -i option is specfied)\n" ^
" -v --- show version\n" ^
" -h --- show this help\n"
" -h --- show this help\n"
in
object (self)
val version = ref false
@ -84,6 +51,7 @@ class options args =
| "-s" -> self#set_mode `SM
| "-i" -> self#set_mode `Eval
| "-ds" -> self#set_dump dump_sm
| "-dsrc" -> self#set_dump dump_source
| "-dp" -> self#set_dump dump_ast
| "-h" -> self#set_help
| "-v" -> self#set_version
@ -97,12 +65,12 @@ class options args =
| None -> ()
in loop ()
(* Workaround until Ostap starts to memoize properly *)
method is_workaround = !const
method is_workaround = !const
method private set_workaround =
const := true
(* end of the workaround *)
method private set_help = help := true
method private set_version = version := true
method private set_version = version := true
method private set_dump mask =
dump := !dump lor mask
method private set_infile name =
@ -160,7 +128,11 @@ class options args =
Buffer.add_string buf "</html>";
self#dump_file "html" (Buffer.contents buf)
)
else ()
method dump_source (ast: Language.Expr.t) =
if (!dump land dump_source) > 0
then Pprinter.pp Format.std_formatter ast;
method dump_SM sm =
if (!dump land dump_sm) > 0
then self#dump_file "sm" (SM.show_prg sm)
@ -169,42 +141,43 @@ class options args =
(match !outfile with
| None -> ()
| Some _ -> (match !mode with `Default -> () | _ -> Printf.printf "Output file option ignored in this mode.\n")
);
);
if !version then Printf.printf "%s\n" Version.version;
if !help then Printf.printf "%s" help_string
method get_debug =
if !debug then "" else "-g"
method set_debug =
debug := true
debug := true
end
let main =
try
try
let cmd = new options Sys.argv in
cmd#greet;
match (try parse cmd with Language.Semantic_error msg -> `Fail msg) with
match (try Language.run_parser cmd with Language.Semantic_error msg -> `Fail msg) with
| `Ok prog ->
cmd#dump_AST (snd prog);
cmd#dump_source (snd prog);
(match cmd#get_mode with
| `Default | `Compile ->
ignore @@ X86.build cmd prog
| _ ->
| _ ->
let rec read acc =
try
let r = read_int () in
Printf.printf "> ";
read (acc @ [r])
with End_of_file -> acc
in
in
let input = read [] in
let output =
if cmd#get_mode = `Eval
then Language.eval prog input
else SM.run (SM.compile cmd prog) input
in
List.iter (fun i -> Printf.printf "%d\n" i) output
List.iter (fun i -> Printf.printf "%d\n" i) output
)
| `Fail er -> Printf.eprintf "Error: %s\n" er; exit 255
with
| Language.Semantic_error msg -> Printf.printf "Error: %s\n" msg; exit 255
| Language.Semantic_error msg -> Printf.printf "Error: %s\n" msg; exit 255
| Commandline_error msg -> Printf.printf "%s\n" msg; exit 255