예제 #1
0
        public void TestReplaceVariablesIndexFunctionCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) => 6");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value        = new IntegerConstantExpression(98);

            var variable = new VariableExpression("variable");
            var dict     = new DictionaryExpression();

            dict.Add(new IntegerConstantExpression(6), value);

            var scope = new InterpreterScope();

            scope.AssignVariable(variable, dict);
            scope.AddFunction(functionDefinition);

            var expr = new IndexedVariableExpression(variable, functionCall);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <IntegerConstantExpression>());
            Assert.That(((IntegerConstantExpression)result).Value, Is.EqualTo(98));
        }
        private FunctionDefinitionExpression Parse(string input)
        {
            var def = UserFunctionDefinitionExpression.ParseForTest(input);

            Assert.That(def, Is.Not.Null);
            return((FunctionDefinitionExpression)def);
        }
예제 #3
0
        public void TestReplaceVariablesMethodCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) { j = i }");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            var parseError = (ParseErrorExpression)result;

            while (parseError.InnerError != null)
            {
                parseError = parseError.InnerError;
            }
            Assert.That(parseError.Message, Is.EqualTo("func did not return a value"));
        }
예제 #4
0
        public void TestFunctionCall()
        {
            var scope = new InterpreterScope();
            var array = new ArrayExpression();

            scope.DefineVariable(new VariableDefinitionExpression("arr"), array);
            scope.AddFunction(UserFunctionDefinitionExpression.ParseForTest(
                                  "function happy(val) => val + \"rama\""
                                  ));

            Assert.That(Evaluate("length(happy(\"banana\"))", scope), Is.EqualTo(10));
        }
예제 #5
0
        public void TestReplaceVariablesLogicalFunctionCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) => byte(i) == 1");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope(AchievementScriptInterpreter.GetGlobalScope());

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.False);
            Assert.That(result, Is.InstanceOf <ParseErrorExpression>());
            Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("Dictionary key must evaluate to a constant"));
        }
예제 #6
0
        public void TestReplaceVariablesFunctionCall()
        {
            var functionDefinition = UserFunctionDefinitionExpression.ParseForTest("function func(i) => 6");

            var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new IntegerConstantExpression(2) });
            var value1       = new IntegerConstantExpression(98);
            var expr         = new DictionaryExpression();

            expr.Add(functionCall, value1);

            var scope = new InterpreterScope();

            scope.AddFunction(functionDefinition);

            ExpressionBase result;

            Assert.That(expr.ReplaceVariables(scope, out result), Is.True);
            Assert.That(result, Is.InstanceOf <DictionaryExpression>());
            var dictResult = (DictionaryExpression)result;

            Assert.That(dictResult.Count, Is.EqualTo(1));
            Assert.That(dictResult[0].Key, Is.EqualTo(new IntegerConstantExpression(6)));
            Assert.That(dictResult[0].Value, Is.EqualTo(value1));
        }
예제 #7
0
 private void AddHappyFunction(InterpreterScope scope)
 {
     scope.AddFunction(UserFunctionDefinitionExpression.ParseForTest(
                           "function happy(num1) => num1"
                           ));
 }