Mercurial > hg > compilers
comparison lab2/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 (* lab2/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 The 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 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 | |
27 (* Abstract syntax *) | |
28 | |
29 type program = Program of decl list * stmt | |
30 | |
31 and decl = Decl of name list * ptype | |
32 | |
33 and stmt = | |
34 Skip | |
35 | Seq of stmt list | |
36 | Assign of expr * expr | |
37 | Print of expr | |
38 | Newline | |
39 | IfStmt of expr * stmt * stmt | |
40 | WhileStmt of expr * stmt | |
41 | |
42 and expr = | |
43 { e_guts: expr_guts; | |
44 mutable e_type: ptype } | |
45 | |
46 and expr_guts = | |
47 Constant of int * ptype | |
48 | Variable of name | |
49 | Sub of expr * expr | |
50 | Monop of Keiko.op * expr | |
51 | Binop of Keiko.op * expr * expr | |
52 | |
53 | |
54 (* seq -- neatly join a list of statements into a sequence *) | |
55 val seq : stmt list -> stmt | |
56 | |
57 (* |makeName| -- construct a name node with dummy annotations *) | |
58 val makeName : ident -> int -> name | |
59 | |
60 (* |get_def| -- rerieve definition from name *) | |
61 val get_def : name -> def | |
62 | |
63 (* |makeExpr| -- construct an expr node with dummy annotations *) | |
64 val makeExpr : expr_guts -> expr | |
65 | |
66 (* |print_tree| -- pretty-print a tree *) | |
67 val print_tree : out_channel -> program -> unit |