2019-11-24 02:30:32 +03:00
|
|
|
exception Commandline_error of string
|
2020-10-03 11:49:43 +03:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
class options args =
|
|
|
|
|
let n = Array.length args in
|
2023-09-04 21:43:28 +02:00
|
|
|
let dump_ast = 0b1 in
|
|
|
|
|
let dump_sm = 0b010 in
|
2020-10-03 11:49:43 +03:00
|
|
|
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 *)
|
2020-02-16 01:21:27 +03:00
|
|
|
let help_string =
|
2023-09-04 21:43:28 +02:00
|
|
|
"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"
|
|
|
|
|
^ " -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"
|
|
|
|
|
^ " -b --- compile to a stack machine bytecode\n"
|
|
|
|
|
^ " -v --- show version\n" ^ " -h --- show this help\n"
|
2020-02-16 01:21:27 +03:00
|
|
|
in
|
2019-11-24 02:30:32 +03:00
|
|
|
object (self)
|
2020-02-16 01:21:27 +03:00
|
|
|
val version = ref false
|
2023-09-04 21:43:28 +02:00
|
|
|
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)
|
2024-07-01 12:06:30 +02:00
|
|
|
val paths = ref [ X86_64.get_std_path () ]
|
2023-09-04 21:43:28 +02:00
|
|
|
val mode = ref (`Default : [ `Default | `Eval | `SM | `Compile | `BC ])
|
|
|
|
|
val curdir = Unix.getcwd ()
|
|
|
|
|
val debug = ref false
|
|
|
|
|
|
2020-01-24 22:30:49 +03:00
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
2023-09-04 21:43:28 +02:00
|
|
|
val const = ref false
|
|
|
|
|
|
2020-01-24 22:30:49 +03:00
|
|
|
(* end of the workaround *)
|
2023-09-04 21:43:28 +02:00
|
|
|
val dump = ref 0
|
|
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
initializer
|
2023-09-04 21:43:28 +02:00
|
|
|
let rec loop () =
|
|
|
|
|
match self#peek with
|
|
|
|
|
| Some opt ->
|
|
|
|
|
(match opt with
|
|
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
|
|
|
|
| "-w" -> self#set_workaround
|
|
|
|
|
(* end of the workaround *)
|
|
|
|
|
| "-c" -> self#set_mode `Compile
|
|
|
|
|
| "-o" -> (
|
|
|
|
|
match self#peek with
|
|
|
|
|
| None ->
|
|
|
|
|
raise
|
|
|
|
|
(Commandline_error "File name expected after '-o' specifier")
|
|
|
|
|
| Some fname -> self#set_outfile fname)
|
|
|
|
|
| "-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
|
|
|
|
|
| "-b" -> self#set_mode `BC
|
|
|
|
|
| "-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
|
|
|
|
|
| "-g" -> self#set_debug
|
|
|
|
|
| _ ->
|
|
|
|
|
if opt.[0] = '-' then
|
|
|
|
|
raise
|
|
|
|
|
(Commandline_error
|
|
|
|
|
(Printf.sprintf "Invalid command line specifier ('%s')" opt))
|
|
|
|
|
else self#set_infile opt);
|
|
|
|
|
loop ()
|
|
|
|
|
| None -> ()
|
|
|
|
|
in
|
|
|
|
|
loop ()
|
|
|
|
|
|
2020-01-24 22:30:49 +03:00
|
|
|
(* Workaround until Ostap starts to memoize properly *)
|
2020-10-03 11:49:43 +03:00
|
|
|
method is_workaround = !const
|
2023-09-04 21:43:28 +02:00
|
|
|
method private set_workaround = const := true
|
|
|
|
|
|
2020-01-24 22:30:49 +03:00
|
|
|
(* end of the workaround *)
|
2023-09-04 21:43:28 +02:00
|
|
|
method private set_help = help := true
|
2020-10-03 11:49:43 +03:00
|
|
|
method private set_version = version := true
|
2023-09-04 21:43:28 +02: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
|
2023-09-04 21:43:28 +02:00
|
|
|
| None -> infile := Some name
|
|
|
|
|
| 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
|
2023-09-04 21:43:28 +02:00
|
|
|
| None -> outfile := Some name
|
|
|
|
|
| Some name' ->
|
|
|
|
|
raise
|
|
|
|
|
(Commandline_error
|
|
|
|
|
(Printf.sprintf "Output file ('%s') already specified" name'))
|
|
|
|
|
|
|
|
|
|
method private add_include_path path = paths := path :: !paths
|
|
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
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")
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
method private peek =
|
|
|
|
|
let j = !i in
|
2023-09-04 21:43:28 +02:00
|
|
|
if j < n then (
|
|
|
|
|
incr i;
|
|
|
|
|
Some args.(j))
|
2019-11-24 02:30:32 +03:00
|
|
|
else None
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
method get_mode = !mode
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2020-02-23 22:15:27 +03:00
|
|
|
method get_output_option =
|
|
|
|
|
match !outfile with
|
2023-09-04 21:43:28 +02:00
|
|
|
| None -> Printf.sprintf "-o %s" self#basename
|
2020-02-23 22:15:27 +03:00
|
|
|
| Some name -> Printf.sprintf "-o %s" name
|
2023-09-04 21:43:28 +02:00
|
|
|
|
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
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
method get_infile =
|
|
|
|
|
match !infile with
|
2023-09-04 21:43:28 +02:00
|
|
|
| None -> raise (Commandline_error "Input file not specified")
|
2019-11-24 02:30:32 +03:00
|
|
|
| Some name -> name
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2019-11-24 02:30:32 +03:00
|
|
|
method get_help = !help
|
|
|
|
|
method get_include_paths = !paths
|
2023-09-04 21:43:28 +02:00
|
|
|
|
|
|
|
|
method basename =
|
|
|
|
|
Filename.chop_suffix (Filename.basename self#get_infile) ".lama"
|
|
|
|
|
|
2020-01-26 06:06:14 +03:00
|
|
|
method topname =
|
2024-07-01 11:37:41 +02:00
|
|
|
match !mode with `Compile -> "init" ^ self#basename | _ -> "main"
|
2023-09-04 21:43:28 +02:00
|
|
|
|
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
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2019-12-12 17:42:45 +03:00
|
|
|
method dump_AST ast =
|
2023-09-04 21:43:28 +02:00
|
|
|
if !dump land dump_ast > 0 then (
|
2020-02-18 13:28:12 +03:00
|
|
|
let buf = Buffer.create 1024 in
|
|
|
|
|
Buffer.add_string buf "<html>";
|
2023-09-04 21:43:28 +02:00
|
|
|
Buffer.add_string buf
|
|
|
|
|
(Printf.sprintf "<title> %s </title>" self#get_infile);
|
2020-02-18 13:28:12 +03:00
|
|
|
Buffer.add_string buf "<body><li>";
|
2023-09-04 21:43:28 +02:00
|
|
|
GT.html Language.Expr.t ast buf;
|
2020-02-18 13:28:12 +03:00
|
|
|
Buffer.add_string buf "</li></body>";
|
|
|
|
|
Buffer.add_string buf "</html>";
|
2023-09-04 21:43:28 +02:00
|
|
|
self#dump_file "html" (Buffer.contents buf))
|
|
|
|
|
|
|
|
|
|
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)
|
2019-12-12 17:42:45 +03:00
|
|
|
else ()
|
2023-09-04 21:43:28 +02:00
|
|
|
|
2020-02-16 01:21:27 +03:00
|
|
|
method greet =
|
2020-02-23 22:15:27 +03:00
|
|
|
(match !outfile with
|
2023-09-04 21:43:28 +02:00
|
|
|
| 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;
|
2023-09-04 21:43:28 +02:00
|
|
|
if !help then Printf.printf "%s" help_string
|
|
|
|
|
|
|
|
|
|
method get_debug = if !debug then "" else "-g"
|
|
|
|
|
method set_debug = debug := true
|
2019-11-24 02:30:32 +03:00
|
|
|
end
|
2020-10-03 11:49:43 +03:00
|
|
|
|
2023-09-04 21:43:28 +02:00
|
|
|
let[@ocaml.warning "-32"] main =
|
2020-10-03 11:49:43 +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;
|
2023-09-04 21:43:28 +02:00
|
|
|
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
|
2024-07-01 12:06:30 +02:00
|
|
|
| `Default | `Compile -> ignore @@ X86_64.build cmd prog
|
2023-09-04 21:43:28 +02:00
|
|
|
| `BC -> SM.ByteCode.compile cmd (SM.compile cmd prog)
|
2020-10-03 11:49:43 +03:00
|
|
|
| _ ->
|
2023-09-04 21:43:28 +02:00
|
|
|
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 cmd prog) input
|
|
|
|
|
in
|
|
|
|
|
List.iter (fun i -> Printf.printf "%d\n" i) output)
|
|
|
|
|
| `Fail er ->
|
|
|
|
|
Printf.eprintf "Error: %s\n" er;
|
|
|
|
|
exit 255
|
2020-02-16 01:21:27 +03:00
|
|
|
with
|
2023-09-04 21:43:28 +02:00
|
|
|
| Language.Semantic_error msg ->
|
|
|
|
|
Printf.printf "Error: %s\n" msg;
|
|
|
|
|
exit 255
|
|
|
|
|
| Commandline_error msg ->
|
|
|
|
|
Printf.printf "%s\n" msg;
|
|
|
|
|
exit 255
|