This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ml
66 lines (56 loc) · 1.67 KB
/
main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(* Programa principal *)
open Format
open Lexing
open Parser
let usage = "usage: natrix [options] file.nx"
let parse_only = ref false
let compiler_only = ref false
let interpreter_only = ref false
let ofile = ref "compiled.s"
let set_file f s = f := s
let spec =
[
"--parse-only", Arg.Set parse_only, " stop after parsing";
"--compiler-only", Arg.Set compiler_only, " only generate compiler";
"--interpreter-only", Arg.Set interpreter_only, " only interpreter";
]
let file =
let file = ref None in
let set_file s =
if not (Filename.check_suffix s ".nx") then
raise (Arg.Bad "no .nx extension");
file := Some s
in
Arg.parse spec set_file usage;
match !file with Some f -> f | None -> Arg.usage spec usage; exit 1
let report (b,e) =
let l = b.pos_lnum in
let fc = b.pos_cnum - b.pos_bol + 1 in
let lc = e.pos_cnum - b.pos_bol + 1 in
eprintf "File \"%s\", line %d, characters %d-%d:\n" file l fc lc
let () =
let c = open_in file in
let lb = Lexing.from_channel c in (* lb = Lexing buffer *)
try
let f = Parser.program Lexer.next_token lb in
close_in c;
if !parse_only then exit 0;
if !interpreter_only then (Interp.program f; exit 0);
if !compiler_only then (Compile.compile_program f !ofile; exit 0);
Interp.program f;
Compile.compile_program f !ofile
with
| Lexer.Lexing_error s ->
report (lexeme_start_p lb, lexeme_end_p lb);
eprintf "lexical error: %s@." s;
exit 1
| Parser.Error ->
report (lexeme_start_p lb, lexeme_end_p lb);
eprintf "syntax error@.";
exit 1
| Interp.Error s ->
eprintf "error: %s@." s;
exit 1
| e ->
eprintf "Anomaly: %s\n@." (Printexc.to_string e);
exit 2