示例#1
0
        public void TestDictionary()
        {
            var scope = new InterpreterScope();
            var dict  = new DictionaryExpression();

            dict.Add(new IntegerConstantExpression(1), new StringConstantExpression("One"));
            scope.DefineVariable(new VariableDefinitionExpression("dict"), dict);

            Assert.That(Evaluate("length(dict)", scope), Is.EqualTo(1));

            dict.Add(new IntegerConstantExpression(5), new StringConstantExpression("Five"));
            dict.Add(new IntegerConstantExpression(9), new StringConstantExpression("Nine"));

            Assert.That(Evaluate("length(dict)", scope), Is.EqualTo(3));
        }
示例#2
0
        public void TestPushMemoryComparison()
        {
            var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope());
            var array = new ArrayExpression();

            scope.DefineVariable(new VariableDefinitionExpression("arr"), array);
            AddHappyFunction(scope);

            Evaluate("array_push(arr, byte(1) == 2)", scope);

            var comparison = (ComparisonExpression)array.Entries[0];

            Assert.That(comparison.Left, Is.InstanceOf <FunctionCallExpression>());
            Assert.That(((FunctionCallExpression)comparison.Left).FunctionName.Name, Is.EqualTo("byte"));
            Assert.That(comparison.Right, Is.InstanceOf <IntegerConstantExpression>());
            Assert.That(((IntegerConstantExpression)comparison.Right).Value, Is.EqualTo(2));
        }
示例#3
0
        public void TestArray()
        {
            var scope = new InterpreterScope();
            var array = new ArrayExpression();

            array.Entries.Add(new IntegerConstantExpression(1));
            array.Entries.Add(new IntegerConstantExpression(2));
            scope.DefineVariable(new VariableDefinitionExpression("arr"), array);

            Assert.That(Evaluate("length(arr)", scope), Is.EqualTo(2));

            array.Entries.Add(new IntegerConstantExpression(9));
            array.Entries.Add(new IntegerConstantExpression(8));
            array.Entries.Add(new IntegerConstantExpression(7));

            Assert.That(Evaluate("length(arr)", scope), Is.EqualTo(5));
        }
示例#4
0
        public void TestMerge()
        {
            var scope1 = new InterpreterScope();

            scope1.DefineVariable(new VariableDefinitionExpression("a"), new IntegerConstantExpression(1));

            var scope2 = new InterpreterScope();

            scope2.DefineVariable(new VariableDefinitionExpression("a"), new IntegerConstantExpression(4));
            scope2.DefineVariable(new VariableDefinitionExpression("d"), new IntegerConstantExpression(5));
            scope2.AddFunction(new FunctionDefinitionExpression("f1"));

            var scope3 = new InterpreterScope();

            scope3.DefineVariable(new VariableDefinitionExpression("b"), new IntegerConstantExpression(2));
            scope3.DefineVariable(new VariableDefinitionExpression("c"), new IntegerConstantExpression(3));

            var scope4 = new InterpreterScope();

            scope4.AddFunction(new FunctionDefinitionExpression("f1"));
            scope4.AddFunction(new FunctionDefinitionExpression("f2"));

            var union = new InterpreterScope();

            Assert.That(union.VariableCount, Is.EqualTo(0));
            Assert.That(union.FunctionCount, Is.EqualTo(0));

            union.Merge(scope1);
            Assert.That(union.VariableCount, Is.EqualTo(1));
            Assert.That(union.FunctionCount, Is.EqualTo(0));

            union.Merge(scope2);
            Assert.That(union.VariableCount, Is.EqualTo(2));
            Assert.That(union.FunctionCount, Is.EqualTo(1));
            Assert.That(union.GetVariable("a"), Is.EqualTo(new IntegerConstantExpression(4)));

            union.Merge(scope3);
            Assert.That(union.VariableCount, Is.EqualTo(4));
            Assert.That(union.FunctionCount, Is.EqualTo(1));

            union.Merge(scope4);
            Assert.That(union.VariableCount, Is.EqualTo(4));
            Assert.That(union.FunctionCount, Is.EqualTo(2));
        }
