|
@@ -1,85 +1,103 @@
|
|
|
(** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
|
|
(** Type definitions for abstract syntax tree, assembly code, and exceptions. *)
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** [(filename, startline, endline, startcol, endcol)]
|
|
|
|
|
+ Location of node in source code. Used for error reporting. *)
|
|
|
type location = string * int * int * int * int
|
|
type location = string * int * int * int * int
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** All operators supported by CiviC. *)
|
|
|
type operator =
|
|
type operator =
|
|
|
| Neg | Not
|
|
| Neg | Not
|
|
|
| Add | Sub | Mul | Div | Mod
|
|
| Add | Sub | Mul | Div | Mod
|
|
|
| Eq | Ne | Lt | Le | Gt | Ge
|
|
| Eq | Ne | Lt | Le | Gt | Ge
|
|
|
| And | Or
|
|
| And | Or
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** Wrappers for constant values. This eliminates the need for separate
|
|
|
|
|
+ [(Bool|Int|Float)Const] nodes, and these values can be passed to {!instr}
|
|
|
|
|
+ constructors as well. *)
|
|
|
type const =
|
|
type const =
|
|
|
| BoolVal of bool
|
|
| BoolVal of bool
|
|
|
| IntVal of int
|
|
| IntVal of int
|
|
|
| FloatVal of float
|
|
| FloatVal of float
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** Data types supported by CiviC. [ArrayDims] defines an array type with a set
|
|
|
|
|
+ o dimensions. {!Dimreduce} replaces this multi-dimensional type with an
|
|
|
|
|
+ [Array] type, which signifies a one-dimensional array of a certain basic
|
|
|
|
|
+ type. *)
|
|
|
type ctype =
|
|
type ctype =
|
|
|
| Void | Bool | Int | Float | Array of ctype
|
|
| Void | Bool | Int | Float | Array of ctype
|
|
|
| ArrayDims of ctype * node list
|
|
| ArrayDims of ctype * node list
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** Annotations for {!node}. Each node has a list of zero or more annotations
|
|
|
|
|
+ that are gradually added through the compiler phases:
|
|
|
|
|
+
|
|
|
|
|
+ - [Loc] is a location added by the parser.
|
|
|
|
|
+ - [Depth] is the nesting level of a variable or function, and is added by
|
|
|
|
|
+ {!Context}.
|
|
|
|
|
+ - [Type] is a {!ctype} annotation added by {!Typecheck}.
|
|
|
|
|
+ - [Index] is the stack index of a variable declaration, added by {!Index}.
|
|
|
|
|
+ - [LabelName] is also added during index analysis, it holds the label name
|
|
|
|
|
+ of a [FunDef]. *)
|
|
|
and annotation =
|
|
and annotation =
|
|
|
| Loc of location
|
|
| Loc of location
|
|
|
| Depth of int
|
|
| Depth of int
|
|
|
- | Index of int
|
|
|
|
|
| Type of ctype
|
|
| Type of ctype
|
|
|
|
|
+ | Index of int
|
|
|
| LabelName of string
|
|
| LabelName of string
|
|
|
|
|
|
|
|
-(** Shorthand for annotation list, only to be used by {! Types.node} definitions
|
|
|
|
|
- below. *)
|
|
|
|
|
|
|
+(** Shorthand for annotation list used in definition of {!node}, should not be
|
|
|
|
|
+ used elsewhere. *)
|
|
|
and ann = annotation list
|
|
and ann = annotation list
|
|
|
|
|
|
|
|
-(** *)
|
|
|
|
|
|
|
+(** Abstract Syntax Tree nodes. *)
|
|
|
and node =
|
|
and node =
|
|
|
(* Global *)
|
|
(* Global *)
|
|
|
| Program of node list * ann
|
|
| Program of node list * ann
|
|
|
- (* list of declarations *)
|
|
|
|
|
|
|
+ (** list of declarations *)
|
|
|
| FunDec of ctype * string * node list * ann
|
|
| FunDec of ctype * string * node list * ann
|
|
|
- (* ret_type, name, params *)
|
|
|
|
|
|
|
+ (** ret_type, name, params *)
|
|
|
| FunDef of bool * ctype * string * node list * node * ann
|
|
| FunDef of bool * ctype * string * node list * node * ann
|
|
|
- (* export, ret_type, name, params, body *)
|
|
|
|
|
|
|
+ (** export, ret_type, name, params, body *)
|
|
|
| GlobalDec of ctype * string * ann
|
|
| GlobalDec of ctype * string * ann
|
|
|
- (* type, name *)
|
|
|
|
|
|
|
+ (** type, name *)
|
|
|
| GlobalDef of bool * ctype * string * node option * ann
|
|
| GlobalDef of bool * ctype * string * node option * ann
|
|
|
- (* export, type, name, initialisation? *)
|
|
|
|
|
|
|
+ (** export, type, name, initialisation *)
|
|
|
| Param of ctype * string * ann
|
|
| Param of ctype * string * ann
|
|
|
- (* type, name *)
|
|
|
|
|
|
|
+ (** type, name *)
|
|
|
| Dim of string * ann
|
|
| Dim of string * ann
|
|
|
- (* name *)
|
|
|
|
|
|
|
+ (** name *)
|
|
|
|
|
|
|
|
| VarDecs of node list
|
|
| VarDecs of node list
|
|
|
| LocalFuns of node list
|
|
| LocalFuns of node list
|
|
|
|
|
|
|
|
(* Statements *)
|
|
(* Statements *)
|
|
|
| VarDec of ctype * string * node option * ann
|
|
| VarDec of ctype * string * node option * ann
|
|
|
- (* type, name, initialisation? *)
|
|
|
|
|
|
|
+ (** type, name, initialisation *)
|
|
|
| Assign of string * node list option * node * ann
|
|
| Assign of string * node list option * node * ann
|
|
|
- (* name, indices?, value *)
|
|
|
|
|
|
|
+ (** name, indices, value *)
|
|
|
| For of string * node * node * node * node * ann
|
|
| For of string * node * node * node * node * ann
|
|
|
- (* counter, start, stop, step, body *)
|
|
|
|
|
|
|
+ (** counter, start, stop, step, body *)
|
|
|
| Allocate of node * node list * ann
|
|
| Allocate of node * node list * ann
|
|
|
- (* dec, dims # name = __allocate(dims) *)
|
|
|
|
|
- | Return of node * ann (* return <value>; *)
|
|
|
|
|
- | Expr of node (* <expr>; *)
|
|
|
|
|
- | Block of node list (* { <body> } *)
|
|
|
|
|
- | If of node * node * ann (* cond, body *)
|
|
|
|
|
- | IfElse of node * node * node * ann (* cond, true_body, false_body *)
|
|
|
|
|
- | While of node * node * ann (* cond, body *)
|
|
|
|
|
- | DoWhile of node * node * ann (* cond, body *)
|
|
|
|
|
|
|
+ (** declaration, dims *)
|
|
|
|
|
+ | Return of node * ann (** return value [return <value>;] *)
|
|
|
|
|
+ | Expr of node (** expression statement [<expr>;] *)
|
|
|
|
|
+ | Block of node list (** body [{ <body> }] *)
|
|
|
|
|
+ | If of node * node * ann (** condition, body [if (condition) { body }] *)
|
|
|
|
|
+ | IfElse of node * node * node * ann
|
|
|
|
|
+ (** condition, true_body, false_body [if (<condition>) { <true_body> } else { <false_body> }] *)
|
|
|
|
|
+ | While of node * node * ann
|
|
|
|
|
+ (** condition, body [while (<condition>) { <body> }] *)
|
|
|
|
|
+ | DoWhile of node * node * ann
|
|
|
|
|
+ (** condition, body [do { <body> } whlie (<condition>)] *)
|
|
|
|
|
|
|
|
(* Expressions *)
|
|
(* Expressions *)
|
|
|
- | Const of const * ann (* bool|int|float value *)
|
|
|
|
|
- | ArrayConst of node list * ann (* [<exprs>] *)
|
|
|
|
|
- | Var of string * node list option * ann (* <name> [<indices>]? *)
|
|
|
|
|
- | Monop of operator * node * ann (* op, operand *)
|
|
|
|
|
- | Binop of operator * node * node * ann (* op, left, right *)
|
|
|
|
|
- | TypeCast of ctype * node * ann (* (type) operand *)
|
|
|
|
|
- | FunCall of string * node list * ann (* name(args) *)
|
|
|
|
|
- | Arg of node (* function argument *)
|
|
|
|
|
|
|
+ | Const of const * ann (** constant [bool|int|float constant] *)
|
|
|
|
|
+ | ArrayConst of node list * ann (** array initialisation [[ <expr> , ... ]] *)
|
|
|
|
|
+ | Var of string * node list option * ann (** name, indices [<name> | <name>[<indices>] ] *)
|
|
|
|
|
+ | Monop of operator * node * ann (** operator, operand [<operator><operand>] *)
|
|
|
|
|
+ | Binop of operator * node * node * ann (** operator, left, right [<left> <operator> <right>] *)
|
|
|
|
|
+ | TypeCast of ctype * node * ann (** target_type, value [(target_type)value] *)
|
|
|
|
|
+ | FunCall of string * node list * ann (** name, args [<name>(<args>)] *)
|
|
|
|
|
+ | Arg of node (** function argument *)
|
|
|
|
|
|
|
|
(* Additional types for convenience in traversals
|
|
(* Additional types for convenience in traversals
|
|
|
* Mostly used to annotate existing nodes with information from declarations *)
|
|
* Mostly used to annotate existing nodes with information from declarations *)
|