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