示例#5
0
        public void TestSimple()
        {
            var scope = new InterpreterScope();
            var array = new ArrayExpression();

            scope.DefineVariable(new VariableDefinitionExpression("arr"), array);

            Evaluate("array_push(arr, 1)", scope);
            Assert.That(array.Entries.Count, Is.EqualTo(1));
            Assert.That(array.Entries[0], Is.InstanceOf <IntegerConstantExpression>());
            Assert.That(((IntegerConstantExpression)array.Entries[0]).Value, Is.EqualTo(1));

            Evaluate("array_push(arr, \"2\")", scope);
            Assert.That(array.Entries.Count, Is.EqualTo(2));
            Assert.That(array.Entries[0], Is.InstanceOf <IntegerConstantExpression>());
            Assert.That(((IntegerConstantExpression)array.Entries[0]).Value, Is.EqualTo(1));
            Assert.That(array.Entries[1], Is.InstanceOf <StringConstantExpression>());
            Assert.That(((StringConstantExpression)array.Entries[1]).Value, Is.EqualTo("2"));
        }
示例#6
0
        public void TestPopFunctionCall()
        {
            var scope = new InterpreterScope();

            var array = new ArrayExpression();

            array.Entries.Add(new FunctionCallExpression("happy", new ExpressionBase[] { new IntegerConstantExpression(1) }));
            scope.DefineVariable(new VariableDefinitionExpression("arr"), array);

            var happyFunc = new FunctionDefinitionExpression("happy");

            happyFunc.Parameters.Add(new VariableDefinitionExpression("num1"));
            happyFunc.Expressions.Add(new ReturnExpression(new VariableExpression("num1")));
            scope.AddFunction(happyFunc);

            var entry = Evaluate("array_pop(arr)", scope);

            // function call should not be evaluated when it's popped off the array
            Assert.That(entry, Is.InstanceOf <FunctionCallExpression>());
            Assert.That(((FunctionCallExpression)entry).FunctionName.Name, Is.EqualTo("happy"));
            Assert.That(((FunctionCallExpression)entry).Parameters.Count, Is.EqualTo(1));

            Assert.That(array.Entries.Count, Is.EqualTo(0));
        }
示例#7
0
        private bool EvaluateLoop(ForExpression forExpression, InterpreterScope scope)
        {
            ExpressionBase range;

            if (!forExpression.Range.ReplaceVariables(scope, out range))
            {
                Error = range as ParseErrorExpression;
                return(false);
            }

            var dict = range as DictionaryExpression;

            if (dict != null)
            {
                var iterator         = forExpression.IteratorName;
                var iteratorScope    = new InterpreterScope(scope);
                var iteratorVariable = new VariableExpression(iterator.Name);

                foreach (var entry in dict.Entries)
                {
                    iteratorScope.Context = new AssignmentExpression(iteratorVariable, entry.Key);

                    ExpressionBase key;
                    if (!entry.Key.ReplaceVariables(iteratorScope, out key))
                    {
                        Error = key as ParseErrorExpression;
                        return(false);
                    }

                    var loopScope = new InterpreterScope(scope);
                    loopScope.DefineVariable(iterator, key);

                    if (!Evaluate(forExpression.Expressions, loopScope))
                    {
                        return(false);
                    }

                    if (loopScope.IsComplete)
                    {
                        if (loopScope.ReturnValue != null)
                        {
                            scope.ReturnValue = loopScope.ReturnValue;
                            scope.IsComplete  = true;
                        }
                        break;
                    }
                }

                return(true);
            }

            var array = range as ArrayExpression;

            if (array != null)
            {
                var iterator         = forExpression.IteratorName;
                var iteratorScope    = new InterpreterScope(scope);
                var iteratorVariable = new VariableExpression(iterator.Name);

                foreach (var entry in array.Entries)
                {
                    iteratorScope.Context = new AssignmentExpression(iteratorVariable, entry);

                    ExpressionBase key;
                    if (!entry.ReplaceVariables(iteratorScope, out key))
                    {
                        Error = key as ParseErrorExpression;
                        return(false);
                    }

                    var loopScope = new InterpreterScope(scope);
                    loopScope.DefineVariable(iterator, key);

                    if (!Evaluate(forExpression.Expressions, loopScope))
                    {
                        return(false);
                    }

                    if (loopScope.IsComplete)
                    {
                        if (loopScope.ReturnValue != null)
                        {
                            scope.ReturnValue = loopScope.ReturnValue;
                            scope.IsComplete  = true;
                        }
                        break;
                    }
                }

                return(true);
            }

            Error = new ParseErrorExpression("Cannot iterate over " + forExpression.Range.ToString(), forExpression.Range);
            return(false);
        }