-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmd.ml
89 lines (72 loc) · 2.74 KB
/
Cmd.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(* Copyright Per Lindgren 2016-2018, see the file "LICENSE" *)
(* for the full license governing this code. *)
open Common
open Options
let usage =
"The cimp v1.0 compiler, Per Lindgren (c) 2018" ^ nl ^ "Options summary:"
let o_verbose = ref false
let o_debug = ref false
let f_infile = ref ""
let f_outfile = ref ""
let d_ast = ref false
let d_past = ref false
let d_code = ref false
let d_pcode = ref false
let imp_ex = ref false
let vm_ex = ref false
let reg = ref false
let opt_ast = ref false
let speclist =
[
("-i", Arg.Set_string f_infile, "\t\t: infile (-i optional)");
("-o", Arg.Set_string f_outfile, "\t\t: outfile (default infile.s)");
("-v", Arg.Set o_verbose, "\t\t: verbose mode (default disable)");
("-D", Arg.Set o_debug, "\t\t: debug mode (default disable)");
("-d_ast", Arg.Set d_ast, "\t: dump AST");
("-d_past", Arg.Set d_past, "\t: dump pretty AST");
("-d_code", Arg.Set d_code, "\t: dump code");
("-d_pcode", Arg.Set d_pcode, "\t: dump pretty code");
("-imp_ex", Arg.Set imp_ex, "\t: imp_ex evaluation");
("-vm_ex", Arg.Set vm_ex, "\t: vm_ex virtual machine execution");
("-reg", Arg.Set reg, "\t: compile with registers");
("-O", Arg.Set opt_ast, "\t: performs ast optimizations before compiling");
]
(* check if e is a file extension of s *)
let ext s e =
let le = String.length e in
let ls = String.length s in
try
compare (String.sub s (ls - le) le) e == 0
with
_ -> false
(* replace file extension e with r *)
let rep_ext s e r =
let le = String.length e in
let ls = String.length s in
String.sub s 0 (ls - le) ^ r
let cmd =
(* Read the arguments *)
Arg.parse speclist (fun x -> f_infile := x) usage;
try
(* infile *)
if (String.compare !f_infile "" == 0) then raise (Arg.Bad "No infile selected");
if not (ext !f_infile ".imp") then
raise (Arg.Bad "Bad infile extention (.imp/.mlw exptected)");
opt.infile <- !f_infile;
(* outfile *)
opt.outfile <- if (String.compare (!f_outfile) "" == 0) then rep_ext (!f_infile) ".xxx" ".s" else !f_outfile;
if (not (ext opt.outfile ".s")) then raise (Arg.Bad("Bad outfile extention (.s exptected) " ^ opt.outfile));
p_stderr ("outfile :" ^ opt.outfile);
(* general options *)
opt.debug <- ! o_debug;
opt.verbose <- ! o_verbose;
opt.d_ast <- ! d_ast;
opt.d_past <- ! d_past;
opt.d_code <- ! d_code;
opt.d_pcode <- ! d_pcode;
opt.imp_ex <- ! imp_ex;
opt.vm_ex <- ! vm_ex;
opt.reg <- ! reg;
opt.opt_ast <- ! opt_ast;
with
| Arg.Bad msg -> p_stderr ("Command line error: " ^ msg); exit (-1);