public void TestEvaluateDictionaryByReference() { // ensures the dictionary is passed by reference to func(), so it can be modified // within func(). it's also much more efficient to pass the dictionary by reference // instead of evaluating it (which creates a copy). var functionDefinition = Parse("function func(d) { d[\"key\"] = 2 }"); var scope = new InterpreterScope(); scope.AddFunction(functionDefinition); var dict = new DictionaryExpression(); dict.Add(new StringConstantExpression("key"), new IntegerConstantExpression(1)); scope.AssignVariable(new VariableExpression("dict"), dict); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { new VariableExpression("dict") }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.Null); Assert.That(dict.Count, Is.EqualTo(1)); Assert.That(dict[0].Value, Is.InstanceOf <IntegerConstantExpression>()); Assert.That(((IntegerConstantExpression)dict[0].Value).Value, Is.EqualTo(2)); }
private string EvaluateError(string formatString, ExpressionBase[] parameters) { var newParameters = new List <ExpressionBase>(); newParameters.Add(new StringConstantExpression(formatString)); newParameters.AddRange(parameters); var expression = new FunctionCallExpression("format", newParameters); var scope = new InterpreterScope(); scope.AddFunction(new FormatFunction()); scope.AddFunction(new AddFunction()); ExpressionBase result; Assert.That(expression.Evaluate(scope, out result), Is.False); Assert.That(result, Is.InstanceOf <ParseErrorExpression>()); var parseError = (ParseErrorExpression)result; while (parseError.InnerError != null) { parseError = parseError.InnerError; } return(parseError.Message); }
public void TestEvaluateUnknownFunction() { var scope = new InterpreterScope(); var value = new IntegerConstantExpression(6); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { value }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.False); Assert.That(result, Is.InstanceOf <ParseErrorExpression>()); Assert.That(((ParseErrorExpression)result).Message, Is.EqualTo("Unknown function: func")); }
public void TestEvaluateMethod() { var functionDefinition = Parse("function func(i) { j = i }"); var scope = new InterpreterScope(); scope.AddFunction(functionDefinition); var value = new IntegerConstantExpression(6); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { value }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.Null); }
public void TestEvaluateMathematical() { var functionDefinition = Parse("function func(i) { return i * 2 }"); var scope = new InterpreterScope(); scope.AddFunction(functionDefinition); var value = new IntegerConstantExpression(6); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { value }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.EqualTo(new IntegerConstantExpression(12))); }
public void TestEvaluateConditional() { var functionDefinition = Parse("function func(i) { if (i < 3) return 4 else return 8 }"); var scope = new InterpreterScope(); scope.AddFunction(functionDefinition); var value = new IntegerConstantExpression(6); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { value }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.EqualTo(new IntegerConstantExpression(8))); value = new IntegerConstantExpression(2); functionCall = new FunctionCallExpression("func", new ExpressionBase[] { value }); Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.EqualTo(new IntegerConstantExpression(4))); }
private string Evaluate(string formatString, ExpressionBase[] parameters) { var newParameters = new List <ExpressionBase>(); newParameters.Add(new StringConstantExpression(formatString)); newParameters.AddRange(parameters); var expression = new FunctionCallExpression("format", newParameters); var scope = new InterpreterScope(); scope.AddFunction(new FormatFunction()); scope.AddFunction(new AddFunction()); ExpressionBase result; Assert.IsTrue(expression.Evaluate(scope, out result)); Assert.IsTrue(result is StringConstantExpression); return(((StringConstantExpression)result).Value); }
public void TestEvaluateDictionaryDirect() { // ensures that a dictionary being passed directly to a function is evaluated var functionDefinition = Parse("function func(d) { return d[\"key\"] }"); var scope = new InterpreterScope(); scope.AddFunction(functionDefinition); var dict = new DictionaryExpression(); dict.Add(new StringConstantExpression("key"), new VariableExpression("variable")); scope.AssignVariable(new VariableExpression("variable"), new IntegerConstantExpression(123)); var functionCall = new FunctionCallExpression("func", new ExpressionBase[] { dict }); ExpressionBase result; Assert.That(functionCall.Evaluate(scope, out result), Is.True); Assert.That(result, Is.InstanceOf <IntegerConstantExpression>()); Assert.That(((IntegerConstantExpression)result).Value, Is.EqualTo(123)); }