Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public void Conditiona_test_op_ordering_2()
        {
            var compiler = new DotNetXzaarScriptCompiler();

            compiler.RegisterVariable <bool>("a");
            compiler.RegisterVariable <bool>("b");
            compiler.RegisterVariable <bool>("c");
            compiler.RegisterVariable <bool>("e");

            var expression = compiler.Compile(
                new XzaarScriptTransformer().Transform(
                    new XzaarScriptParser().Parse(
                        new XzaarScriptLexer().Tokenize("a && b || c && e")))) as LambdaXzaarCompiledScript;

            Assert.AreEqual("(a, b, c, e) => ((a And b) Or (c And e))", expression.GetLambdaExpression().ToString());
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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));
        }
Ejemplo n.º 5
0
        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());
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
 public static Delegate CompileToDotNet(this XzaarExpression expression)
 {
     return(DotNetXzaarScriptCompiler.Compile(expression));
 }