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