|
@@ -1,3 +1,5 @@
|
|
|
|
|
+type loc = string * int * int * int * int
|
|
|
|
|
+
|
|
|
type monop = Neg | Not
|
|
type monop = Neg | Not
|
|
|
type binop = Add | Sub | Mul | Div | Mod
|
|
type binop = Add | Sub | Mul | Div | Mod
|
|
|
| Eq | Ne | Lt | Le | Gt | Ge
|
|
| Eq | Ne | Lt | Le | Gt | Ge
|
|
@@ -6,44 +8,49 @@ type ctype = Void | Bool | Int | Float
|
|
|
| ArrayDec of ctype * string list
|
|
| ArrayDec of ctype * string list
|
|
|
| ArrayDef of ctype * node list
|
|
| ArrayDef of ctype * node list
|
|
|
and node =
|
|
and node =
|
|
|
- (* Global *)
|
|
|
|
|
- | Program of node list
|
|
|
|
|
- | Param of ctype * string
|
|
|
|
|
- | FunDec of ctype * string * node list
|
|
|
|
|
- | FunDef of bool * ctype * string * node list * node list
|
|
|
|
|
- | GlobalDec of ctype * string
|
|
|
|
|
- | GlobalDef of bool * ctype * string * node option
|
|
|
|
|
|
|
+ (* global *)
|
|
|
|
|
+ | Program of node list * loc
|
|
|
|
|
+ | Param of ctype * string * loc
|
|
|
|
|
+ | FunDec of ctype * string * node list * loc
|
|
|
|
|
+ | FunDef of bool * ctype * string * node list * node list * loc
|
|
|
|
|
+ | GlobalDec of ctype * string * loc
|
|
|
|
|
+ | GlobalDef of bool * ctype * string * node option * loc
|
|
|
|
|
|
|
|
- (* Statements *)
|
|
|
|
|
- | VarDec of ctype * string * node option
|
|
|
|
|
- | Assign of string * node
|
|
|
|
|
- | Return of node
|
|
|
|
|
- | If of node * node list
|
|
|
|
|
- | IfElse of node * node list * node list
|
|
|
|
|
- | While of node * node list
|
|
|
|
|
- | DoWhile of node * node list
|
|
|
|
|
- | For of string * node * node * node * node list
|
|
|
|
|
- | Expr of node
|
|
|
|
|
|
|
+ (* statements *)
|
|
|
|
|
+ | VarDec of ctype * string * node option * loc
|
|
|
|
|
+ | Assign of string * node * loc
|
|
|
|
|
+ | Return of node * loc
|
|
|
|
|
+ | If of node * node list * loc
|
|
|
|
|
+ | IfElse of node * node list * node list * loc
|
|
|
|
|
+ | While of node * node list * loc
|
|
|
|
|
+ | DoWhile of node * node list * loc
|
|
|
|
|
+ | For of string * node * node * node * node list * loc
|
|
|
|
|
+ | Allocate of string * node list * loc
|
|
|
|
|
+ | Expr of node * loc
|
|
|
|
|
+ | Statements of node list * loc
|
|
|
|
|
|
|
|
- (* Expressions *)
|
|
|
|
|
- | BoolConst of bool
|
|
|
|
|
- | IntConst of int
|
|
|
|
|
- | FloatConst of float
|
|
|
|
|
- | ArrayConst of node list
|
|
|
|
|
- | Var of string
|
|
|
|
|
- | Deref of string * node list
|
|
|
|
|
- | Monop of monop * node
|
|
|
|
|
- | Binop of binop * node * node
|
|
|
|
|
- | Cond of node * node * node
|
|
|
|
|
- | TypeCast of ctype * node
|
|
|
|
|
- | FunCall of string * 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 monop * node * loc
|
|
|
|
|
+ | Binop of binop * node * node * loc
|
|
|
|
|
+ | Cond of node * node * node * loc
|
|
|
|
|
+ | TypeCast of ctype * node * loc
|
|
|
|
|
+ | FunCall of string * node list * loc
|
|
|
|
|
|
|
|
-(* Intermediate representations between phases *)
|
|
|
|
|
|
|
+(* intermediate representations between phases *)
|
|
|
type repr =
|
|
type repr =
|
|
|
| Inputfile of string option * int (* filename, verbose *)
|
|
| Inputfile of string option * int (* filename, verbose *)
|
|
|
| Node of node * int (* ast, verbose *)
|
|
| Node of node * int (* ast, verbose *)
|
|
|
| Assembly of string list * int (* instructions *)
|
|
| Assembly of string list * int (* instructions *)
|
|
|
|
|
|
|
|
|
|
+exception LocError of string * loc
|
|
|
|
|
+
|
|
|
exception CompileError of string
|
|
exception CompileError of string
|
|
|
|
|
|
|
|
|
|
|
|
@@ -51,34 +58,36 @@ exception CompileError of string
|
|
|
* Template for node matching follows below.
|
|
* Template for node matching follows below.
|
|
|
*
|
|
*
|
|
|
* let rec visit = function
|
|
* let rec visit = function
|
|
|
- * | Program (decls) ->
|
|
|
|
|
- * | Param (ctype, name) ->
|
|
|
|
|
- * | FunDec (ret_type, name, params) ->
|
|
|
|
|
- * | FunDef (export, ret_type, name, params, body) ->
|
|
|
|
|
- * | GlobalDec (ctype, name) ->
|
|
|
|
|
- * | GlobalDef (export, ctype, name, None) ->
|
|
|
|
|
- * | GlobalDef (export, ctype, name, Some init) ->
|
|
|
|
|
|
|
+ * | 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, loc) ->
|
|
|
*
|
|
*
|
|
|
- * | VarDec (ctype, name, None) ->
|
|
|
|
|
- * | VarDec (ctype, name, Some init) ->
|
|
|
|
|
- * | Assign (name, value) ->
|
|
|
|
|
- * | Return (value) ->
|
|
|
|
|
- * | If (cond, body) ->
|
|
|
|
|
- * | IfElse (cond, true_body, false_body) ->
|
|
|
|
|
- * | While (cond, body) ->
|
|
|
|
|
- * | DoWhile (cond, body) ->
|
|
|
|
|
- * | For (counter, start, stop, step, body) ->
|
|
|
|
|
- * | Expr (value) ->
|
|
|
|
|
|
|
+ * | BoolConst (value, loc) ->
|
|
|
|
|
+ * | IntConst (value, loc) ->
|
|
|
|
|
+ * | FloatConst (value, loc) ->
|
|
|
|
|
+ * | Var (name, 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) ->
|
|
|
*
|
|
*
|
|
|
- * | BoolConst (value) ->
|
|
|
|
|
- * | IntConst (value) ->
|
|
|
|
|
- * | FloatConst (value) ->
|
|
|
|
|
- * | Var (name) ->
|
|
|
|
|
- * | Monop (op, value) ->
|
|
|
|
|
- * | Binop (op, left, right) ->
|
|
|
|
|
- * | Cond (cond, true_expr, false_expr) ->
|
|
|
|
|
- * | TypeCast (ctype, value) ->
|
|
|
|
|
- * | FunCall (name, args) ->
|
|
|
|
|
|
|
+ * | Statements (stats, loc) ->
|
|
|
*
|
|
*
|
|
|
* | node -> transform visit node
|
|
* | node -> transform visit node
|
|
|
*
|
|
*
|