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