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
mike@0 1 (* lab2/tree.mli *)
mike@0 2 (* Copyright (c) 2017 J. M. Spivey *)
mike@0 3
mike@0 4 open Dict
mike@0 5
mike@0 6 (*
mike@0 7 This module describes the type of abstract syntax trees that is used
mike@0 8 as the main interface between parts of the comipler. A tree is built
mike@0 9 by the parser, then checked by the semantic analyser, which annotates
mike@0 10 identifiers in the tree with their definitions. The intermediate code
mike@0 11 generator finally traverses the tree, emitting code for each
mike@0 12 expression or statement.
mike@0 13
mike@0 14 The module also contains some functions that are used to build the
mike@0 15 tree initially; they construct nodes with default values for the
mike@0 16 annotations. Proper values are filled in later during semantic
mike@0 17 analysis.
mike@0 18 *)
mike@0 19
mike@0 20 (* |name| -- type for applied occurrences with annotations *)
mike@0 21 type name =
mike@0 22 { x_name: ident; (* Name of the reference *)
mike@0 23 x_line: int; (* Line number *)
mike@0 24 mutable x_def: def option } (* Definition in scope *)
mike@0 25
mike@0 26
mike@0 27 (* Abstract syntax *)
mike@0 28
mike@0 29 type program = Program of decl list * stmt
mike@0 30
mike@0 31 and decl = Decl of name list * ptype
mike@0 32
mike@0 33 and stmt =
mike@0 34 Skip
mike@0 35 | Seq of stmt list
mike@0 36 | Assign of expr * expr
mike@0 37 | Print of expr
mike@0 38 | Newline
mike@0 39 | IfStmt of expr * stmt * stmt
mike@0 40 | WhileStmt of expr * stmt
mike@0 41
mike@0 42 and expr =
mike@0 43 { e_guts: expr_guts;
mike@0 44 mutable e_type: ptype }
mike@0 45
mike@0 46 and expr_guts =
mike@0 47 Constant of int * ptype
mike@0 48 | Variable of name
mike@0 49 | Sub of expr * expr
mike@0 50 | Monop of Keiko.op * expr
mike@0 51 | Binop of Keiko.op * expr * expr
mike@0 52
mike@0 53
mike@0 54 (* seq -- neatly join a list of statements into a sequence *)
mike@0 55 val seq : stmt list -> stmt
mike@0 56
mike@0 57 (* |makeName| -- construct a name node with dummy annotations *)
mike@0 58 val makeName : ident -> int -> name
mike@0 59
mike@0 60 (* |get_def| -- rerieve definition from name *)
mike@0 61 val get_def : name -> def
mike@0 62
mike@0 63 (* |makeExpr| -- construct an expr node with dummy annotations *)
mike@0 64 val makeExpr : expr_guts -> expr
mike@0 65
mike@0 66 (* |print_tree| -- pretty-print a tree *)
mike@0 67 val print_tree : out_channel -> program -> unit