示例#1
0
        public void GeneratorTest()
        {
            var generator = new Generator(PascalDefinition.ProductionRules);
            var slr1table = generator.Generate(PascalDefinition.NonTerminalKey.Start);
            var driver    = new Slr1Driver(slr1table);
            List <LexicalElement> lexicalElements = new List <LexicalElement>();

            using (var file = new FileStream("test_source1.txt", FileMode.Open)) {
                var reader = new StreamReader(file);
                var l      = new LexerStateMachine(reader);
                l.AdvanceChar();
                LexicalElement le;
                while ((le = l.NextToken()) != null)
                {
                    if (le is LineFeedElement)
                    {
                        continue;
                    }
                    lexicalElements.Add(le);
                }
            }
            var q        = new Queue <LexicalElement>(lexicalElements);
            var treeRoot = driver.Parse(q,
                                        CommonUtils.Closure(
                                            new HashSet <Item>()
            {
                new Item()
                {
                    ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                }
            },
                                            driver.ProductionDictionary), typeof(SNode), new List <ParserConfiguration>());

            Assert.IsInstanceOfType(treeRoot, typeof(SNode));
        }
示例#2
0
 private void InitializeSlr1Table()
 {
     this.generator = new Generator(PascalDefinition.ProductionRules);
     slr1Table      = generator.Generate(PascalDefinition.NonTerminalKey.Start);
     slr1Table.AllowedErrorRecoveryKey.Add(PascalDefinition.NonTerminalKey.Statement, () => new StatementNode(new SyntaxNode[0]));
     slr1Driver = new Slr1Driver(slr1Table);
 }
示例#3
0
        public void LLVMTest2()
        {
            LLVM.LinkInMCJIT();
            LLVM.InitializeX86TargetMC();
            LLVM.InitializeX86Target();
            LLVM.InitializeX86TargetInfo();
            LLVM.InitializeX86AsmParser();
            LLVM.InitializeX86AsmPrinter();
            var mod     = LLVMModuleRef.CreateWithName("Test1");
            var options = LLVMMCJITCompilerOptions.Create();

            options.NoFramePointerElim = 1;
            var engine   = mod.CreateMCJITCompiler(ref options);
            var builder  = mod.Context.CreateBuilder();
            var lexicals = new List <LexicalElement>();
            var globals  = new LLVMSymbolTable(null);

            globals.Add("z", mod.AddGlobal(LLVMTypeRef.Int32, "z"));
            object z      = 0;
            var    handle = GCHandle.Alloc(z, GCHandleType.Pinned);

            engine.AddGlobalMapping(globals["z"], handle.AddrOfPinnedObject());
            using (var fs = new FileStream("D:\\Repos\\PascalCompiler\\SyntaxAnalyzerTest\\test_source2.txt", FileMode.Open)) {
                using (var ss = new StreamReader(fs)) {
                    var l = new LexerStateMachine(ss);
                    l.AdvanceChar();
                    LexicalElement t;
                    while ((t = l.NextToken()) != null)
                    {
                        if (t is LineFeedElement)
                        {
                            continue;
                        }
                        lexicals.Add(t);
                    }
                }
            }
            var        history    = new List <ParserConfiguration>();
            var        exceptions = new List <SyntaxException>();
            SyntaxNode treeRoot;
            var        generator = new Generator(PascalDefinition.ProductionRules);
            var        slr1Table = generator.Generate(PascalDefinition.NonTerminalKey.Start);

            slr1Table.AllowedErrorRecoveryKey.Add(PascalDefinition.NonTerminalKey.Statement, () => new StatementNode(new SyntaxNode[0]));
            var slr1Driver = new Slr1Driver(slr1Table);

            treeRoot = slr1Driver.Parse(new Queue <LexicalElement>(lexicals),
                                        CommonUtils.Closure(
                                            new HashSet <Item>()
            {
                new Item()
                {
                    ProductionRule = PascalDefinition.ProductionRules[0], Cursor = 0
                }
            },
                                            generator.ProductionDict), typeof(SNode), history, exceptions);
            var translator =
                new LLVMTranslator.LLVMTranslator(mod, engine, builder, globals);

            translator.Visit(treeRoot);
            if (!mod.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error))
            {
                Trace.WriteLine(error);
            }
            Trace.WriteLine(mod.PrintToString());
            PM a = engine.GetPointerToGlobal <PM>(translator.func);

            a();
            builder.Dispose();
            engine.Dispose();
            handle.Free();
        }