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