} // CodeGen.WriteLine public static void WriteString(string str, int offSet) { // Generates code to output a string str stored at known location int l = str.Length, first = PVM.GetHp() + offSet + 1; Console.Write(first);/* * if (stkTop <= first + l + 1) { * Parser.SemError("program too long"); generatingCode = false; * return; * } * for (int i = 0; i < l; i++) { * first++; PVM.mem[first] = str[i]; * } * first++; PVM.mem[first] = 0; * Emit(PVM.prns); Emit(PVM.GetHp() + offSet);*/ } // CodeGen.WriteString:
} // CodeGen.TwoWord public static void Branch(string mnemonic, Label adr) { // Inline assembly of two word branch style instruction with Label operand Emit(PVM.OpCode(mnemonic)); Emit(adr.Address()); } // CodeGen.Branch
} // CodeGen.OneWord public static void TwoWord(string mnemonic, int adr) { // Inline assembly of two word instruction with integer operand Emit(PVM.OpCode(mnemonic)); Emit(adr); } // CodeGen.TwoWord
} // CodeGen.GetInitSP public static void OneWord(string mnemonic) { // Inline assembly of one word instruction with no operand Emit(PVM.OpCode(mnemonic)); } // CodeGen.OneWord
public static void Main(string[] args) { bool mergeErrors = false, execution = true, immediate = false; string inputName = null; // ------------------------ process command line parameters: Console.WriteLine("Parva compiler 2.2017 October"); for (int i = 0; i < args.Length; i++) { if (args[i].ToLower().Equals("-l")) { mergeErrors = true; } else if (args[i].ToLower().Equals("-d")) { Parser.debug = true; } else if (args[i].ToLower().Equals("-n")) { execution = false; } else if (args[i].ToLower().Equals("-g")) { immediate = true; } else { inputName = args[i]; } } if (inputName == null) { Console.WriteLine("No input file specified"); Console.WriteLine("Usage: Parva [-l] [-d] [-n] [-g] source.pav"); Console.WriteLine("-l directs source listing to listing.txt"); Console.WriteLine("-d turns on debug mode"); Console.WriteLine("-n no execution after compilation"); Console.WriteLine("-g execute immediately after compilation (StdIn/StdOut)"); System.Environment.Exit(1); } // ------------------------ parser and scanner initialization int pos = inputName.LastIndexOf('/'); if (pos < 0) { pos = inputName.LastIndexOf('\\'); } string dir = inputName.Substring(0, pos + 1); Scanner.Init(inputName); Errors.Init(inputName, dir, mergeErrors); PVM.Init(); Table.Init(); // ------------------------ compilation Parser.Parse(); Errors.Summarize(); // ------------------------ interpretation bool assembledOK = Parser.Successful(); int initSP = CodeGen.GetInitSP(); string codeName = newFileName(inputName, ".cod"); int codeLength = CodeGen.GetCodeLength(); PVM.ListCode(codeName, codeLength); if (!assembledOK || codeLength == 0) { Console.WriteLine("Unable to interpret code"); System.Environment.Exit(1); } else if (!execution) { Console.WriteLine("\nCompiled: exiting with no execution requested"); System.Environment.Exit(1); } else { if (immediate) { PVM.QuickInterpret(codeLength, initSP); } char reply = 'n'; do { Console.Write("\n\nInterpret [y/N]? "); reply = (Console.ReadLine() + " ").ToUpper()[0]; if (reply == 'Y') { PVM.Interpret(codeLength, initSP); } } while (reply == 'Y'); } } // Main
} // CodeGen.Branch public static void MaxMin(string mnemonic, int amount){ if(amount != 1) for(int i = 0; i < amount - 1; i++) Emit(PVM.OpCode(mnemonic)); }