public void CanExecuteStatemetnsTwice()
        {
            var l = new ListOfStatementsExpression(new IStatement[] {
                new AssignmentStatement("a", new FunctionExpression("+", new VariableValue("a"), new IntegerValue(5))),
            });
            RootContext c = new RootContext();

            c.SetVariableValue("a", 0);
            l.Evaluate(c);
            l.Evaluate(c);
            var r = c.GetVariableValue("a");

            Assert.AreEqual(10, r, "value of result");
        }
        public void ExecuteSingleExpressionNoSideEffects()
        {
            var         l = new ListOfStatementsExpression(new IStatement[] { new AssignmentStatement("a", new IntegerValue(5)) });
            RootContext c = new RootContext();
            var         r = l.Evaluate(c);

            Assert.IsFalse(c.GetVariableValueOrNull("a").Item1, "variable part fo context");
        }
        public void ExecuteSingleExpression()
        {
            var         l = new ListOfStatementsExpression(new IStatement[] { new ExpressionStatement(new IntegerValue(5)) });
            RootContext c = new RootContext();
            var         r = l.Evaluate(c);

            Assert.AreEqual(5, r, "Result of eval");
        }
        public void TestNoSttatements()
        {
            var         l = new ListOfStatementsExpression(new IStatement[] { });
            RootContext c = new RootContext();
            var         r = l.Evaluate(c);

            Assert.IsNull(r, "result when no statements");
        }
        public void ExecuteContextUpdatedWhileRunning()
        {
            var l = new ListOfStatementsExpression(new IStatement[] {
                new AssignmentStatement("a", new IntegerValue(5)),
                new ExpressionStatement(new FunctionExpression("+", new VariableValue("a"), new IntegerValue(5)))
            });
            RootContext c = new RootContext();
            var         r = l.Evaluate(c);

            Assert.AreEqual(10, r, "value of result");
        }
        public void UpdateExistingVariables()
        {
            var l = new ListOfStatementsExpression(new IStatement[] {
                new AssignmentStatement("a", new IntegerValue(5)),
            });
            RootContext c = new RootContext();

            c.SetVariableValue("a", 6);
            l.Evaluate(c);
            var r = c.GetVariableValue("a");

            Assert.AreEqual(5, r, "value of result");
        }
Esempio n. 7
0
        /// <summary>
        /// Simple if statement.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="logicalTest"></param>
        /// <param name="statements"></param>
        /// <returns></returns>
        public static object ifReserved(IScopeContext ctx, IExpression logicalTest, ListOfStatementsExpression statements)
        {
            // See if we have fired
            var testResult = logicalTest.Evaluate(ctx);

            if (testResult.GetType() != typeof(bool) && testResult.GetType() != typeof(int))
            {
                throw new ArgumentException($"The test {logicalTest.ToString()} did not evaluate to an integer or a boolean");
            }

            var shouldExecute = testResult.GetType() == typeof(bool)
                ? (bool)testResult
                : (int)testResult != 0;

            if (shouldExecute)
            {
                var newScope = new ScopeContext(ctx);
                return(statements.Evaluate(newScope));
            }
            return(null);
        }
Esempio n. 8
0
        /// <summary>
        /// A regular for loop, using a list of dictionaries as the items we loop over. For each iteration, a dictionary is
        /// pulled off the loopControl sequence. The keys are all evaluated to strings, and then used as variable names that
        /// can be referenced in the body of the loop. They are set to the value of the dictionary value.
        /// </summary>
        /// <param name="ctx">Scope context for variable definitions, etc.</param>
        /// <param name="loopControl">A list of dictionaries that we used to set the loop variables</param>
        /// <param name="statements">The list of statements we will process</param>
        /// <returns></returns>
        /// <remarks>Because "for" is a reserved word, this function needs the "Reserved" tacked onto the end. During method
        /// resolution, the language core should take care of this.</remarks>
        public static object forReserved(IScopeContext ctx, IEnumerable <object> loopControl, ListOfStatementsExpression statements)
        {
            object result = null;

            foreach (var iterLoopVars in loopControl)
            {
                var dict = iterLoopVars as IDictionary <object, object>;
                if (dict == null)
                {
                    throw new ArgumentException("For loop over dictionary items - every item must be a dictionary!");
                }

                var newScope = new ScopeContext(ctx);
                foreach (var varDefined in dict)
                {
                    newScope.SetVariableValueLocally(varDefined.Key.ToString(), varDefined.Value);
                }

                result = statements.Evaluate(newScope);
            }

            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// Given a list of objects that are dictionarys, run the loop. Take the last result from each loop, and transform it into a
        /// list of values that gets passed back to the caller.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="loopControl"></param>
        /// <param name="statements"></param>
        /// <returns></returns>
        public static IEnumerable <object> map(IScopeContext ctx, IEnumerable <object> loopControl, ListOfStatementsExpression statements)
        {
            return(loopControl.Select(iterLoopVars =>
            {
                var dict = iterLoopVars as IDictionary <object, object>;
                if (dict == null)
                {
                    throw new ArgumentException("For loop over dictionary items - every item must be a dictionary!");
                }

                var newScope = new ScopeContext(ctx);
                foreach (var varDefined in dict)
                {
                    newScope.SetVariableValueLocally(varDefined.Key.ToString(), varDefined.Value);
                }

                var v = statements.Evaluate(newScope);
                if (v == null)
                {
                    throw new ArgumentNullException("Iteration of map returned null!");
                }
                return v;
            }));
        }