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); }
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")); }
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)); }
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")); }
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)); }
private void AddHappyFunction(InterpreterScope scope) { scope.AddFunction(UserFunctionDefinitionExpression.ParseForTest( "function happy(num1) => num1" )); }