annotate tools/ncmain.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 (* ncmain.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 (* The 'nodexp' program is a preprocessor that adds one new notation to
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 OCaml. An expression like <LOAD addr_size, <LOCAL stat_link>> is shorthand
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 for Node (LOAD addr_size, [Node (LOCAL stat_link, [])]) -- a bit easier to
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 read, but a lot easier to type. You can also write, e.g.,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 <SEQ, @(List.map gen_stmt ss)> as shorthand for the expression
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 Node (SEQ, List.map gen_stmt ss), which is no longer, but at least gives
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 a consistent look to the program.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 A program that's written in this notation ought to contain the type
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 declaration
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 type optree = Node of inst * optree list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 to make the expressions properly typed. The <...> expressions can be
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 used both as patterns and as proper expressions. Ambiguity with the use of
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 < and > as comparison operators is removed by the rule that the construct
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 must begin <ident with no space. *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 open Print
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 let qflag = ref false
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 let spec = Arg.align ["-q", Arg.Unit (fun () -> qflag := true),
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 " disable #line directives"]
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 let main () =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30 let fname = ref "standard input" in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 let chan = ref stdin in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 let seen = ref false in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 let open_it fn =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 if not !seen then begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 fname := fn; chan := open_in fn; seen := true
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 end in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 Arg.parse spec open_it "Usage: nodexp [-q] file";
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 let lexbuf = Lexing.from_channel !chan in
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39 begin
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 if not !qflag then printf "# 1 \"$\"\n" [fStr !fname];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 try Ncparse.text Nclex.token lexbuf with
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 Parsing.Parse_error ->
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 fprintf stderr "\"$\", line $: syntax error\n"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 [fStr !fname; fNum !Nclex.line_no];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 exit 1
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 end;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 exit 0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 let nodexp = main ()