annotate lab4/tree.mli @ 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 (* lab4/tree.mli *)
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 Dict
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 (*
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 This module describes the type of abstract syntax trees that is used
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 as the main interface between parts of the comipler. A tree is built
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 by the parser, then checked by the semantic analyser, which annotates
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 identifiers in the tree with their definitions. The intermediate code
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 generator finally traverses the tree, emitting code for each
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 expression or statement.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 This module also contains some functions that are used to build the
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 tree initially; they construct nodes with default values for the
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 annotations. Proper values are filled in later during semantic
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 analysis.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 (* |name| -- type for applied occurrences, with mutable annotations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 type name =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 { x_name: ident; (* Name of the reference *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 x_line: int; (* Line number *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 mutable x_def: def option } (* Definition in scope *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 val get_def : name -> def
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 (* abstract syntax *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 type program = Prog of block * def list ref
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 and block = Block of decl list * stmt * int ref * int ref
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 and decl =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 ConstDecl of ident * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 | VarDecl of def_kind * ident list * typexpr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 | TypeDecl of (ident * typexpr) list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 | ProcDecl of proc_heading * block
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 | PParamDecl of proc_heading
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 and proc_heading = Heading of name * decl list * typexpr option
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 and stmt =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 { s_guts: stmt_guts;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 s_line: int }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 and stmt_guts =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 Skip
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 | Seq of stmt list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 | Assign of expr * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 | ProcCall of name * expr list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 | Return of expr option
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 | IfStmt of expr * stmt * stmt
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 | WhileStmt of expr * stmt
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54 | RepeatStmt of stmt * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 | ForStmt of expr * expr * expr * stmt * def option ref
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 | CaseStmt of expr * (expr * stmt) list * stmt
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58 and expr =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 { e_guts: expr_guts;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60 mutable e_type: ptype;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 mutable e_value: int option }
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 and expr_guts =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 Constant of int * ptype
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 | Variable of name
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66 | Sub of expr * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 | Select of expr * name
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68 | Deref of expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 | String of Optree.symbol * int
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70 | Nil
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 | FuncCall of name * expr list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 | Monop of Optree.op * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 | Binop of Optree.op * expr * expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 and typexpr =
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 TypeName of name
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 | Array of expr * typexpr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 | Record of decl list
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 | Pointer of typexpr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 (* |makeExpr| -- construct an expr node with dummy annotations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 val makeExpr : expr_guts -> expr
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84 (* |makeStmt| -- construct a stmt node *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 val makeStmt : stmt_guts * int -> stmt
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 (* |makeName| -- construct a name node with dummy annotations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 val makeName : ident * int -> name
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 (* |makeBlock| -- construct a block node with dummy annotations *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 val makeBlock : decl list * stmt -> block
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 (* |print_tree| -- pretty-print a tree *)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 val print_tree : out_channel -> string -> program -> unit