annotate 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
rev   line source
mike@0 1 (* lab4/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 This 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 mutable 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 val get_def : name -> def
mike@0 27
mike@0 28 (* abstract syntax *)
mike@0 29 type program = Prog of block * def list ref
mike@0 30
mike@0 31 and block = Block of decl list * stmt * int ref * int ref
mike@0 32
mike@0 33 and decl =
mike@0 34 ConstDecl of ident * expr
mike@0 35 | VarDecl of def_kind * ident list * typexpr
mike@0 36 | TypeDecl of (ident * typexpr) list
mike@0 37 | ProcDecl of proc_heading * block
mike@0 38 | PParamDecl of proc_heading
mike@0 39
mike@0 40 and proc_heading = Heading of name * decl list * typexpr option
mike@0 41
mike@0 42 and stmt =
mike@0 43 { s_guts: stmt_guts;
mike@0 44 s_line: int }
mike@0 45
mike@0 46 and stmt_guts =
mike@0 47 Skip
mike@0 48 | Seq of stmt list
mike@0 49 | Assign of expr * expr
mike@0 50 | ProcCall of name * expr list
mike@0 51 | Return of expr option
mike@0 52 | IfStmt of expr * stmt * stmt
mike@0 53 | WhileStmt of expr * stmt
mike@0 54 | RepeatStmt of stmt * expr
mike@0 55 | ForStmt of expr * expr * expr * stmt * def option ref
mike@0 56 | CaseStmt of expr * (expr * stmt) list * stmt
mike@0 57
mike@0 58 and expr =
mike@0 59 { e_guts: expr_guts;
mike@0 60 mutable e_type: ptype;
mike@0 61 mutable e_value: int option }
mike@0 62
mike@0 63 and expr_guts =
mike@0 64 Constant of int * ptype
mike@0 65 | Variable of name
mike@0 66 | Sub of expr * expr
mike@0 67 | Select of expr * name
mike@0 68 | Deref of expr
mike@0 69 | String of Optree.symbol * int
mike@0 70 | Nil
mike@0 71 | FuncCall of name * expr list
mike@0 72 | Monop of Optree.op * expr
mike@0 73 | Binop of Optree.op * expr * expr
mike@0 74
mike@0 75 and typexpr =
mike@0 76 TypeName of name
mike@0 77 | Array of expr * typexpr
mike@0 78 | Record of decl list
mike@0 79 | Pointer of typexpr
mike@0 80
mike@0 81 (* |makeExpr| -- construct an expr node with dummy annotations *)
mike@0 82 val makeExpr : expr_guts -> expr
mike@0 83
mike@0 84 (* |makeStmt| -- construct a stmt node *)
mike@0 85 val makeStmt : stmt_guts * int -> stmt
mike@0 86
mike@0 87 (* |makeName| -- construct a name node with dummy annotations *)
mike@0 88 val makeName : ident * int -> name
mike@0 89
mike@0 90 (* |makeBlock| -- construct a block node with dummy annotations *)
mike@0 91 val makeBlock : decl list * stmt -> block
mike@0 92
mike@0 93 (* |print_tree| -- pretty-print a tree *)
mike@0 94 val print_tree : out_channel -> string -> program -> unit