comparison lab4/main.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* lab4/main.ml *)
2 (* Copyright (c) 2017 J. M. Spivey *)
3
4 open Print
5 open Mach
6 open Source
7
8 let debug = ref 0
9
10 let usage = "Usage: ppc0 [-v] [-t] file"
11
12 let spec =
13 Arg.align
14 ["-b", Arg.Unit (fun () -> Tgen.boundchk := true),
15 " enable bound checks";
16 "-d", Arg.Int (function x -> debug := x), "n set debug level";
17 "-O", Arg.Unit (fun () -> Tgen.optlevel := 1),
18 " enable optimiser (simplifier, jump opt)";
19 "-O2", Arg.Unit (fun () -> Tgen.optlevel := 2),
20 " more optimisation (common subexpressions)";
21 "-noregvars", Arg.Unit (fun () -> Check.regvars := false),
22 " disable register variables"]
23
24 let main () =
25 let fns = ref [] in
26 Arg.parse spec (function s -> fns := !fns @ [s]) usage;
27 if List.length !fns <> 1 then begin
28 fprintf stderr "$\n" [fStr usage]; exit 2
29 end;
30 let in_file = List.hd !fns in
31 let in_chan = open_in in_file in
32 let lexbuf = Lexing.from_channel in_chan in
33 Source.init in_file in_chan;
34 Tgen.debug := !debug;
35 Tran.debug := !debug;
36 ignore (Parsing.set_trace (!debug > 2));
37
38 (* Parse the program *)
39 let prog =
40 try Parser.program Lexer.token lexbuf with
41 Parsing.Parse_error ->
42 let tok = Lexing.lexeme lexbuf in
43 Source.err_message "syntax error at token '$'" [fStr tok] !Lexer.lineno;
44 exit 1 in
45
46 if !debug > 2 then begin
47 printf "$Abstract syntax tree:\n" [fStr Mach.comment];
48 Tree.print_tree stdout Mach.comment prog;
49 printf "\n" []
50 end;
51
52 (* Semantic analysis *)
53 begin try Check.annotate prog with
54 Check.Sem_error (fmt, args, ln) ->
55 Source.err_message fmt args ln;
56 exit 1
57 end;
58
59 (* Translate the program *)
60 Tgen.translate prog;
61 exit 0
62
63 let ppc = main ()