annotate lab3/main.ml @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 (* lab3/main.ml *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 (* Copyright (c) 2017 J. M. Spivey *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 open Print
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 open Source
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 (* |main| -- main program *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 let main () =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 let dflag = ref false in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 let fns = ref [] in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 let usage = "Usage: ppc [-d] file.p" in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 Arg.parse [("-d", Arg.Set dflag, " Print the tree");
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 ("-O", Arg.Unit (fun () -> Kgen.optflag := true), " Peephole optimiser")]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 (function s -> fns := !fns @ [s]) usage;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 if List.length !fns <> 1 then begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 fprintf stderr "$\n" [fStr usage]; exit 2
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 let in_file = List.hd !fns in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 let in_chan = open_in in_file in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 Source.init in_file in_chan;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 let lexbuf = Lexing.from_channel in_chan in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 let prog = try Parser.program Lexer.token lexbuf with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 Parsing.Parse_error ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 let tok = Lexing.lexeme lexbuf in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 err_message "syntax error at token '$'"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 [fStr tok] !Lexer.lineno;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 exit 1 in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 if !dflag then Tree.print_tree stdout prog;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 begin try Check.annotate prog with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 Check.Semantic_error (fmt, args, line) ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 err_message fmt args line;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 exit 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 printf "MODULE Main 0 0\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 printf "IMPORT Lib 0\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 printf "ENDHDR\n\n" [];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 Kgen.translate prog;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 exit 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 let ppc = main ()