示例#1
0
        public void AddCode(string name, string code)
        {
            var pp = new Preprocessor(context.Report);

            pp.AddCode("machine.h", context.MachineInfo.HeaderCode);
            pp.AddCode(name, code);
            var lexer  = new Lexer(pp);
            var parser = new CParser();

            Add(parser.ParseTranslationUnit(lexer));
        }
示例#2
0
        public InternalFunction(MachineInfo machineInfo, string prototype, InternalFunctionAction action = null)
        {
            var report = new Report(new Report.TextWriterPrinter(Console.Out));
            var parser = new CParser();
            var pp     = new Preprocessor(report);

            pp.AddCode("<Internal>", prototype + ";");
            var      tu       = parser.ParseTranslationUnit(new Lexer(pp));
            Compiler compiler = new Compiler(machineInfo, report);

            compiler.Add(tu);
            var exe = compiler.Compile();

            if (tu.Functions.Count == 0)
            {
                throw new Exception("Failed to parse function prototype: " + prototype);
            }
            var f = tu.Functions[0];

            Name         = f.Name;
            NameContext  = f.NameContext;
            FunctionType = f.FunctionType;

            Action = action;
        }
示例#3
0
        TranslationUnit Parse(string code)
        {
            var report = new Report(new TestPrinter());
            var pp     = new Preprocessor(report);

            pp.AddCode("stdin", code);
            var lexer  = new Lexer(pp);
            var parser = new CParser();

            return(parser.ParseTranslationUnit(lexer, report));
        }
示例#4
0
        CType ParseType(string code)
        {
            var report = new Report(new TestPrinter());
            var pp     = new Preprocessor(report);

            pp.AddCode("stdin", code);
            var lexer  = new Lexer(pp);
            var parser = new CParser();
            var tu     = parser.ParseTranslationUnit(lexer, report);

            return(tu.Variables[0].VariableType);
        }
示例#5
0
        static void Main(string[] args)
        {
            preprocessor = new Preprocessor();

            string output = args[0];
            string input  = args[1];

            string[] code = File.ReadAllLines(input);

            try
            {
                preprocessor.AddCode(code);
                code = preprocessor.ProcessCode();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            FileInfo outputFile = new FileInfo(output);


            File.WriteAllLines(Path.Combine(outputFile.DirectoryName, "prep_" + outputFile.Name + "_.asm"), code);

            try
            {
                assembler = new Assembler(preprocessor.Pragmas);
                assembler.AddSource(code);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }


            try
            {
                assembler.Assemble();
            }
            catch (AssemblyException ex)
            {
                Console.WriteLine("{0}: {1} - {2}", ex.LineNumber, ex.Message, ex.Code);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            File.WriteAllBytes(output, assembler.Binary);
            Console.WriteLine("{0} bytes written", assembler.Length);

            Console.ReadLine();
        }