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