annotate lab2/main.ml @ 1:b5139af1a420 tip basis

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