private ForInstruction PopulateForInstruction(ScopePrototype scope, ForExpression forExpression)
        {
            // check if the value to be iterated over exists in the scope
            if (!scope.Variables.Contains(forExpression.Collection.VariableName))
            {
                throw new SemanticsException($"Variable {forExpression.Collection.VariableName} has not been declared in the scope of operation for in function {scope.FunctionName}.");
            }
            var forInstruction = new ForInstruction(scope, forExpression);

            PopulateInstructions(forExpression.Instructions, forInstruction.Block.NestedBlocks, forInstruction.Block.ScopePrototype);
            return(forInstruction);
        }
Example #2
0
        public void DefinedForInstruction_CallInsideScopeWithArrayOfObjects_NestedScopesInitializedCorrectly()
        {
            // prepare
            var outer_scope_prototype = new ScopePrototype {
                Variables = new HashSet <string> {
                    "model"
                },
            };
            var outer_scope = new Scope(outer_scope_prototype)
            {
                VariableValues = new Dictionary <string, AssignedValue> {
                    { "model", new AssignedValue(JToken.Parse("{'field1':[{'prop': 2},{'prop': 5},{'prop':8}],'field2':'val2'}")) }
                }
            };
            var forInstruction = new ForInstruction(outer_scope_prototype, new ForExpression {
                Collection = new ValueOf {
                    VariableName = "model",
                    NestedValue  = new ValueOf {
                        VariableName = "field1"
                    }
                },
                ElementName = "element"
            });

            forInstruction.Block.NestedBlocks.Add(new StringComponentInstruction(forInstruction.Block.ScopePrototype, new ValueOf {
                VariableName = "element",
                NestedValue  = new ValueOf {
                    VariableName = "prop"
                }
            }));

            // act
            var streamWriter = new Mock <StreamWriter>(new MemoryStream());
            var node         = new Node {
                StreamWriter = streamWriter.Object,
                NewLine      = false,
                NestedLevel  = 0,
                Scope        = outer_scope
            };

            forInstruction.Execute(node);

            // validate
            // Assert.Single(forInstruction.ScopePrototype.Variables);
            // Assert.True(forInstruction.Scope.VariableValues.ContainsKey("model"));
            // Assert.Single(forInstruction.Block.Scope.VariableValues);
            // Assert.Equal("8", forInstruction.Block.Scope.VariableValues["element"].);
            streamWriter.Verify(s => s.Write("2"), Times.Once);
            streamWriter.Verify(s => s.Write("5"), Times.Once);
            streamWriter.Verify(s => s.Write("8"), Times.Once);
        }
        public void TestExpressions()
        {
            VirtualMachine vm = new VirtualMachine();

            List <byte> actual = new ForInstruction()
                                 .NumberOfLoops(new LiteralInstruction(_numberOfLoops))
                                 .Body(b =>
            {
                b.Literal(_val);
            })
                                 .ToInstructions();

            TestHelper.AssertResultsEqual(_expected, actual);
        }
        public void TestNoBodyOrNumberOfLoops()
        {
            VirtualMachine vm = new VirtualMachine();

            _expected = new List <byte>()
            {
                (byte)InstructionsEnum.For,
                (byte)InstructionsEnum.EndFor
            };

            List <byte> actual = new ForInstruction()
                                 .ToInstructions();

            TestHelper.AssertResultsEqual(_expected, actual);
        }