コード例 #1
0
ファイル: Main.cs プロジェクト: Brumous101/Compiler-Design
        public static void Main(string[] args)
        {
            Syms.init("ssupl.tokens");

            string gdata;

            using (var r = new StreamReader("terminals.txt"))
            {
                gdata = r.ReadToEnd();
            }
            var tokenizer = new Tokenizer(gdata);

            string idata;

            using (var r = new StreamReader("input.txt"))
            {
                idata = r.ReadToEnd();
            }


            var rex = new Regex(@"\n[ \t]+([^\n]+)");

            idata  = rex.Replace(idata, " $1");
            idata += "\n";

            tokenizer.setInput(idata);
            IList <IToken> tokens = new List <IToken>();

            while (true)
            {
                Token t = tokenizer.next();
                if (t.Symbol == "$")
                {
                    break;
                }
                CommonToken T = new CommonToken(Syms.stringToInt[t.Symbol], t.Lexeme);
                T.Line = t.Line;
                tokens.Add(T);
            }



            var antlrtokenizer = new BufferedTokenStream(new ListTokenSource(tokens));
            var parser         = new ssuplParser(antlrtokenizer);

            parser.BuildParseTree = true;
            parser.ErrorHandler   = new BailErrorStrategy();
            var antlrroot = parser.start();

            var listener = new CodeGenerator();
            var walker   = new ParseTreeWalker();

            walker.Walk(listener, antlrroot);
            var allcode = new ASM(
                "default rel",
                "section .text",
                "global main",
                "main:",
                listener.code.Get(antlrroot),
                "section .data");

            using (var w = new StreamWriter("out.asm"))
            {
                w.Write(allcode.ToString());
            }
        }
コード例 #2
0
ファイル: Main.cs プロジェクト: Brumous101/Compiler-Design
        public static void Main(string[] args)
        {
            Syms.init("ssupl.tokens");

            string gdata;

            using (var r = new StreamReader("terminals.txt"))
            {
                gdata = r.ReadToEnd();
            }
            var tokenizer = new Tokenizer(gdata);

            string idata;

            using (var r = new StreamReader("input.txt"))
            {
                idata = r.ReadToEnd();
            }

            tokenizer.setInput(idata);
            IList <IToken> tokens = new List <IToken>();

            while (true)
            {
                Token t = tokenizer.next();
                //Console.WriteLine("token: " + t);
                if (t.Symbol == "$")
                {
                    break;
                }
                CommonToken T = new CommonToken(Syms.stringToInt[t.Symbol], t.Lexeme);
                T.Line = t.Line;
                tokens.Add(T);
            }


            var antlrtokenizer = new BufferedTokenStream(new ListTokenSource(tokens));
            var parser         = new ssuplParser(antlrtokenizer);

            parser.BuildParseTree = true;
            parser.ErrorHandler   = new BailErrorStrategy();
            var antlrroot = parser.start();

            var listener = new CodeGenerator();
            var walker   = new ParseTreeWalker();

            walker.Walk(listener, antlrroot);
            var allcode = new ASM(
                "default rel",
                "section .text",
                "global main",
                "main:",
                listener.code.Get(antlrroot),
                "section .data");

            //This makes the functions and actual program
            using (var w = new StreamWriter("out.asm"))
            {
                //Console.WriteLine("----------------------------");
                //Console.WriteLine(allcode);
                //Console.WriteLine("----------------------------");
                w.Write(allcode.ToString());
                foreach (var literal in listener.stringPool.Keys)
                {
                    w.WriteLine("; " + literal.Replace("\n", "\\n"));
                    w.WriteLine(listener.stringPool[literal].address + ":");
                    w.Write("db ");
                    byte[] b = Encoding.ASCII.GetBytes(literal);
                    for (int i = 0; i < literal.Length; ++i)
                    {
                        w.Write(b[i]);
                        w.Write(",");
                    }
                    w.WriteLine("0");
                }
            }

            //This makes variables possible in the global data after the program
            //Literally gets append to the bottom of the out.asm file
            var symtable = listener.symtable.table;

            foreach (var sym in symtable)
            {
                //Need dq to have 0,0,0,0 for vec4
                if (sym.Value.type == VarType.VEC4)
                {
                    var globaldata = new ASM(
                        $"{sym.Value.location}:",
                        "dq 0",
                        "dq 0",
                        "dq 0",
                        "dq 0");

                    using (var appendglobals = File.AppendText("out.asm"))
                    {
                        appendglobals.Write(globaldata.ToString());
                    }
                }
                else
                {
                    var globaldata = new ASM(
                        $"{sym.Value.location}:",
                        "dq 0");
                    using (var appendglobals = File.AppendText("out.asm"))
                    {
                        appendglobals.Write(globaldata.ToString());
                    }
                }
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Syms.init("ssupl.tokens");

            string gdata;

            using (var r = new StreamReader("terminals.txt"))
            {
                gdata = r.ReadToEnd();
            }
            gdata = gdata.Replace("\r", "\n");
            var tokenizer = new Tokenizer(gdata);

            //existing code
            string idata;

            using (var r = new StreamReader("input.txt"))
            {
                idata = r.ReadToEnd();
            }

            //new code

            //var rex = new Regex(@"\r");

            idata = idata.Replace("\r", "");
            var rex = new Regex(@"\n[ \t]+([^\n]+)");

            //need one leading space in replacement

            idata = rex.Replace(idata, " $1");

            idata += "\n";      //make sure file ends with newline

            tokenizer.setInput(idata);
            IList <IToken> tokens = new List <IToken>();

            while (true)
            {
                Token t = tokenizer.next();
                if (t.Symbol == "$")
                {
                    break;  //at end
                }
                //CommonToken is defined in the ANTLR runtime
                CommonToken T = new CommonToken(Syms.stringToInt[t.Symbol], t.Lexeme);
                T.Line = t.Line;
                tokens.Add(T);
            }

            var antlrtokenizer = new BufferedTokenStream(new ListTokenSource(tokens));
            var parser         = new ssuplParser(antlrtokenizer);

            parser.BuildParseTree = true;
            //optional: parser.ErrorHandler = new BailErrorStrategy ();
            //'start' should be the name of the grammar's start symbol
            var antlrroot = parser.start();

            var listener = new CodeGenerator();
            var walker   = new ParseTreeWalker();

            walker.Walk(listener, antlrroot);
            //Console.WriteLine(listener.code.Get(antlrroot).ToString());
            var allcode = new ASM(
                "default rel",
                "section .text",
                "global main",
                "main:",
                listener.code.Get(antlrroot).ToString(),
                "ret",
                "section .data"
                );

            Console.WriteLine(allcode.ToString());
            using (var w = new StreamWriter("out.asm"))
            {
                w.Write(allcode.ToString());
            }
            //Console.ReadLine();
        }