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 = [
|
2020-04-11 21:09:51 +03:00
|
|
|
"skip";
|
2019-03-07 19:06:04 +03:00
|
|
|
"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";
|
2019-12-23 18:40:48 +03:00
|
|
|
"infix"; "infixl"; "infixr"; "at"; "before"; "after";
|
2020-04-11 21:09:51 +03:00
|
|
|
"true"; "false"; "lazy"; "eta"; "syntax"]
|
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
|
2020-01-14 03:30:17 +03:00
|
|
|
inherit Util.Lexers.infix 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 [
|
2020-02-17 01:51:52 +03:00
|
|
|
Matcher.Skip.whitespaces " \t\n\r";
|
2018-02-25 14:48:13 +03:00
|
|
|
Matcher.Skip.lineComment "--";
|
|
|
|
|
Matcher.Skip.nestedComment "(*" "*)"
|
|
|
|
|
] s
|
|
|
|
|
end
|
|
|
|
|
)
|
2020-01-24 22:30:49 +03:00
|
|
|
(if cmd#is_workaround then ostap (p:!(Language.constparse cmd) -EOF) else 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
|
2019-12-12 17:42:45 +03:00
|
|
|
let dump_ast = 1 in
|
|
|
|
|
let dump_sm = 2 in
|
2020-02-16 01:21:27 +03:00
|
|
|
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" ^
|
2020-02-23 22:15:27 +03:00
|
|
|
" -o <file> --- write executable into file <file>\n" ^
|
2020-02-16 01:21:27 +03:00
|
|
|
" -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" ^
|
|
|
|
|
" -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"
|
|
|
|
|
in
|
2019-11-24 02:30:32 +03:00
|
|
|
object (self)
|
2020-02-16 01:21:27 +03:00
|
|
|
val version = ref false
|
|
|
|
|
val help = ref false
|
|
|
|
|
val i = ref 1
|
|
|
|
|
val infile = ref (None : string option)
|
2020-02-23 22:15:27 +03:00
|
|
|
val outfile = ref (None : string option)
|
2020-02-16 01:21:27 +03:00
|
|
|
val paths = ref [X86.get_std_path ()]
|
|
|
|
|
val mode = ref (`Default : [`Default | `Eval | `SM | `Compile ])
|
2020-09-04 00:25:07 +03:00
|
|
|
val curdir = Unix.getcwd ()
|
2020-01-24 22:30:49 +03:00
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
|
|
|
|
val const = ref false
|
|
|
|
|
(* end of the workaround *)
|
2019-12-12 17:42:45 +03:00
|
|
|
val dump = ref 0
|
2019-11-24 02:30:32 +03:00
|
|
|
initializer
|
|
|
|
|
let rec loop () =
|
|
|
|
|
match self#peek with
|
|
|
|
|
| Some opt ->
|
|
|
|
|
(match opt with
|
2020-01-24 22:30:49 +03:00
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
|
|
|
|
| "-w" -> self#set_workaround
|
|
|
|
|
(* end of the workaround *)
|
2019-12-12 17:42:45 +03:00
|
|
|
| "-c" -> self#set_mode `Compile
|
2020-02-23 22:15:27 +03:00
|
|
|
| "-o" -> (match self#peek with None -> raise (Commandline_error "File name expected after '-o' specifier") | Some fname -> self#set_outfile fname)
|
2020-02-16 01:21:27 +03:00
|
|
|
| "-I" -> (match self#peek with None -> raise (Commandline_error "Path expected after '-I' specifier") | Some path -> self#add_include_path path)
|
2019-12-12 17:42:45 +03:00
|
|
|
| "-s" -> self#set_mode `SM
|
|
|
|
|
| "-i" -> self#set_mode `Eval
|
|
|
|
|
| "-ds" -> self#set_dump dump_sm
|
|
|
|
|
| "-dp" -> self#set_dump dump_ast
|
|
|
|
|
| "-h" -> self#set_help
|
2020-02-16 01:21:27 +03:00
|
|
|
| "-v" -> self#set_version
|
2019-11-24 02:30:32 +03:00
|
|
|
| _ ->
|
|
|
|
|
if opt.[0] = '-'
|
2020-02-16 01:21:27 +03:00
|
|
|
then raise (Commandline_error (Printf.sprintf "Invalid command line specifier ('%s')" opt))
|
2019-11-24 02:30:32 +03:00
|
|
|
else self#set_infile opt
|
|
|
|
|
);
|
|
|
|
|
loop ()
|
|
|
|
|
| None -> ()
|
|
|
|
|
in loop ()
|
2020-01-24 22:30:49 +03:00
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
|
|
|
|
method is_workaround = !const
|
|
|
|
|
method private set_workaround =
|
|
|
|
|
const := true
|
|
|
|
|
(* end of the workaround *)
|
2020-02-16 01:21:27 +03:00
|
|
|
method private set_help = help := true
|
|
|
|
|
method private set_version = version := true
|
2019-12-12 17:42:45 +03:00
|
|
|
method private set_dump mask =
|
|
|
|
|
dump := !dump lor mask
|
2019-11-24 02:30:32 +03:00
|
|
|
method private set_infile name =
|
|
|
|
|
match !infile with
|
|
|
|
|
| None -> infile := Some name
|
2020-02-16 01:21:27 +03:00
|
|
|
| Some name' -> raise (Commandline_error (Printf.sprintf "Input file ('%s') already specified" name'))
|
2020-02-23 22:15:27 +03:00
|
|
|
method private set_outfile name =
|
|
|
|
|
match !outfile with
|
|
|
|
|
| None -> outfile := Some name
|
|
|
|
|
| Some name' -> raise (Commandline_error (Printf.sprintf "Output file ('%s') already specified" name'))
|
2019-11-24 02:30:32 +03:00
|
|
|
method private add_include_path path =
|
|
|
|
|
paths := path :: !paths
|
|
|
|
|
method private set_mode s =
|
|
|
|
|
match !mode with
|
|
|
|
|
| `Default -> mode := s
|
2020-02-16 01:21:27 +03:00
|
|
|
| _ -> raise (Commandline_error "Extra compilation mode specifier")
|
2019-11-24 02:30:32 +03:00
|
|
|
method private peek =
|
|
|
|
|
let j = !i in
|
|
|
|
|
if j < n
|
|
|
|
|
then (incr i; Some (args.(j)))
|
|
|
|
|
else None
|
|
|
|
|
method get_mode = !mode
|
2020-02-23 22:15:27 +03:00
|
|
|
method get_output_option =
|
|
|
|
|
match !outfile with
|
|
|
|
|
| None -> Printf.sprintf "-o %s" self#basename
|
|
|
|
|
| Some name -> Printf.sprintf "-o %s" name
|
2020-09-04 00:25:07 +03:00
|
|
|
method get_absolute_infile =
|
|
|
|
|
let f = self#get_infile in
|
|
|
|
|
if Filename.is_relative f then Filename.concat curdir f else f
|
2019-11-24 02:30:32 +03:00
|
|
|
method get_infile =
|
|
|
|
|
match !infile with
|
2020-02-16 01:21:27 +03:00
|
|
|
| None -> raise (Commandline_error "Input file not specified")
|
2019-11-24 02:30:32 +03:00
|
|
|
| Some name -> name
|
|
|
|
|
method get_help = !help
|
|
|
|
|
method get_include_paths = !paths
|
2020-01-26 06:06:14 +03:00
|
|
|
method basename = Filename.chop_suffix (Filename.basename self#get_infile) ".expr"
|
|
|
|
|
method topname =
|
|
|
|
|
match !mode with
|
|
|
|
|
| `Compile -> "init" ^ self#basename
|
|
|
|
|
| _ -> "main"
|
2019-12-12 17:42:45 +03:00
|
|
|
method dump_file ext contents =
|
|
|
|
|
let name = self#basename in
|
|
|
|
|
let outf = open_out (Printf.sprintf "%s.%s" name ext) in
|
|
|
|
|
Printf.fprintf outf "%s" contents;
|
|
|
|
|
close_out outf
|
|
|
|
|
method dump_AST ast =
|
|
|
|
|
if (!dump land dump_ast) > 0
|
2020-02-18 13:28:12 +03:00
|
|
|
then (
|
|
|
|
|
let buf = Buffer.create 1024 in
|
|
|
|
|
Buffer.add_string buf "<html>";
|
|
|
|
|
Buffer.add_string buf (Printf.sprintf "<title> %s </title>" self#get_infile);
|
|
|
|
|
Buffer.add_string buf "<body><li>";
|
|
|
|
|
GT.html(Language.Expr.t) ast buf;
|
|
|
|
|
Buffer.add_string buf "</li></body>";
|
|
|
|
|
Buffer.add_string buf "</html>";
|
|
|
|
|
self#dump_file "html" (Buffer.contents buf)
|
|
|
|
|
)
|
2019-12-12 17:42:45 +03:00
|
|
|
else ()
|
|
|
|
|
method dump_SM sm =
|
|
|
|
|
if (!dump land dump_sm) > 0
|
|
|
|
|
then self#dump_file "sm" (SM.show_prg sm)
|
|
|
|
|
else ()
|
2020-02-16 01:21:27 +03:00
|
|
|
method greet =
|
2020-02-23 22:15:27 +03:00
|
|
|
(match !outfile with
|
|
|
|
|
| None -> ()
|
|
|
|
|
| Some _ -> (match !mode with `Default -> () | _ -> Printf.printf "Output file option ignored in this mode.\n")
|
|
|
|
|
);
|
2020-02-16 01:21:27 +03:00
|
|
|
if !version then Printf.printf "%s\n" Version.version;
|
|
|
|
|
if !help then Printf.printf "%s" help_string
|
2019-11-24 02:30:32 +03:00
|
|
|
end
|
|
|
|
|
|
2018-02-25 14:48:13 +03:00
|
|
|
let main =
|
2020-01-14 05:36:03 +03:00
|
|
|
try
|
2019-11-24 02:30:32 +03:00
|
|
|
let cmd = new options Sys.argv in
|
2020-02-16 01:21:27 +03:00
|
|
|
cmd#greet;
|
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-12-12 17:42:45 +03:00
|
|
|
cmd#dump_AST (snd 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
|
|
|
|
|
| _ ->
|
2020-01-14 05:36:03 +03:00
|
|
|
let rec read acc =
|
2019-11-24 02:30:32 +03:00
|
|
|
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
|
2020-02-16 01:21:27 +03:00
|
|
|
with
|
|
|
|
|
| Language.Semantic_error msg -> Printf.printf "Error: %s\n" msg
|
|
|
|
|
| Commandline_error msg -> Printf.printf "%s\n" msg
|