|
@@ -44,18 +44,18 @@ let loc_from_lexpos pstart pend =
|
|
|
let rec flatten_blocks lst =
|
|
let rec flatten_blocks lst =
|
|
|
let flatten = flatten_blocks in
|
|
let flatten = flatten_blocks in
|
|
|
let trav = function
|
|
let trav = function
|
|
|
- | FunDef (export, ret_type, name, params, Block body, loc) ->
|
|
|
|
|
- FunDef (export, ret_type, name, flatten params, Block (flatten body), loc)
|
|
|
|
|
- | If (cond, Block body, loc) ->
|
|
|
|
|
- If (cond, Block (flatten body), loc)
|
|
|
|
|
- | IfElse (cond, Block tbody, Block fbody, loc) ->
|
|
|
|
|
- IfElse (cond, Block (flatten tbody), Block (flatten fbody), loc)
|
|
|
|
|
- | While (cond, Block body, loc) ->
|
|
|
|
|
- While (cond, Block (flatten body), loc)
|
|
|
|
|
- | DoWhile (cond, Block body, loc) ->
|
|
|
|
|
- DoWhile (cond, Block (flatten body), loc)
|
|
|
|
|
- | For (counter, start, stop, step, Block body, loc) ->
|
|
|
|
|
- For (counter, start, stop, step, Block (flatten body), loc)
|
|
|
|
|
|
|
+ | FunDef (export, ret_type, name, params, Block body, ann) ->
|
|
|
|
|
+ FunDef (export, ret_type, name, flatten params, Block (flatten body), ann)
|
|
|
|
|
+ | If (cond, Block body, ann) ->
|
|
|
|
|
+ If (cond, Block (flatten body), ann)
|
|
|
|
|
+ | IfElse (cond, Block tbody, Block fbody, ann) ->
|
|
|
|
|
+ IfElse (cond, Block (flatten tbody), Block (flatten fbody), ann)
|
|
|
|
|
+ | While (cond, Block body, ann) ->
|
|
|
|
|
+ While (cond, Block (flatten body), ann)
|
|
|
|
|
+ | DoWhile (cond, Block body, ann) ->
|
|
|
|
|
+ DoWhile (cond, Block (flatten body), ann)
|
|
|
|
|
+ | For (counter, start, stop, step, Block body, ann) ->
|
|
|
|
|
+ For (counter, start, stop, step, Block (flatten body), ann)
|
|
|
| node -> node
|
|
| node -> node
|
|
|
in
|
|
in
|
|
|
match lst with
|
|
match lst with
|
|
@@ -69,56 +69,56 @@ let rec flatten_blocks lst =
|
|
|
let transform_children trav node =
|
|
let transform_children trav node =
|
|
|
let trav_all nodes = List.map trav nodes in
|
|
let trav_all nodes = List.map trav nodes in
|
|
|
match node with
|
|
match node with
|
|
|
- | Program (decls, loc) ->
|
|
|
|
|
- Program (flatten_blocks (trav_all decls), loc)
|
|
|
|
|
- | FunDec (ret_type, name, params, loc) ->
|
|
|
|
|
- FunDec (ret_type, name, trav_all params, loc)
|
|
|
|
|
- | FunDef (export, ret_type, name, params, body, loc) ->
|
|
|
|
|
- FunDef (export, ret_type, name, trav_all params, trav body, loc)
|
|
|
|
|
- | GlobalDec (ctype, name, loc) ->
|
|
|
|
|
- GlobalDec (ctype, name, loc)
|
|
|
|
|
- | GlobalDef (export, ctype, name, Some init, loc) ->
|
|
|
|
|
- GlobalDef (export, ctype, name, Some (trav init), loc)
|
|
|
|
|
-
|
|
|
|
|
- | VarDec (ctype, name, Some init, loc) ->
|
|
|
|
|
- VarDec (ctype, name, Some (trav init), loc)
|
|
|
|
|
- | Assign (name, None, value, loc) ->
|
|
|
|
|
- Assign (name, None, trav value, loc)
|
|
|
|
|
- | Assign (name, Some dims, value, loc) ->
|
|
|
|
|
- Assign (name, Some (trav_all dims), trav value, loc)
|
|
|
|
|
- | VarLet (dec, None, value, loc) ->
|
|
|
|
|
- VarLet (dec, None, trav value, loc)
|
|
|
|
|
- | VarLet (dec, Some dims, value, loc) ->
|
|
|
|
|
- VarLet (dec, Some (trav_all dims), trav value, loc)
|
|
|
|
|
- | Return (value, loc) ->
|
|
|
|
|
- Return (trav value, loc)
|
|
|
|
|
- | If (cond, body, loc) ->
|
|
|
|
|
- If (trav cond, trav body, loc)
|
|
|
|
|
- | IfElse (cond, true_body, false_body, loc) ->
|
|
|
|
|
- IfElse (trav cond, trav true_body, trav false_body, loc)
|
|
|
|
|
- | While (cond, body, loc) ->
|
|
|
|
|
- While (trav cond, trav body, loc)
|
|
|
|
|
- | DoWhile (cond, body, loc) ->
|
|
|
|
|
- DoWhile (trav cond, trav body, loc)
|
|
|
|
|
- | For (counter, start, stop, step, body, loc) ->
|
|
|
|
|
- For (counter, trav start, trav stop, trav step, trav body, loc)
|
|
|
|
|
- | Allocate (name, dims, dec, loc) ->
|
|
|
|
|
- Allocate (name, trav_all dims, dec, loc)
|
|
|
|
|
|
|
+ | Program (decls, ann) ->
|
|
|
|
|
+ Program (flatten_blocks (trav_all decls), ann)
|
|
|
|
|
+ | FunDec (ret_type, name, params, ann) ->
|
|
|
|
|
+ FunDec (ret_type, name, trav_all params, ann)
|
|
|
|
|
+ | FunDef (export, ret_type, name, params, body, ann) ->
|
|
|
|
|
+ FunDef (export, ret_type, name, trav_all params, trav body, ann)
|
|
|
|
|
+ | GlobalDec (ctype, name, ann) ->
|
|
|
|
|
+ GlobalDec (ctype, name, ann)
|
|
|
|
|
+ | GlobalDef (export, ctype, name, Some init, ann) ->
|
|
|
|
|
+ GlobalDef (export, ctype, name, Some (trav init), ann)
|
|
|
|
|
+
|
|
|
|
|
+ | VarDec (ctype, name, Some init, ann) ->
|
|
|
|
|
+ VarDec (ctype, name, Some (trav init), ann)
|
|
|
|
|
+ | Assign (name, None, value, ann) ->
|
|
|
|
|
+ Assign (name, None, trav value, ann)
|
|
|
|
|
+ | Assign (name, Some dims, value, ann) ->
|
|
|
|
|
+ Assign (name, Some (trav_all dims), trav value, ann)
|
|
|
|
|
+ | VarLet (dec, None, value, ann) ->
|
|
|
|
|
+ VarLet (dec, None, trav value, ann)
|
|
|
|
|
+ | VarLet (dec, Some dims, value, ann) ->
|
|
|
|
|
+ VarLet (dec, Some (trav_all dims), trav value, ann)
|
|
|
|
|
+ | Return (value, ann) ->
|
|
|
|
|
+ Return (trav value, ann)
|
|
|
|
|
+ | If (cond, body, ann) ->
|
|
|
|
|
+ If (trav cond, trav body, ann)
|
|
|
|
|
+ | IfElse (cond, true_body, false_body, ann) ->
|
|
|
|
|
+ IfElse (trav cond, trav true_body, trav false_body, ann)
|
|
|
|
|
+ | While (cond, body, ann) ->
|
|
|
|
|
+ While (trav cond, trav body, ann)
|
|
|
|
|
+ | DoWhile (cond, body, ann) ->
|
|
|
|
|
+ DoWhile (trav cond, trav body, ann)
|
|
|
|
|
+ | For (counter, start, stop, step, body, ann) ->
|
|
|
|
|
+ For (counter, trav start, trav stop, trav step, trav body, ann)
|
|
|
|
|
+ | Allocate (name, dims, dec, ann) ->
|
|
|
|
|
+ Allocate (name, trav_all dims, dec, ann)
|
|
|
| Expr value ->
|
|
| Expr value ->
|
|
|
Expr (trav value)
|
|
Expr (trav value)
|
|
|
| Block (body) ->
|
|
| Block (body) ->
|
|
|
Block (trav_all body)
|
|
Block (trav_all body)
|
|
|
|
|
|
|
|
- | Monop (op, value, loc) ->
|
|
|
|
|
- Monop (op, trav value, loc)
|
|
|
|
|
- | Binop (op, left, right, loc) ->
|
|
|
|
|
- Binop (op, trav left, trav right, loc)
|
|
|
|
|
- | Cond (cond, true_expr, false_expr, loc) ->
|
|
|
|
|
- Cond (trav cond, trav true_expr, trav false_expr, loc)
|
|
|
|
|
- | TypeCast (ctype, value, loc) ->
|
|
|
|
|
- TypeCast (ctype, trav value, loc)
|
|
|
|
|
- | FunCall (name, args, loc) ->
|
|
|
|
|
- FunCall (name, trav_all args, loc)
|
|
|
|
|
|
|
+ | Monop (op, value, ann) ->
|
|
|
|
|
+ Monop (op, trav value, ann)
|
|
|
|
|
+ | Binop (op, left, right, ann) ->
|
|
|
|
|
+ Binop (op, trav left, trav right, ann)
|
|
|
|
|
+ | Cond (cond, true_expr, false_expr, ann) ->
|
|
|
|
|
+ Cond (trav cond, trav true_expr, trav false_expr, ann)
|
|
|
|
|
+ | TypeCast (ctype, value, ann) ->
|
|
|
|
|
+ TypeCast (ctype, trav value, ann)
|
|
|
|
|
+ | FunCall (name, args, ann) ->
|
|
|
|
|
+ FunCall (name, trav_all args, ann)
|
|
|
| Arg value ->
|
|
| Arg value ->
|
|
|
Arg (trav value)
|
|
Arg (trav value)
|
|
|
|
|
|
|
@@ -133,9 +133,78 @@ let transform_children trav node =
|
|
|
| FunUse (dec, params, ann) ->
|
|
| FunUse (dec, params, ann) ->
|
|
|
FunUse (dec, trav_all params, ann)
|
|
FunUse (dec, trav_all params, ann)
|
|
|
|
|
|
|
|
|
|
+ | VarDecs decs ->
|
|
|
|
|
+ VarDecs (trav_all decs)
|
|
|
|
|
+ | LocalFuns funs ->
|
|
|
|
|
+ LocalFuns (trav_all funs)
|
|
|
|
|
+
|
|
|
| _ -> node
|
|
| _ -> node
|
|
|
|
|
|
|
|
- let rec annof = function
|
|
|
|
|
|
|
+let annotate a = function
|
|
|
|
|
+ | Program (decls, ann) ->
|
|
|
|
|
+ Program (decls, a :: ann)
|
|
|
|
|
+ | FunDec (ret_type, name, params, ann) ->
|
|
|
|
|
+ FunDec (ret_type, name, params, a :: ann)
|
|
|
|
|
+ | FunDef (export, ret_type, name, params, body, ann) ->
|
|
|
|
|
+ FunDef (export, ret_type, name, params, body, a :: ann)
|
|
|
|
|
+ | GlobalDec (ctype, name, ann) ->
|
|
|
|
|
+ GlobalDec (ctype, name, a :: ann)
|
|
|
|
|
+ | GlobalDef (export, ctype, name, init, ann) ->
|
|
|
|
|
+ GlobalDef (export, ctype, name, init, a :: ann)
|
|
|
|
|
+ | VarDec (ctype, name, init, ann) ->
|
|
|
|
|
+ VarDec (ctype, name, init, a :: ann)
|
|
|
|
|
+ | Assign (name, dims, value, ann) ->
|
|
|
|
|
+ Assign (name, dims, value, a :: ann)
|
|
|
|
|
+ | VarLet (dec, dims, value, ann) ->
|
|
|
|
|
+ VarLet (dec, dims, value, a :: ann)
|
|
|
|
|
+ | Return (value, ann) ->
|
|
|
|
|
+ Return (value, a :: ann)
|
|
|
|
|
+ | If (cond, body, ann) ->
|
|
|
|
|
+ If (cond, body, a :: ann)
|
|
|
|
|
+ | IfElse (cond, true_body, false_body, ann) ->
|
|
|
|
|
+ IfElse (cond, true_body, false_body, a :: ann)
|
|
|
|
|
+ | While (cond, body, ann) ->
|
|
|
|
|
+ While (cond, body, a :: ann)
|
|
|
|
|
+ | DoWhile (cond, body, ann) ->
|
|
|
|
|
+ DoWhile (cond, body, a :: ann)
|
|
|
|
|
+ | For (counter, start, stop, step, body, ann) ->
|
|
|
|
|
+ For (counter, start, stop, step, body, a :: ann)
|
|
|
|
|
+ | Allocate (name, dims, dec, ann) ->
|
|
|
|
|
+ Allocate (name, dims, dec, a :: ann)
|
|
|
|
|
+ | Monop (op, value, ann) ->
|
|
|
|
|
+ Monop (op, value, a :: ann)
|
|
|
|
|
+ | Binop (op, left, right, ann) ->
|
|
|
|
|
+ Binop (op, left, right, a :: ann)
|
|
|
|
|
+ | Cond (cond, true_expr, false_expr, ann) ->
|
|
|
|
|
+ Cond (cond, true_expr, false_expr, a :: ann)
|
|
|
|
|
+ | TypeCast (ctype, value, ann) ->
|
|
|
|
|
+ TypeCast (ctype, value, a :: ann)
|
|
|
|
|
+ | FunCall (name, args, ann) ->
|
|
|
|
|
+ FunCall (name, args, a :: ann)
|
|
|
|
|
+ | Arg value ->
|
|
|
|
|
+ Arg (value)
|
|
|
|
|
+ | Var (dec, dims, ann) ->
|
|
|
|
|
+ Var (dec, dims, a :: ann)
|
|
|
|
|
+ | VarUse (dec, dims, ann) ->
|
|
|
|
|
+ VarUse (dec, dims, a :: ann)
|
|
|
|
|
+ | FunUse (dec, params, ann) ->
|
|
|
|
|
+ FunUse (dec, params, a :: ann)
|
|
|
|
|
+ | Const (BoolVal value, ann) ->
|
|
|
|
|
+ Const (BoolVal value, a :: ann)
|
|
|
|
|
+ | Const (IntVal value, ann) ->
|
|
|
|
|
+ Const (IntVal value, a :: ann)
|
|
|
|
|
+ | Const (FloatVal value, ann) ->
|
|
|
|
|
+ Const (FloatVal value, a :: ann)
|
|
|
|
|
+ | ArrayConst (value, ann) ->
|
|
|
|
|
+ ArrayConst (value, a :: ann)
|
|
|
|
|
+ | Param (ctype, name, ann) ->
|
|
|
|
|
+ Param (ctype, name, a :: ann)
|
|
|
|
|
+ | Dim (name, ann) ->
|
|
|
|
|
+ Dim (name, a :: ann)
|
|
|
|
|
+
|
|
|
|
|
+ | _ -> raise InvalidNode
|
|
|
|
|
+
|
|
|
|
|
+let rec annof = function
|
|
|
| Program (_, ann)
|
|
| Program (_, ann)
|
|
|
| Param (_, _, ann)
|
|
| Param (_, _, ann)
|
|
|
| Dim (_, ann)
|
|
| Dim (_, ann)
|
|
@@ -153,9 +222,9 @@ let transform_children trav node =
|
|
|
| DoWhile (_, _, ann)
|
|
| DoWhile (_, _, ann)
|
|
|
| For (_, _, _, _, _, ann)
|
|
| For (_, _, _, _, _, ann)
|
|
|
| Allocate (_, _, _, ann)
|
|
| Allocate (_, _, _, ann)
|
|
|
- | BoolConst (_, ann)
|
|
|
|
|
- | IntConst (_, ann)
|
|
|
|
|
- | FloatConst (_, ann)
|
|
|
|
|
|
|
+ | Const (BoolVal _, ann)
|
|
|
|
|
+ | Const (IntVal _, ann)
|
|
|
|
|
+ | Const (FloatVal _, ann)
|
|
|
| ArrayConst (_, ann)
|
|
| ArrayConst (_, ann)
|
|
|
| Var (_, _, ann)
|
|
| Var (_, _, ann)
|
|
|
| Monop (_, _, ann)
|
|
| Monop (_, _, ann)
|
|
@@ -173,20 +242,33 @@ let transform_children trav node =
|
|
|
|
|
|
|
|
| _ -> raise InvalidNode
|
|
| _ -> raise InvalidNode
|
|
|
|
|
|
|
|
- let locof node =
|
|
|
|
|
|
|
+let locof node =
|
|
|
let rec trav = function
|
|
let rec trav = function
|
|
|
| [] -> noloc
|
|
| [] -> noloc
|
|
|
| Loc loc :: tl -> loc
|
|
| Loc loc :: tl -> loc
|
|
|
| _ :: tl -> trav tl
|
|
| _ :: tl -> trav tl
|
|
|
in trav (annof node)
|
|
in trav (annof node)
|
|
|
|
|
|
|
|
- let rec depthof node =
|
|
|
|
|
|
|
+let rec depthof node =
|
|
|
let rec trav = function
|
|
let rec trav = function
|
|
|
- | [] -> raise InvalidNode
|
|
|
|
|
|
|
+ | [] ->
|
|
|
|
|
+ prerr_string "cannot get depth for: ";
|
|
|
|
|
+ prt_node node;
|
|
|
|
|
+ raise InvalidNode
|
|
|
| Depth depth :: tl -> depth
|
|
| Depth depth :: tl -> depth
|
|
|
| _ :: tl -> trav tl
|
|
| _ :: tl -> trav tl
|
|
|
in trav (annof node)
|
|
in trav (annof node)
|
|
|
|
|
|
|
|
|
|
+let rec indexof node =
|
|
|
|
|
+ let rec trav = function
|
|
|
|
|
+ | [] ->
|
|
|
|
|
+ prerr_string "cannot get index for: ";
|
|
|
|
|
+ prt_node node;
|
|
|
|
|
+ raise InvalidNode
|
|
|
|
|
+ | Index index :: tl -> index
|
|
|
|
|
+ | _ :: tl -> trav tl
|
|
|
|
|
+ in trav (annof node)
|
|
|
|
|
+
|
|
|
let typeof = function
|
|
let typeof = function
|
|
|
(* Some nodes have their type as property *)
|
|
(* Some nodes have their type as property *)
|
|
|
| VarDec (ctype, _, _, _)
|
|
| VarDec (ctype, _, _, _)
|
|
@@ -201,7 +283,10 @@ let typeof = function
|
|
|
(* Other nodes must be annotated during typechecking *)
|
|
(* Other nodes must be annotated during typechecking *)
|
|
|
| node ->
|
|
| node ->
|
|
|
let rec trav = function
|
|
let rec trav = function
|
|
|
- | [] -> prerr_string "cannot get type for: "; prt_node node; raise InvalidNode
|
|
|
|
|
|
|
+ | [] ->
|
|
|
|
|
+ prerr_string "cannot get type for: ";
|
|
|
|
|
+ prt_node node;
|
|
|
|
|
+ raise InvalidNode
|
|
|
| Type t :: tl -> t
|
|
| Type t :: tl -> t
|
|
|
| _ :: tl -> trav tl
|
|
| _ :: tl -> trav tl
|
|
|
in trav (annof node)
|
|
in trav (annof node)
|
|
@@ -210,7 +295,7 @@ let prerr_loc (fname, ystart, yend, xstart, xend) =
|
|
|
let file = open_in fname in
|
|
let file = open_in fname in
|
|
|
|
|
|
|
|
(* skip lines until the first matched line *)
|
|
(* skip lines until the first matched line *)
|
|
|
- for i = 1 to ystart - 1 do input_line file done;
|
|
|
|
|
|
|
+ for i = 1 to ystart - 1 do let _ = input_line file in () done;
|
|
|
|
|
|
|
|
(* for each line in `loc`, print the source line with an underline *)
|
|
(* for each line in `loc`, print the source line with an underline *)
|
|
|
for l = ystart to yend do
|
|
for l = ystart to yend do
|