Forráskód Böngészése

Added the local keyword in the for syntax

Chimrod 4 hónapja
szülő
commit
4979729490
2 módosított fájl, 23 hozzáadás és 2 törlés
  1. 6 2
      lib/qparser/parser.mly
  2. 17 0
      test/syntax.ml

+ 6 - 2
lib/qparser/parser.mly

@@ -68,7 +68,8 @@ line_statement:
       { let {loc; expression; body;  _}  = a in 
         Analyzer.Instruction.act loc ~label:expression body
       }
-    | FOR 
+    | FOR
+      local = option(LOCAL)
       variable = variable
       EQUAL
       start = expression
@@ -79,7 +80,10 @@ line_statement:
       s = line_statement*
       END EOL+
       {
-          let variable = Helper.variable variable in
+          let local = match local with 
+            | Some () -> true
+            | _ -> false in
+          let variable = { (Helper.variable variable) with local } in
           let start = Analyzer.Expression.v start in
           let to_ = Analyzer.Expression.v to_ in
           let step = Option.map (fun v -> Analyzer.Expression.v (snd v)) step in

+ 17 - 0
test/syntax.ml

@@ -988,6 +988,22 @@ let test_local () =
           Tree.Ast.Integer (_position, "12") );
     ]
 
+let test_local_for () =
+  _test_instruction {|for local a = 1 to 10:
+end|}
+    [
+      Tree.Ast.For
+        {
+          loc = _position;
+          variable =
+            { Tree.Ast.pos = _position; name = "A"; index = None; local = true };
+          start = Tree.Ast.Integer (_position, "1");
+          to_ = Tree.Ast.Integer (_position, "10");
+          step = None;
+          statements = [];
+        };
+    ]
+
 let test =
   ( "Syntax",
     [
@@ -1060,4 +1076,5 @@ let test =
       Alcotest.test_case "for ... end" `Quick test_for_end;
       Alcotest.test_case "for step ... end" `Quick test_for_end_with_step;
       Alcotest.test_case "local variable" `Quick test_local;
+      Alcotest.test_case "local variable in for loop" `Quick test_local_for;
     ] )