Esempio n. 1
0
        public void CompileMRuleJoin()
        {
            var mrulejoin = new MRuleJoin
            {
                Name     = "Join rule",
                Children = new List <MRule>
                {
                    new MRuleDef
                    {
                        Name       = "Child rule 1",
                        Condition  = "Vars.Age.Max() < 55",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 1000)", ProjectionType = ProjectionType.Amount
                        },
                    },
                    new MRuleDef
                    {
                        Name       = "Child rule 2",
                        Condition  = "Vars.Age.Max() < 35",
                        Projection = new Projection {
                            Value = "Math.Min(Vars.Amount, 2000)", ProjectionType = ProjectionType.Amount
                        },
                    }
                }
            };

            var compiler = new AstCompiler();

            mrulejoin.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
        }
Esempio n. 2
0
        public static Process CreateFromAst(AstNode n, string[] namespaces, Type[] types)
        {
            var sb = new StringBuilder();

            foreach (var ns in namespaces)
            {
                sb.AppendLine($"using {ns};");
            }
            sb.AppendLine(@"
using System;
using System.Linq;

namespace ce_toy_fx.dynamic
{
    class DynamicProcess
    {
        public static Process GetProcess()
        {
            return
            ");

            var compiler = new AstCompiler();

            n.Accept(compiler);
            sb.AppendLine(compiler.ToString());

            sb.AppendLine(@"
                .CompileToProcess(""Sample process"");
        }
    }
}
");
            return(CreateFromString(sb.ToString(), types));
        }
Esempio n. 3
0
        public void TestCompilesComplexAstToThreeRules()
        {
            var ast      = complexAst();
            var compiler = new AstCompiler();

            var rules = compiler.Compile(ast).ToArray();

            Assert.That(rules.Length, Is.EqualTo(3));
        }
Esempio n. 4
0
        public void TestCompileSimpleAstToCorrectSelector()
        {
            var ast      = simpleAst();
            var compiler = new AstCompiler();

            var results = compiler.Compile(ast).ToArray();

            Assert.That(results[0].Selector.Value, Is.EqualTo("a"));
        }
Esempio n. 5
0
        public void TestCompilesSimpleAstDownToSingleRule()
        {
            var ast      = simpleAst();
            var compiler = new AstCompiler();

            var results = compiler.Compile(ast).ToArray();

            Assert.That(results.Length, Is.EqualTo(1));
        }
Esempio n. 6
0
        public void TestCompilesComplexAstToCorrectSelectors()
        {
            var ast      = complexAst();
            var compiler = new AstCompiler();

            var rules = compiler.Compile(ast).ToArray();

            Assert.That(rules[0].Selector.Value, Is.EqualTo("div"));
            Assert.That(rules[1].Selector.Value, Is.EqualTo("div p"));
            Assert.That(rules[2].Selector.Value, Is.EqualTo("div p a"));
        }
Esempio n. 7
0
        public void TestCompileSimpleAstToCorrectDeclarations()
        {
            var ast      = simpleAst();
            var compiler = new AstCompiler();

            var declarations = compiler.Compile(ast).First().Declarations.ToArray();

            Assert.That(declarations.Length, Is.EqualTo(2));
            Assert.That(declarations[0], Is.EqualTo(new Declaration("color", "red")));
            Assert.That(declarations[1], Is.EqualTo(new Declaration("font-size", "12px")));
        }
Esempio n. 8
0
        public void TestSassNodeWithoutDeclarationsDoesntGetYielded()
        {
            var ast = new FluentAstBuilder().SassNode("a", a =>
            {
            }).ToTree();

            var compiler = new AstCompiler();

            var results = compiler.Compile(ast);

            Assert.That(results.Count(), Is.EqualTo(0));
        }
Esempio n. 9
0
        public void CompileProjectionAmount(ProjectionType type, string expectedProjection)
        {
            var projection = new Projection {
                Value = "x", ProjectionType = type
            };

            var compiler = new AstCompiler();

            projection.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
            Assert.Equal($".Select(Vars => {expectedProjection})", code.TrimEnd());
        }
Esempio n. 10
0
        public void TestCompilesComplexAstToCorrectDeclarations()
        {
            var ast      = complexAst();
            var compiler = new AstCompiler();

            var rules         = compiler.Compile(ast).ToArray();
            var declarations0 = rules[0].Declarations.ToArray();
            var declarations1 = rules[1].Declarations.ToArray();
            var declarations2 = rules[2].Declarations.ToArray();

            Assert.That(declarations0[0], Is.EqualTo(new Declaration("font-size", "12px")));
            Assert.That(declarations0[1], Is.EqualTo(new Declaration("color", "red")));
            Assert.That(declarations0[2], Is.EqualTo(new Declaration("height", "64em")));

            Assert.That(declarations1[0], Is.EqualTo(new Declaration("margin", "0")));

            Assert.That(declarations2[0], Is.EqualTo(new Declaration("font-weight", "bold")));
        }
Esempio n. 11
0
        public void CompileSCase()
        {
            var case1 = new SRuleDef
            {
                Name       = "case 1",
                Condition  = "Vars.Salary > 1000",
                Projection = new Projection {
                    ProjectionType = ProjectionType.Policy
                },
            };

            var case2 = new SRuleDef
            {
                Name       = "case 2",
                Condition  = "Vars.Salary > 1500",
                Projection = new Projection {
                    ProjectionType = ProjectionType.Policy
                },
            };

            var sCaseRule = new SRuleCase
            {
                Name     = "scase rule",
                Variable = "Age",
                Children = new List <(Condition, SRule)>
                {
                    ("Vars.Age < 18", case1),
                    ("Vars.Age >= 18", case2)
                }
            };

            var compiler = new AstCompiler();

            sCaseRule.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
        }
    }
        public byte[] CompileProgram()
        {
            var astCompiler   = new AstCompiler(_programAst);
            var emitter       = astCompiler.CreateEmitter();
            var vmCodeEmitter = new HappyPenguinCodeEmitter();

            emitter.EmitCode(vmCodeEmitter); //This will emit code to the code emitter object
            vmCodeEmitter.Emit(OpCode.Halt); //Add a final halt
            var compiledProgram = vmCodeEmitter.GetEmittedCode();
            var codeEncoder     = new CodeEncoder();



            byte[] compiledProgramBytes;
            using (var outStream = new MemoryStream())
            {
                codeEncoder.EncodeCodeToStream(compiledProgram, outStream);
                compiledProgramBytes = compiledProgramBytes = outStream.ToArray();
            }
            return(compiledProgramBytes);
        }
Esempio n. 13
0
        public void CompilingMRuleDef()
        {
            var mrule = new MRuleDef
            {
                Name       = "Test rule",
                Condition  = "Vars.Age.Max() < 55",
                Projection = new Projection {
                    Value = "Math.Min(Vars.Amount, 1000)", ProjectionType = ProjectionType.Amount
                },
                //VariableReferences = new string[] { "Age", "Amount" }
            };

            var compiler = new AstCompiler();

            mrule.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
        }
Esempio n. 14
0
        public void CompileSRule()
        {
            var liftedrule = new SRuleLift
            {
                Child = new SRuleJoin
                {
                    Name     = "Srule group",
                    Children = new List <SRule>
                    {
                        new SRuleDef
                        {
                            Name       = "srule 1",
                            Condition  = "Vars.Salary < 1000 && Vars.Age < 25",
                            Projection = new Projection {
                                ProjectionType = ProjectionType.Policy
                            },
                        },
                        new SRuleDef
                        {
                            Name       = "srule 2",
                            Condition  = "Vars.Amount < 1000",
                            Projection = new Projection {
                                ProjectionType = ProjectionType.Policy
                            },
                        }
                    }
                }
            };

            var compiler = new AstCompiler();

            liftedrule.Accept(compiler);

            var code = compiler.ToString();

            output.WriteLine(code);
            Assert.NotNull(code);
        }
Esempio n. 15
0
        public static void Main()
        {
            CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;

            var vm = new VM();

            vm.SetVariable("pi", 3.14);
            vm.SetVariable("cwd", Directory.GetCurrentDirectory());

            vm.RegisterCommand(new EchoCommand());
            vm.RegisterCommand(new SetCommand());
            vm.RegisterCommand(new VarsCommand());
            vm.RegisterCommand(new HelpCommand());
            vm.RegisterCommand(new PwdCommand());
            vm.RegisterCommand(new CdCommand());
            vm.RegisterCommand(new LsCommand());
            vm.RegisterCommand(new GetenvCommand());
            vm.RegisterCommand(new EnvCommand());

            bool   showAst = false;
            bool   throwOnRuntimeExceptions = false;
            string src = "";

            Console.WriteLine("Welcome to MeeShell, type help then enter for help, .q then enter to quit !");
            while (true)
            {
                if (src.Length == 0)
                {
                    Console.Write("> ");
                }
                else
                {
                    Console.Write(". ");
                }
                var line = Console.ReadLine();

                if (line == ".q")
                {
                    break;
                }
                if (line == ".c")
                {
                    src = "";
                    continue;
                }
                if (line == ".v")
                {
                    showAst = !showAst;
                    continue;
                }
                if (line == ".e")
                {
                    throwOnRuntimeExceptions = !throwOnRuntimeExceptions;
                    continue;
                }

                var astCompiler = new AstCompiler();
                List <Mir.IOperation> opcodes;
                try
                {
                    if (src?.Length == 0)
                    {
                        src = line;
                    }
                    else
                    {
                        src += "\n";
                        src += line;
                    }

                    var result = ParseProgram.Parse(new Parser.ParserContext(), src);
                    if (!result.Success)
                    {
                        if (!(result.Expected.Length == 1 && result.Expected[0] == "End of Source"))
                        {
                            src = "";
                            Console.WriteLine("Parse failed, expected: " + string.Join(", ", result.Expected));
                        }
                        continue;
                    }
                    src = "";
                    if (showAst)
                    {
                        Utils.DumpAst(result.Value);
                    }
                    opcodes = astCompiler.Compile(result.Value);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Compilation exception: " + ex.Message);
                    continue;
                }

                try
                {
                    vm.Run(opcodes);
                    var result = vm.Pop();
                    if (result == null)
                    {
                        Console.WriteLine("-> null");
                    }
                    else
                    {
                        Console.WriteLine("-> " + result.GetType());
                        if (result.Internal is string s)
                        {
                            Console.WriteLine(s);
                        }
                        else if (result.Internal is IDictionary dictionary)
                        {
                            foreach (var key in dictionary.Keys)
                            {
                                Console.WriteLine(key + ": " + dictionary[key]);
                            }
                        }
                        else if (result.Internal is IEnumerable enumerable)
                        {
                            foreach (var element in enumerable)
                            {
                                Console.WriteLine(element);
                            }
                        }
                        else
                        {
                            Console.WriteLine(result.Internal);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Runtime exception: " + ex.Message);
                    if (throwOnRuntimeExceptions)
                    {
                        throw;
                    }
                }
            }
        }