public void Function_main_hello_world() { theString = null; var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("extern fn println(string text);" + "fn main() { println(\"hello world \" + 24); }"); // var ast0 = parser.Parse(tokens); var ast1 = transformer.Transform(ast0); compiler.RegisterExternFunction("println", this.GetType().GetMethod("WriteToString", new[] { typeof(string) }), XzaarScope.Global); // typeof(Console) ... WriteLine var expression = compiler.Compile(ast1) as LambdaXzaarCompiledScript; var lambdaExpression = expression.GetLambdaExpression(); // since we return one function, our output will be an Action (extern ones are not included) var result = expression.Invoke <Action>(); if (result != null) { result(); } Assert.AreEqual("hello world 24", theString); Assert.AreNotEqual(null, lambdaExpression); }
private string FormatCode(string inputCode) { var tokens = new XzaarScriptLexer().Tokenize(inputCode); var ast = new XzaarScriptParser().Parse(tokens); ast = new XzaarScriptTransformer().Transform(ast); var analyzer = new XzaarExpressionAnalyzer(); var analyzed = analyzer.AnalyzeExpression(ast); var codeGenerator = new CodeGeneratorVisitor(); return(codeGenerator.Visit(analyzed.GetExpression())); }
public void Compile_value_mul_10() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("apa * 10"); var ast0 = parser.Parse(tokens); var ast1 = transformer.Transform(ast0); compiler.RegisterVariable <int>("apa"); var expression = compiler.Compile(ast1) as LambdaXzaarCompiledScript; var result = expression.Invoke <int>(10); Assert.AreEqual(10 * 10, result); }
public void Compile_value_lt_1_mul_2() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("(apa == 8) || (apa < (1 * 2))"); var ast0 = parser.Parse(tokens); var ast1 = transformer.Transform(ast0); compiler.RegisterVariable <int>("apa"); var expression = compiler.Compile(ast1) as LambdaXzaarCompiledScript; Assert.IsTrue(expression.Invoke <bool>(0)); Assert.IsTrue(expression.Invoke <bool>(8)); Assert.IsFalse(expression.Invoke <bool>(10)); }
public void TransformTest_a_lt_b_mul_c() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var tokens = lexer.Tokenize("a < (b * c)"); Assert.IsTrue(ValidateTokens(tokens, "a", "<", "(", "b", "*", "c", ")"), $"Failed to validate tokens! Expected: 'a < (b * c)' but got '{string.Join(" ", tokens.Select(s => s.Value))}'"); var ast = parser.Parse(tokens); ast = transformer.Transform(ast); Assert.AreEqual(1, ast.Children.Count); Assert.AreEqual(XzaarNodeTypes.PROGRAM, ast.NodeType); Assert.AreEqual(XzaarNodeTypes.EQUALITY, ast[0].NodeType); }
public void Conditiona_test_op_ordering_1() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("a && b || c"); var ast0 = parser.Parse(tokens); var ast1 = transformer.Transform(ast0); compiler.RegisterVariable <bool>("a"); compiler.RegisterVariable <bool>("b"); compiler.RegisterVariable <bool>("c"); var expression = compiler.Compile(ast1) as LambdaXzaarCompiledScript; Assert.AreEqual("(a, b, c) => ((a And b) Or c)", expression.GetLambdaExpression().ToString()); }
public void TransformTest_if_else_then_without_bodies_semicolon_separated() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("if (a == 0) a = 3; else a = 4;"); var ast = parser.Parse(tokens); ast = transformer.Transform(ast); compiler.RegisterVariable <int>("a"); var expression = compiler.Compile(ast) as LambdaXzaarCompiledScript; var str = expression.GetLambdaExpression().ToString(); Assert.AreEqual("a => IIF((a == 0), (a = 3), (a = 4))", str); }
public void Interpreter_full_chain_complex_3() { var lexer = new XzaarScriptLexer(); var parser = new XzaarScriptParser(); var transformer = new XzaarScriptTransformer(); var compiler = new DotNetXzaarScriptCompiler(); var tokens = lexer.Tokenize("apa == 8 && over == 9000 || apa == 24"); var ast0 = parser.Parse(tokens); var ast1 = transformer.Transform(ast0); compiler.RegisterVariable <int>("apa"); compiler.RegisterVariable <int>("over"); var expression = compiler.Compile(ast1) as LambdaXzaarCompiledScript; Assert.AreEqual("(apa, over) => (((apa == 8) And (over == 9000)) Or (apa == 24))", expression.GetLambdaExpression().ToString()); Assert.IsTrue(expression.Invoke <bool>(8, 9000)); Assert.IsFalse(expression.Invoke <bool>(8, 0)); Assert.IsTrue(expression.Invoke <bool>(24, 0)); Assert.IsFalse(expression.Invoke <bool>(0, 0)); }