Przeglądaj źródła

Added new node type for array assignment

Taddeus Kroes 12 lat temu
rodzic
commit
85108f6d9b
4 zmienionych plików z 10 dodań i 0 usunięć
  1. 1 0
      ast.ml
  2. 4 0
      parser.mly
  3. 2 0
      stringify.ml
  4. 3 0
      util.ml

+ 1 - 0
ast.ml

@@ -22,6 +22,7 @@ and node =
     (* 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

+ 4 - 0
parser.mly

@@ -159,6 +159,10 @@ statement:
     | name=ID; ASSIGN; value=expr; SEMICOL
     { Assign (name, value, loc $startpos(name) $endpos(name)) }
 
+    | name=ID; LBRACK; dims=separated_list(COMMA, expr); RBRACK;
+      ASSIGN; value=expr; SEMICOL
+    { ArrayAssign (name, dims, value, loc $startpos(name) $endpos(name)) }
+
     | name=ID; LPAREN; args=separated_list(COMMA, expr); RPAREN; SEMICOL
     { Expr (FunCall (name, make_args args, loc $startpos(name) $endpos(name))) }
 

+ 2 - 0
stringify.ml

@@ -70,6 +70,8 @@ and node2str node =
         (type2str var_type) ^ " " ^ name ^ " = " ^ (str init) ^ ";"
     | Assign (name, value, _) ->
         name ^ " = " ^ (str value) ^ ";"
+    | ArrayAssign (name, dims, value, _) ->
+        name ^ "[" ^ (concat ", " dims) ^ "] = " ^ (str value) ^ ";"
     | Expr expr ->
         str expr ^ ";"
     | Return (value, _) ->

+ 3 - 0
util.ml

@@ -41,6 +41,8 @@ let transform_children trav node =
         VarDec (ctype, name, Some (trav init), loc)
     | Assign (name, value, loc) ->
         Assign (name, trav value, loc)
+    | ArrayAssign (name, dims, value, loc) ->
+        ArrayAssign (name, trav_all dims, trav value, loc)
     | Return (value, loc) ->
         Return (trav value, loc)
     | If (cond, body, loc) ->
@@ -100,6 +102,7 @@ let rec transform_all trav = function
     | GlobalDef (_, _, _, _, loc)
     | VarDec (_, _, _, loc)
     | Assign (_, _, loc)
+    | ArrayAssign (_, _, _, loc)
     | Return (_, loc)
     | If (_, _, loc)
     | IfElse (_, _, _, loc)