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, context.Report)); }
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); }
public TranslationUnit ParseTranslationUnit(string code, Report report) { if (report == null) { throw new ArgumentNullException("report"); } var pp = new Preprocessor(report); pp.AddCode("stdin", code); var lex = new Lexer(pp); return(ParseTranslationUnit(lex, report)); }
public InternalFunction(string prototype, InternalFunctionAction action = null) { var report = new Report(new TextWriterReportPrinter(Console.Out)); var parser = new CParser(); var pp = new Preprocessor(report); pp.AddCode("<Internal>", prototype + ";"); var tu = parser.ParseTranslationUnit(new Lexer(pp), report); var f = tu.Functions[0]; Name = f.Name; FunctionType = f.FunctionType; Action = action; }
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()); }