Przeglądaj źródła

Added the local keyword in the for syntax

Chimrod 4 miesięcy temu
rodzic
commit
20fcb5529e
2 zmienionych plików z 23 dodań i 2 usunięć
  1. 6 2
      lib/qparser/parser.mly
  2. 17 0
      test/syntax.ml

+ 6 - 2
lib/qparser/parser.mly

@@ -67,7 +67,8 @@ line_statement:
       }
     | b = inlined_block(line_sep)
       { b }
-    | FOR 
+    | FOR
+      local = option(LOCAL)
       variable = variable
       EQUAL
       start = expression
@@ -78,7 +79,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;
     ] )