type loc = string * int * int * int * int let noloc = ("", 0, 0, 0, 0) type operator = Neg | Not | Add | Sub | Mul | Div | Mod | Eq | Ne | Lt | Le | Gt | Ge | And | Or type ctype = Void | Bool | Int | Float | ArrayDec of ctype * node list | ArrayDef of ctype * node list | ArraySpec of ctype * int and node = (* global *) | Program of node list * loc | Param of ctype * string * loc | Dim of string * loc | FunDec of ctype * string * node list * loc | FunDef of bool * ctype * string * node list * node * loc | GlobalDec of ctype * string * loc | GlobalDef of bool * ctype * string * node option * loc (* statements *) | VarDec of ctype * string * node option * loc | Assign of string * node * loc | ArrayAssign of string * node list * node * loc | Return of node * loc | If of node * node * loc | IfElse of node * node * node * loc | While of node * node * loc | DoWhile of node * node * loc | For of string * node * node * node * node * loc | Allocate of string * node list * loc | Expr of node | Block of node list (* expressions *) | BoolConst of bool * loc | IntConst of int * loc | FloatConst of float * loc | ArrayConst of node list * loc | ArrayScalar of node * loc | Var of string * loc | Deref of string * node list * loc | Monop of operator * node * loc | Binop of operator * node * node * loc | Cond of node * node * node * loc | TypeCast of ctype * node * loc | FunCall of string * node list * loc | Arg of node (* additional types for convenience in traversals *) | VarLet of node * ctype * int | VarUse of node * ctype * int | FunUse of node * node * int | DimDec of node | Type of node * ctype | DummyNode (* container for command-line arguments *) type args = { mutable infile : string option; mutable outfile : string option; mutable verbose : int; mutable cpp : bool; } (* intermediate representations between phases *) type intermediate = | Args of args | FileContent of string * string * args | Ast of node * args | Assembly of string list * args (* exceptions *) exception LocError of loc * string exception NodeError of node * string exception CompileError of string exception EmptyError exception InvalidNode exception InvalidInput of string (* * Template for node matching follows below. * * let rec visit = function * | Program (decls, loc) -> * | Param (ctype, name, loc) -> * | FunDec (ret_type, name, params, loc) -> * | FunDef (export, ret_type, name, params, body, loc) -> * | GlobalDec (ctype, name, loc) -> * | GlobalDef (export, ctype, name, None, loc) -> * | GlobalDef (export, ctype, name, Some init, loc) -> * * | VarDec (ctype, name, None, loc) -> * | VarDec (ctype, name, Some init, loc) -> * | Assign (name, value, loc) -> * | Return (value, loc) -> * | If (cond, body, loc) -> * | IfElse (cond, true_body, false_body, loc) -> * | While (cond, body, loc) -> * | DoWhile (cond, body, loc) -> * | For (counter, start, stop, step, body, loc) -> * | Expr (value) -> * | Block (stats) -> * * | BoolConst (value, loc) -> * | IntConst (value, loc) -> * | FloatConst (value, loc) -> * | ArrayConst (dims, loc) -> * | ArrayScalar (value, loc) -> * | Var (name, loc) -> * | Deref (name, dims, loc) -> * | Monop (op, value, loc) -> * | Binop (op, left, right, loc) -> * | Cond (cond, true_expr, false_expr, loc) -> * | TypeCast (ctype, value, loc) -> * | FunCall (name, args, loc) -> * * | node -> transform visit node * *)