Ejemplo n.º 1
0
        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());
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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());
            }
        }