Browse Source

Included the inline for loop syntax

Chimrod 3 months ago
parent
commit
670eac9c96
2 changed files with 56 additions and 0 deletions
  1. 21 0
      lib/qparser/qsp_single_line.mly
  2. 35 0
      test/syntax.ml

+ 21 - 0
lib/qparser/qsp_single_line.mly

@@ -38,6 +38,27 @@ empty:
             ~elifs
             ~else_
       }
+    | FOR
+      local = option(LOCAL)
+      variable = variable
+      EQUAL
+      start = expression
+      TO 
+      to_ = expression
+      step = option(pair(STEP, expression))
+      COLUMN
+      s = rev (final_inline_instruction)
+      END
+      { 
+          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
+          Analyzer.Instruction.for_ $loc variable ~start ~to_ ~step s
+      }
 
 final_inline_instruction:
     | hd = inline_instruction 

+ 35 - 0
test/syntax.ml

@@ -1004,6 +1004,40 @@ end|}
         };
     ]
 
+let inline_for () =
+  _test_instruction {|for i = 0 to 5: msg i|}
+    [
+      Tree.Ast.For
+        {
+          loc = _position;
+          variable =
+            {
+              Tree.Ast.pos = _position;
+              name = "I";
+              index = None;
+              local = false;
+            };
+          start = Tree.Ast.Integer (_position, "0");
+          to_ = Tree.Ast.Integer (_position, "5");
+          step = None;
+          statements =
+            [
+              Tree.Ast.Call
+                ( _position,
+                  T.Msg,
+                  [
+                    Tree.Ast.Ident
+                      {
+                        Tree.Ast.pos = _position;
+                        name = "I";
+                        index = None;
+                        local = false;
+                      };
+                  ] );
+            ];
+        };
+    ]
+
 let test =
   ( "Syntax",
     [
@@ -1077,4 +1111,5 @@ let test =
       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;
+      Alcotest.test_case "inline for" `Quick inline_for;
     ] )