Exemple #1
0
        private CompilerContext IfStatement(CompilerContext context)
        {
            var ifFalse = Enumerable.Empty <Instruction>();

            var expression = context.Node.GetNamedChild("if_condition").GetNamedChild("relational_expression");

            context.Compile(expression);
            var jumpCode = context.JumpExpression;

            var        block  = context.Node.GetNamedChild("block");
            var        child  = context.Push(block);
            CodeStream ifTrue = child.Compiler(child);

            var elseBlock = context.Node.GetNamedChild("else_block");

            if (elseBlock != null)
            {
                block   = elseBlock.GetNamedChild("block");
                child   = context.Push(block);
                ifFalse = child.Compiler(child);
                ifTrue.AsFluent()
                .Jmp.I(ifFalse.Count() + 1);
            }

            context.Code.AsFluent()
            .UnaryOp(jumpCode).I(ifTrue.Count() + 1);

            context.Code += ifTrue;
            context.Code += ifFalse;

            return(context);
        }
Exemple #2
0
            public void showCodeStream()
            {
                CodeStream.show(CodeViewer.editor());
                CodeStream.show(CodeStreamCodeViewer.editor());
                CodeStream.show(CodeStreamTreeView);


                O2Thread.mtaThread(
                    () => {
                    if (ShowGraphInNewWindow)
                    {
                        if (JoinGraphData.isFalse())
                        {
                            CodeStreamGraph = O2Gui.open <Panel>("O2 Ast Engine Graph", 400, 400).add_Graph();
                        }
                        CodeStream.show(CodeStreamGraph);
                    }
                    else
                    {
                        if (JoinGraphData.isFalse())
                        {
                            CodeStreamGraphPanel.clear();
                            CodeStreamGraph = CodeStreamGraphPanel.add_Graph();
                        }
                        CodeStream.show(CodeStreamGraph);
                    }
                    CodeStreamGraphScript.InvocationParameters.add("graph", CodeStreamGraph);
                });
            }
Exemple #3
0
        public void CanComposeSingleInstruction()
        {
            var stream = new CodeStream();
            var i1     = new Instruction();
            var s2     = stream + i1;

            Assert.That(s2, Is.EquivalentTo(new[] { i1 }));
        }
Exemple #4
0
        public void CanComposeSingleInstruction()
        {
            var stream = new CodeStream();
            var i1 = new Instruction();
            var s2 = stream + i1;

            Assert.That(s2, Is.EquivalentTo(new[] { i1 }));
        }
Exemple #5
0
        public void Movi()
        {
            var stream = new CodeStream();

            stream.AsFluent()
            .Mov.RI(Register.A, 0);

            Assert.That(stream.First(), Is.EqualTo(new Instruction(OpCode.Mov).Destination(Register.A).Source(0)).Using(new InstructionComparer()));
        }
Exemple #6
0
        public void WriteExplicitInstruction()
        {
            var stream = new CodeStream
            {
                new Instruction(OpCode.Mov)
            };

            Assert.That(stream.ToArray(), Is.Not.Empty);
        }
Exemple #7
0
        public void WriteInstruction()
        {
            var stream      = new CodeStream();
            var instruction = new Instruction();

            stream.Add(instruction);

            Assert.That(stream.ToArray(), Is.EqualTo(new[] { instruction }));
        }
Exemple #8
0
        public void Movi()
        {
            var stream = new CodeStream();

            stream.AsFluent()
                .Mov.RI(Register.A, 0);

            Assert.That(stream.First(), Is.EqualTo(new Instruction(OpCode.Mov).Destination(Register.A).Source(0)).Using(new InstructionComparer()));
        }
Exemple #9
0
        public void Dispose()
        {
            _relocWriter.Dispose();
            _unwindInfoWriter.Dispose();

            CodeStream.Dispose();
            RelocStream.Dispose();
            UnwindInfoStream.Dispose();
        }
Exemple #10
0
        public void WriteMultipleInstruction()
        {
            var stream = new CodeStream();
            var i1     = new Instruction();
            var i2     = new Instruction();

            stream.Add(i1, i2);

            Assert.That(stream.ToArray(), Is.EqualTo(new[] { i1, i2 }));
        }
Exemple #11
0
        public void CanComposeMultipleInstructions()
        {
            var i1 = new Instruction();
            var i2 = new Instruction();
            var s1 = new CodeStream { i1 };
            var s2 = new CodeStream {i2};
            var s3 = s1 + s2;

            Assert.That(s3, Is.EquivalentTo(new[] { i1, i2 }));
        }
Exemple #12
0
        public void Movr()
        {
            var stream = new CodeStream();

            stream.AsFluent()
                .Mov.RR(Register.A, Register.B);

            var expected = new Instruction(OpCode.Mov).Source(Register.B).Destination(Register.A);
            Assert.That(stream.First(), Is.EqualTo(expected).Using(new InstructionComparer()));
        }
