public void FunctionCallCallsVisitOnAllParametersPassedTest() { var target = new EvaluateVisitor(); var functionName = RandomGenerator.String(); var functionNameExpr = new IdentifierExpr(functionName); var parameters = new[] { new VarDefinitionStmt(new IdentifierExpr("A"), new IdentifierExpr("INT"), false, new ConstantExpr(1)), new VarDefinitionStmt(new IdentifierExpr("B"), new IdentifierExpr("STRING"), false, new ConstantExpr("TEST")), new VarDefinitionStmt(new IdentifierExpr("C"), new IdentifierExpr("BOOL"), false, new ConstantExpr(true)) }; var values = new Expression[] { new ConstantExpr(1), new ConstantExpr(RandomGenerator.String()), new ConstantExpr(true) }; var returnValue = values[1]; var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, parameters, new ScopeBlockStmt(new[] { new ReturnStmt(returnValue) }), new IdentifierExpr("String")); var expr = new FunctionCallExpr(functionNameExpr, values); _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition)); var actual = target.Visit(expr, _scope); Assert.AreEqual(((ConstantExpr)returnValue).Value, actual.ToObject()); }
public Value Visit(FunctionDefinitionExpr expr, Scope scope) { var value = Value.FromObject(expr); scope.DefineIdentifier(expr.Name, value, value.Type.ToString()); return(new Value(expr)); }
public void BodyPropertyReturnsPassedBody() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); Assert.AreSame(body, target.Body); }
public void BodyPropertyReturnsPassedBody2() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new ScopeBlockStmt(new [] { new NoOpStatement() }); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); Assert.AreSame(body, target.Body); }
public void ReturnTypePropertyReturnsPassedReturnType() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); Assert.AreSame(returnType, target.ReturnType); }
private Value RunFunction(FunctionDefinitionExpr func, Scope scope) { try { func.Body.Accept(this, scope); throw new ReturnStatementExpectedException("Function without a return statement encountered " + func.Name); } catch (ReturnStatementException returnStatement) { return(returnStatement.Value); } }
public void AcceptMethodCallsVisitOnVisitorWithThis() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); var visitor = new Mock <IExpressionVisitor <string, int> >(); target.Accept(visitor.Object, 0); visitor.Verify(x => x.Visit(target, 0), Times.Once); }
public void FunctionCallWithNoReturnStatementTest() { var target = new EvaluateVisitor(); var functionName = RandomGenerator.String(); var functionNameExpr = new IdentifierExpr(functionName); var returnValue = RandomGenerator.String(); var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] { new NoOpStatement() }), new IdentifierExpr("String")); var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]); _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition)); target.Visit(expr, _scope); }
public void AcceptMethodCallsOnlyVisitOnVisitorWithThisAndNoOtherVisitMethods() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); // throw exception is any other methods called other than the PlusExpr overload. var visitor = new Mock <IExpressionVisitor <string, int> >(MockBehavior.Strict); visitor.Setup(x => x.Visit(target, 1234)).Returns(""); target.Accept(visitor.Object, 1234); }
public void FunctionCallTest() { var target = new EvaluateVisitor(); var functionName = RandomGenerator.String(); var functionNameExpr = new IdentifierExpr(functionName); var returnValue = RandomGenerator.String(); var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] { new ReturnStmt(new ConstantExpr(returnValue)) }), new IdentifierExpr("String")); var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]); _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition)); var actual = target.Visit(expr, _scope); Assert.AreEqual(returnValue, actual.ToObject()); }
public void FunctionCallOnUndefinedFuncTest() { var target = new EvaluateVisitor(); var functionName = RandomGenerator.String(); var definedFunctionNameExpr = new IdentifierExpr(functionName); var calledFunctionNameExpr = new IdentifierExpr(functionName + "UNDEFINED"); var returnValue = RandomGenerator.String(); var functionDefinition = new FunctionDefinitionExpr(definedFunctionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] { new ReturnStmt(new ConstantExpr(returnValue)) }), new IdentifierExpr("String")); var expr = new FunctionCallExpr(calledFunctionNameExpr, new Expression[0]); _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition)); target.Visit(expr, _scope); }
public void AcceptMethodCallsOnlyVisitOnVisitorWithThisAndNoOtherVisitMethods() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); // throw exception is any other methods called other than the PlusExpr overload. var visitor = new Mock<IExpressionVisitor<string, int>>(MockBehavior.Strict); visitor.Setup(x => x.Visit(target, 1234)).Returns(""); target.Accept(visitor.Object, 1234); }
public void AcceptMethodCallsVisitOnVisitorWithThis() { var name = new IdentifierExpr(RandomGenerator.String()); var arguments = new VarDefinitionStmt[0]; var body = new NoOpStatement(); var returnType = new IdentifierExpr(RandomGenerator.String()); var target = new FunctionDefinitionExpr(name, arguments, body, returnType); var visitor = new Mock<IExpressionVisitor<string, int>>(); target.Accept(visitor.Object, 0); visitor.Verify(x => x.Visit(target, 0), Times.Once); }
public string Visit(FunctionDefinitionExpr expr, Scope context) { return("function definition"); }
public ValueType Visit(FunctionDefinitionExpr expr, Scope context) { return(ValueType.Function); }
public void FunctionCallTest() { var target = new EvaluateVisitor(); var functionName = RandomGenerator.String(); var functionNameExpr = new IdentifierExpr(functionName); var returnValue = RandomGenerator.String(); var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] {new ReturnStmt(new ConstantExpr(returnValue))}), new IdentifierExpr("String")); var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]); _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition)); var actual = target.Visit(expr, _scope); Assert.AreEqual(returnValue, actual.ToObject()); }