public static void compile(string srcfile, string asmfile, string objfile, string exefile) { c = new compiler(null, srcfile, 1); List <string> asm = c.generateAssembly(); string asmText = ""; for (int i = 0; i < asm.Count; i++) //convert to string array { asmText += asm[i]; if (i != asm.Count - 1) { asmText += "\n"; } } File.WriteAllText(asmfile, asmText); ExeTools.ExeTools.Assemble(asmfile, objfile); ExeTools.ExeTools.Link(objfile, exefile); }
public static void makelr0dfa(string gFile) { c = new compiler(gFile, null, 1); c.dumpLR_DFA(); }
public static void interpret(string gFile, string iFile) { c = new compiler(gFile, iFile); c.Interpret(); }
public static Dictionary <string, Dictionary <string, HashSet <string> > > computeLLTable(string gFile) { c = new compiler(gFile); return(c.getTable()); }
public static TreeNode compile(string gFile, string iFile) { c = new compiler(gFile, iFile, 1); return(c.getTree()); }
public static HashSet <string> computeNullables(string gFile) { compiler c = new compiler(gFile); return(c.getNullables()); }
public static Dictionary <string, HashSet <string> > computeFollow(string gFile) { c = new compiler(gFile); return(c.getFollows()); }
public static Dictionary <string, HashSet <string> > computeFirsts(string gFile) { compiler c = new compiler(gFile); return(c.getFirsts()); }
public static State makelr0dfa(string gFile) { c = new compiler(gFile, null, 1); c.dumpLR_DFA(); return(c.getLR0_DFA()); }
/// <summary>application entry point</summary> static int Main(string[] args) { string destHexFilename = null; string destVerilogFilename = null; string destLstFilename = null; string destBootBinFilename = null; try { //args = new string[] { "../../../main.txt" }; Console.WriteLine("DEBUG: hardcoded args"); Dictionary <string, UInt32> defines = new Dictionary <string, uint>() { { "#MEMSIZE_BYTES(", 8192 * 4 }, { "#BASEADDR_CODE(", 0 }, { "#BASEADDR_DATA(", 4000 } }; // 0x1000 if (args.Length < 1) { throw new Exception("no input files"); } // === preprocess all input files into token list === List <token> tokens = new List <token>(); foreach (string fname_ in args) { string fname = fname_; string dir = System.IO.Path.GetDirectoryName(fname); string fnameNoDir = System.IO.Path.GetFileName(fname); string dirOut = System.IO.Path.Combine(dir, "out"); fname = System.IO.Path.Combine(dir, fnameNoDir); string fnameOut = System.IO.Path.Combine(dirOut, fnameNoDir); // === first source file determines the name of the output files === if (destHexFilename == null) { destHexFilename = System.IO.Path.ChangeExtension(fnameOut, "hex"); if (destHexFilename == fname) { throw new Exception(".hex is not permitted as input file extension"); } destVerilogFilename = System.IO.Path.ChangeExtension(fnameOut, "v"); if (destVerilogFilename == fname) { throw new Exception(".v is not permitted as input file extension"); } destLstFilename = System.IO.Path.ChangeExtension(fnameOut, "lst"); if (destLstFilename == fname) { throw new Exception(".lst is not permitted as input file extension"); } destBootBinFilename = System.IO.Path.ChangeExtension(fnameOut, "bootBin"); if (destBootBinFilename == fname) { throw new Exception(".bootBin is not permitted as input file extension"); } // === create output directory === System.IO.Directory.CreateDirectory(dirOut); // === prevent stale output by deleting first === System.IO.File.Delete(destHexFilename); System.IO.File.Delete(destVerilogFilename); System.IO.File.Delete(destLstFilename); System.IO.File.Delete(destBootBinFilename); } List <string> filerefs = new List <string>(); string content = System.IO.File.ReadAllText(fname); filerefs.Add(fname); HashSet <string> includeOnce = new HashSet <string>(); preprocessor.parse(content, filerefs, dir, tokens, defines, includeOnce); } // === compile === compiler comp = new compiler(tokens, baseAddrCode_bytes: defines["#BASEADDR_CODE("], baseAddrData_bytes: defines["#BASEADDR_DATA("], memSize_bytes: defines["#MEMSIZE_BYTES("]); // === write output files === comp.dumpHex(destHexFilename); comp.dumpVerilog(destVerilogFilename); comp.dumpLst(destLstFilename); comp.dumpBootBin(destBootBinFilename); Environment.Exit(0); return(0); // EXIT_SUCCESS } catch (Exception e) { Console.Error.WriteLine("Error: " + e.Message); Environment.Exit(-1); return(-1); // EXIT_FAILURE } }
private void run(bool debug) { string fullSource = source; //very crude selective use of library functions if (source.Contains("while(")) { fullSource += whileFunction; } if (source.Contains("if(")) { fullSource += ifFunction; } if (source.Contains("for(")) { fullSource += forFunction; } if (source.Contains("loop(")) { fullSource += loopFunction; } var p = new parser5(fullSource); // var p = new parser(fullSource); astNode tree; try { tree = p.parse(); } catch (Exception parseErr) { MessageBox.Show("Parse Error " + parseErr.Message + " Line " + p.line); return; } if (debug) { displaySyntaxTree(tree); } compiler.fixDotNotation(tree); if (debug) { displaySyntaxTree(tree); } compiler comp = new compiler(); //get functions from connected devices var ExternalFunctions = new functionList(); dynamic RX = PC.findNode("RX"); buildExternalFunctions(RX, ExternalFunctions); dynamic IMU = PC.findNode("IMU"); buildExternalFunctions(IMU, ExternalFunctions); dynamic pilot = PC.findNode("Pilot"); buildExternalFunctions(pilot, ExternalFunctions); dynamic TX = PC.findNode("TX"); buildExternalFunctions(TX, ExternalFunctions); if (PC.node.parameterCount == 0) { PC.node.properties.Add("say", new ccParameter(0, "say", 7, 0, PC.node)); PC.node.parameterCount++; } buildExternalFunctions(PC, ExternalFunctions); int[] bbc; try { bbc = comp.compile(tree, ExternalFunctions); } catch (Exception ce) { string msg = "Compilation Error " + ce.Message; foreach (var k in ce.Data.Keys) { msg += " " + k.ToString() + " " + ce.Data[k].ToString(); } MessageBox.Show(msg); return; } if (debug) { comp.list(bbc); Console.Out.WriteLine(comp.listMethods()); } if (ComboBoxTarget.SelectedItem.ToString() != "PC") { //run on receiver if (pilot != null) { try { //stop existing script running pilot.enabled = false; //send to script array on rx pilot.script = bbc; //int[] test = pilot.script; Console.Out.WriteLine("Script Uploaded"); //run code pilot.enabled = true; } catch (Exception RXrunEx) { MessageBox.Show("Failed to run exception " + RXrunEx.Message); } } else { Console.Out.WriteLine("Pilot node not found"); } } if (ComboBoxTarget.SelectedItem.ToString() == "PC") { // run script on pc virtual machine int[] inputs = new int[] { 0, 1, 2, 3, 4 }; try { comp.run(inputs, debug, new List <object> { PC, TX, RX, pilot, IMU }); } catch (Exception runEx) { MessageBox.Show("Interp exception " + runEx.Message); } } // emitBBC(bbc); }
/// <summary> /// pass a grammarFile, inputFile, and specify a compilerType if you dont want it to be LL(0) by default. /// compilerTypes : {(0, LL(0)), (1, SLR(1)), (2, GLR)} /// </summary> /// <param name="gFile"></param> /// <param name="iFile"></param> /// <param name="compilerType"></param> /// <returns></returns> public static TreeNode parseTree(string gFile, string iFile, int compilerType = 0) { c = new compiler(gFile, iFile, compilerType); return(c.getTree()); }