コード例 #1
0
        public static ASM operator +(ASM a1, ASM a2)
        {
            ASM res = new ASM();

            res.instructions.AddRange(a1.instructions);
            res.instructions.AddRange(a2.instructions);
            return(res);
        }
コード例 #2
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();
        }