Exemple #13
0
        public void Movr()
        {
            var stream = new CodeStream();

            stream.AsFluent()
            .Mov.RR(Register.A, Register.B);

            var expected = new Instruction(OpCode.Mov).Source(Register.B).Destination(Register.A);

            Assert.That(stream.First(), Is.EqualTo(expected).Using(new InstructionComparer()));
        }
Exemple #14
0
        public void CanComposeMultipleInstructions()
        {
            var i1 = new Instruction();
            var i2 = new Instruction();
            var s1 = new CodeStream {
                i1
            };
            var s2 = new CodeStream {
                i2
            };
            var s3 = s1 + s2;

            Assert.That(s3, Is.EquivalentTo(new[] { i1, i2 }));
        }
Exemple #15
0
        static void Main(string[] args)
        {
            // The preparation:
            CodeStream code_tokens = new CodeStream(File.OpenText(@"..\..\..\Samples\ebnf_ebnf.txt").ReadToEnd());
            //EBNF ebnf = new EBNF();
            Stream ebnfStream = File.OpenRead(@"..\..\..\Samples\compiled_ebnf.persist");
            BinaryFormatter BFReader = new BinaryFormatter();
            Language ebnf = (Language)BFReader.Deserialize(ebnfStream);
            ebnfStream.Flush();
            ebnfStream.Close();

            Scanner SourceScanner = new Scanner(code_tokens, ebnf.ScannerNames, ebnf.ScannerProductions, ebnf.Ignore);
            Parser EBNFLanguageParser = new Parser(new ParserCompiler(@"..\..\..\Samples\compiled_ebnf.persist"),
                                                   SourceScanner,
                                                   ebnf.ParserNames,
                                                   ebnf.ParserProductions);

            // The business:
            EBNFLanguageParser.Parse();

            Console.WriteLine("Done. Press any key to continue...");
            Console.ReadKey();
        }
Exemple #16
0
        protected string CompileAndRunMethod <TDelegate, TParam>(TDelegate action, params TParam[] arguments)
        {
            var asm    = GetAssembly();
            var method = ImportMethod(asm, action);

            var context = new TestContext(asm, arguments.Cast <object>().ToArray());

            context.MethodContexts.Add(CodeStream.Create(context, method));

            try
            {
                //compile
                context = this.AssemblyCompiler.Compile(context) as TestContext;
                Helper.IsNotNull(context);

                //run the compiled exe and return output
                return(Helper.Execute(context.Output));
            }
            catch (Exception e)
            {
                Helper.Break();
                throw;
            }
            finally
            {
                if (context != null)
                {
                    foreach (var file in context.OutputFiles)
                    {
                        File.Delete(file.Filename);
                    }

                    File.Delete(context.Output);
                }
            }
        }
 public IMethodCompilerContext GetContext(IAssemblyCompilerContext context, MethodReference method)
 {
     return(CodeStream.Create(context, method));
 }
Exemple #18
0
 public FluentWriter(CodeStream codeStream)
 {
     _codeStream = codeStream;
 }
Exemple #19
0
 public static FluentWriter AsFluent(this CodeStream stream)
 {
     return(new FluentWriter(stream));
 }
Exemple #20
0
 public FluentWriter(CodeStream codeStream)
 {
     _codeStream = codeStream;
 }
Exemple #21
0
        public void AsEnumerable()
        {
            var stream = new CodeStream();

            Assert.That(stream, Is.InstanceOf <IEnumerable <Instruction> >());
        }
Exemple #22
0
        public void Enumerate()
        {
            var stream = new CodeStream();

            Assert.That(stream.AsEnumerable().ToArray(), Is.InstanceOf <Instruction[]>());
        }
Exemple #23
0
        public void WriteInstruction()
        {
            var stream =new CodeStream();
            var instruction = new Instruction();
            stream.Add(instruction);

            Assert.That(stream.ToArray(), Is.EqualTo(new[] { instruction }));
        }
Exemple #24
0
        public void WriteExplicitInstruction()
        {
            var stream =new CodeStream
            {
                new Instruction(OpCode.Mov)
            };

            Assert.That(stream.ToArray(), Is.Not.Empty);
        }
Exemple #25
0
 public void Enumerate()
 {
     var stream = new CodeStream();
     Assert.That(stream.AsEnumerable().ToArray(), Is.InstanceOf<Instruction[]>());
 }
Exemple #26
0
 public void AsEnumerable()
 {
     var stream = new CodeStream();
     Assert.That(stream, Is.InstanceOf<IEnumerable<Instruction>>());
 }
Exemple #27
0
        public void WriteMultipleInstruction()
        {
            var stream =new CodeStream();
            var i1 = new Instruction();
            var i2 = new Instruction();
            stream.Add(i1, i2);

            Assert.That(stream.ToArray(), Is.EqualTo(new[] { i1, i2 }));
        }