Exemplo n.º 1
0
        public CompilerContext(MachineInfo machineInfo, Report report)
        {
            if (machineInfo == null)
            {
                throw new ArgumentNullException("machineInfo");
            }
            if (report == null)
            {
                throw new ArgumentNullException("report");
            }

            MachineInfo = machineInfo;
            Report      = report;
        }
Exemplo n.º 2
0
        public static ColorSpan[] Colorize(string code, MachineInfo machineInfo = null, Report.Printer printer = null)
        {
            var report = new Report(printer);

            var mi = machineInfo ?? new MachineInfo();

            var name = "colorize.cpp";
            var pp   = new Preprocessor(report, passthrough: true);

            pp.AddCode(name, code);

            var lexer = new Lexer(pp);

            var tokens = new List <ColorSpan> ();

            var funcs = new HashSet <string> (mi.InternalFunctions.Where(x => string.IsNullOrEmpty(x.NameContext)).Select(x => x.Name));

            while (true)
            {
                lexer.SkipWhiteSpace();

                var p = pp.CurrentPosition - 1;

                if (!lexer.advance())
                {
                    break;
                }

                var tok = lexer.token();
                var val = lexer.value();

                var e = pp.CurrentPosition - 1;
                if (e < 0 || p + 1 >= code.Length)
                {
                    e = code.Length;
                }
                //Console.WriteLine ($"{e-p}@{p} \"{code.Substring (p, e - p)}\" = {tok} ({val})");
                var color = ColorizeToken(tok, val, funcs);
                tokens.Add(new ColorSpan {
                    Index  = p,
                    Length = e - p,
                    Color  = color,
                });
            }

            return(tokens.ToArray());
        }
Exemplo n.º 3
0
        public static Interpreter.Executable Compile(string code, MachineInfo machineInfo = null, Report.Printer printer = null)
        {
            var report = new Report(printer);

            var mi = machineInfo ?? new MachineInfo();

            var pp = new Preprocessor(report);

            pp.AddCode("machine.h", mi.HeaderCode);
            pp.AddCode(DefaultName, code);
            var lexer  = new Lexer(pp);
            var parser = new CParser();
            var tu     = parser.ParseTranslationUnit(lexer);

            var c = new Interpreter.Compiler(mi, report);

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

            return(exe);
        }
Exemplo n.º 4
0
 public EmitContext(MachineInfo machineInfo, Report report, FunctionDeclaration fdecl = null)
     : base(machineInfo, report)
 {
     FunctionDecl = fdecl;
 }
Exemplo n.º 5
0
 public Compiler(MachineInfo mi, Report report)
     : this(new CompilerContext(mi, report))
 {
 }