public void CanReturnSimpleValue() { Lambda lambda = Compiler.CompileFunction( new FunctionDefinition { ClassMethod = false, Name = "testFunc", ReturnType = new TypeSyntax { TypeName = new NameExpression { Name = "string" } }, Parameters = new ParameterDefinition[] { }, Body = new BlockStatement { Statements = new Statement[] { new ReturnStatement { Expression = new StringConstant { Value = "Test" } } } } } ); Assert.Equal(RedwoodType.GetForCSharpType(typeof(string)), lambda.ReturnType); Assert.Equal("Test", lambda.Run()); }
private async Task <Lambda> MakeLambda(string code) { Parser parser = new Parser( new StreamReader( new MemoryStream( Encoding.UTF8.GetBytes(code) ) ) ); FunctionDefinition function = await parser.ParseFunctionDefinition(); Assert.NotNull(function); return(Compiler.CompileFunction(function)); }
public void CanReturnValueFromParameter() { Lambda lambda = Compiler.CompileFunction( new FunctionDefinition { ClassMethod = false, Name = "testFunc", ReturnType = new TypeSyntax { TypeName = new NameExpression { Name = "string" } }, Parameters = new ParameterDefinition[] { new ParameterDefinition { Type = new TypeSyntax { TypeName = new NameExpression { Name = "string" }, }, Name = "paramA" } }, Body = new BlockStatement { Statements = new Statement[] { new ReturnStatement { Expression = new NameExpression { Name = "paramA" } } } } } ); Assert.Equal("TestABC", lambda.Run("TestABC")); Assert.Equal("Test123", lambda.Run("Test123")); }
public void CanCallAnotherFunction() { FunctionDefinition innerFunction = new FunctionDefinition { ClassMethod = false, Name = "innerTestFunc", ReturnType = new TypeSyntax { TypeName = new NameExpression { Name = "string" } }, Parameters = new ParameterDefinition[] { new ParameterDefinition { Type = new TypeSyntax { TypeName = new NameExpression { Name = "string" }, }, Name = "paramA" } }, Body = new BlockStatement { Statements = new Statement[] { new ReturnStatement { Expression = new NameExpression { Name = "paramA" } } } } }; Lambda lambda = Compiler.CompileFunction( new FunctionDefinition { ClassMethod = false, Name = "testFunc", ReturnType = new TypeSyntax { TypeName = new NameExpression { Name = "string" } }, Parameters = new ParameterDefinition[] { }, Body = new BlockStatement { Statements = new Statement[] { innerFunction, new ReturnStatement { Expression = new CallExpression { FunctionName = new NameExpression { Name = "innerTestFunc" }, Arguments = new Expression[] { new StringConstant { Value = "Test" } } } } } } } ); Assert.Equal("Test", lambda.Run()); }