Kaynağa Gözat

Fixed issue where nested for-loop counters would not be replaced properly

Taddeus Kroes 12 yıl önce
ebeveyn
işleme
6a77ae3cca
4 değiştirilmiş dosya ile 14 ekleme ve 7 silme
  1. 1 1
      ast.ml
  2. 10 5
      phases/desug.ml
  3. 1 1
      stringify.ml
  4. 2 0
      util.ml

+ 1 - 1
ast.ml

@@ -42,12 +42,12 @@ and node =
     | 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 *)
+    | Cond of node * node * node * loc
     | VarLet of node * ctype * int
     | VarUse of node * ctype * int
     | FunUse of node * node * int

+ 10 - 5
phases/desug.ml

@@ -1,9 +1,15 @@
 open Ast
 open Util
 
-let rec replace_var var replacement = function
-    | Var (name, loc) when name = var -> Var (replacement, loc)
-    | node -> transform_children (replace_var var replacement) node
+let rec replace_var var replacement node =
+    let trav = (replace_var var replacement) in
+    match node with
+    | Var (name, loc) when name = var ->
+        Var (replacement, loc)
+    | For (counter, start, stop, step, body, loc) when counter = var ->
+        For (replacement, trav start, trav stop, trav step, trav body, loc)
+    | node ->
+        transform_children trav node
 
 let for_to_while node =
     let new_vars = ref [] in
@@ -40,8 +46,7 @@ let for_to_while node =
 
         | node -> transform_children traverse node
     in
-    let node = traverse node in
-    (node, new_vars)
+    (traverse node, new_vars)
 
 let rec var_init = function
     (* Move global initialisations to __init function *)

+ 1 - 1
stringify.ml

@@ -106,9 +106,9 @@ and node2str node =
     | Monop (op, opnd, _) -> op2str op ^ str opnd
     | Binop (op, left, right, _) ->
         "(" ^ str left ^ " " ^ op2str op ^ " " ^ str right ^ ")"
-    | Cond (cond, t, f, _) -> (str cond) ^ " ? " ^ str t ^ " : " ^ str f
     | TypeCast (ctype, value, _) -> "(" ^ type2str ctype ^ ")" ^ str value
     | FunCall (name, args, _) -> name ^ "(" ^ (concat ", " args) ^ ")"
+    | Cond (cond, t, f, _) -> (str cond) ^ " ? " ^ str t ^ " : " ^ str f
 
     (* FIXME: these should be printed when verbose=3
     | Arg node -> "<arg>(" ^ str node ^ ")"

+ 2 - 0
util.ml

@@ -74,6 +74,8 @@ let transform_children trav node =
         FunCall (name, trav_all args, loc)
     | Arg value ->
         Arg (trav value)
+    | Deref (name, dims, loc) ->
+        Deref (name, trav_all dims, loc)
 
     | Type (value, ctype) ->
         Type (trav value, ctype)