} // ReportError static void Abort(string errorMessage) { // reports error and exits ReportError(errorMessage); output.Close(); System.Environment.Exit(1); } // Abort
} // PointerType // +++++++++++++++++++++ Main driver function +++++++++++++++++++++++++++++++ public static void Main(string[] args) { // Open input and output files from command line arguments if (args.Length == 0) { Console.WriteLine("Usage: Declarations FileName"); System.Environment.Exit(1); } input = new InFile(args[0]); output = new OutFile(NewFileName(args[0], ".out")); GetChar(); // Lookahead character do { GetSym(); // Lookahead symbol if (First_Mod2Decl.Contains(sym.kind)) { Mod2Decl(); } else { ReportError("Incorrect starting symbol"); // Start to parse from the goal symbol } } while (sym.kind != EOFSym); // if we get back here everything must have been satisfactory Console.WriteLine((errorCnt == 0) ? "Parsed correctly" : ("End of file reached\nError Count: " + errorCnt)); output.Close(); } // Main
} // ReportError static void Abort(string errorMessage) { // Abandons parsing after issuing error message ReportError(errorMessage); output.Close(); System.Environment.Exit(1); } // Abort
// +++++++++++++++++++++ Main driver function +++++++++++++++++++++++++++++++ public static void Main(string[] args) { // Open input and output files from command line arguments if (args.Length == 0) { Console.WriteLine("Usage: Declarations FileName"); System.Environment.Exit(1); } input = new InFile(args[0]); output = new OutFile(NewFileName(args[0], ".out")); GetChar(); // Lookahead character // To test the scanner we can use a loop like the following: /* do { * GetSym(); // Lookahead symbol * OutFile.StdOut.Write(sym.kind, 3); * OutFile.StdOut.WriteLine(" " + sym.val); // See what we got * } while (sym.kind != EOFSym); */ // After the scanner is debugged we shall substitute this code: GetSym(); // Lookahead symbol Mod2Decl(); // Start to parse from the goal symbol // if we get back here everything must have been satisfactory Console.WriteLine("Parsed correctly"); output.Close(); } // Main
} // PVM.QuickInterpret public static void Interpret(int codeLen, int initSP) { // Interactively opens data and results files. Then interprets the codeLen // instructions stored in mem, with stack pointer initialized to initSP Console.Write("\nTrace execution (y/N/q)? "); char reply = (Console.ReadLine() + " ").ToUpper()[0]; bool traceStack = false, traceHeap = false; if (reply != 'Q') { bool tracing = reply == 'Y'; if (tracing) { Console.Write("\nTrace Stack (y/N)? "); traceStack = (Console.ReadLine() + " ").ToUpper()[0] == 'Y'; Console.Write("\nTrace Heap (y/N)? "); traceHeap = (Console.ReadLine() + " ").ToUpper()[0] == 'Y'; } Console.Write("\nData file [STDIN] ? "); InFile data = new InFile(Console.ReadLine()); Console.Write("\nResults file [STDOUT] ? "); OutFile results = new OutFile(Console.ReadLine()); Emulator(0, codeLen, initSP, data, results, tracing, traceStack, traceHeap); results.Close(); data.Close(); } } // PVM.Interpret
public static void Main(string[] args) { // check that arguments have been supplied if (args.Length != 2) { Console.WriteLine("missing args"); System.Environment.Exit(1); } // attempt to open data file InFile data = new InFile(args[0]); if (data.OpenError()) { Console.WriteLine("cannot open " + args[0]); System.Environment.Exit(1); } // attempt to open results file OutFile results = new OutFile(args[1]); if (results.OpenError()) { Console.WriteLine("cannot open " + args[1]); System.Environment.Exit(1); } // various initializations int total = 0; IntSet mySet = new IntSet(); IntSet smallSet = new IntSet(1, 2, 3, 4, 5); string smallSetStr = smallSet.ToString(); // read and process data file int item = data.ReadInt(); while (!data.NoMoreData()) { total = total + item; if (item > 0) { mySet.Incl(item); } item = data.ReadInt(); } // write various results to output file results.Write("total = "); results.WriteLine(total, 5); results.WriteLine("unique positive numbers " + mySet.ToString()); results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet).ToString()); results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet).ToString()); /* or simply * results.WriteLine("union with " + smallSetStr + " = " + mySet.Union(smallSet)); * results.WriteLine("intersection with " + smallSetStr + " = " + mySet.Intersection(smallSet)); */ results.Close(); } // Main
} // PVM.Interpret public static void ListCode(string fileName, int codeLen) { // Lists the codeLen instructions stored in mem on a named output file int i, j; if (fileName == null) { return; } OutFile codeFile = new OutFile(fileName); /* ------------- The following may be useful for debugging the interpreter * i = 0; * while (i < codeLen) { * codeFile.Write(mem[i], 5); * if ((i + 1) % 15 == 0) codeFile.WriteLine(); * i++; * } * codeFile.WriteLine(); * * ------------- */ i = 0; codeFile.WriteLine("ASSEM\nBEGIN"); while (i < codeLen && mem[i] != PVM.nul) { int o = mem[i] % (PVM.nul + 1); // force in range codeFile.Write(" {"); codeFile.Write(i, 5); codeFile.Write(" } "); codeFile.Write(mnemonics[o], -8); switch (o) // two word opcodes { case PVM.call: case PVM.bfalse: case PVM.btrue: case PVM.brn: case PVM.bze: case PVM.dsp: case PVM.lda: case PVM.ldc: case PVM.ldl: case PVM.stl: case PVM.stlc: i = (i + 1) % memSize; codeFile.Write(mem[i]); break; case PVM.prns: // special case i = (i + 1) % memSize; j = mem[i]; codeFile.Write(" \""); while (mem[j] != 0) { switch (mem[j]) { case '\\': codeFile.Write("\\\\"); break; case '\"': codeFile.Write("\\\""); break; case '\'': codeFile.Write("\\\'"); break; case '\b': codeFile.Write("\\b"); break; case '\t': codeFile.Write("\\t"); break; case '\n': codeFile.Write("\\n"); break; case '\f': codeFile.Write("\\f"); break; case '\r': codeFile.Write("\\r"); break; default: codeFile.Write((char)mem[j]); break; } j--; } codeFile.Write("\""); break; } // switch i = (i + 1) % memSize; codeFile.WriteLine(); } // while (i < codeLen) codeFile.WriteLine("END."); codeFile.Close(); } // PVM.ListCode
//---------------------------------------- static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.Default; Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); // For . instead , in Convert var IsNormalMode = false; var IsRedirectOut = false; var IsDiagnosticTime = false; var watch = new Stopwatch(); string FileName = ""; string OutFileName = ""; for (var i = 0; i < args.Length; i++) { switch (args[i]) { case "-script": if (i + 1 < args.Length) { IsNormalMode = true; FileName = args[i++ + 1]; while (i + 1 < args.Length && args[i + 1][0] != '+' && args[i + 1][0] != '-') { i++; FileName += " " + args[i]; } } break; case "-dtime": IsDiagnosticTime = true; break; case "-out": if (i + 1 < args.Length) { IsRedirectOut = true; OutFileName = args[i++ + 1]; while (i + 1 < args.Length && args[i + 1][0] != '+' && args[i + 1][0] != '-') { i++; OutFileName += " " + args[i]; } } break; } } StreamWriter OutFile = null; if (IsRedirectOut) { OutFile = new StreamWriter(OutFileName, false, Encoding.Default); } if (!IsNormalMode) { Console.WriteLine("--------------------------"); Console.WriteLine("Віртуальна машина CEurope"); Console.WriteLine("--------------------------"); Console.WriteLine("Щоб запустити скрипт передайте в параметри:"); Console.WriteLine("-script <шлях>"); Console.ReadKey(); } else { Interpreter.SendInfoLine("Виконую " + FileName, OutFile); if (File.Exists(FileName)) { using (var script_file = new StreamReader(FileName, Encoding.Default)) { var script = script_file.ReadToEnd(); watch.Start(); Interpreter.Interpret(script, OutFile); watch.Stop(); } } else { Console.ForegroundColor = ConsoleColor.Red; Interpreter.SendInfoLine("Файл не знайдено", OutFile); Console.ReadKey(); return; } if (IsDiagnosticTime) { Interpreter.SendInfoLine(string.Format("Програма завершена за {0} ...", watch.Elapsed), OutFile); } else { Interpreter.SendInfoLine("Програма завершена...", OutFile); } } if (!IsRedirectOut) { Console.ReadKey(); } else { OutFile.Close(); } }
} // PVM.Interpret public static void ListCode(string fileName, int codeLen) { // Lists the codeLen instructions stored in mem on a named output file int i, j, n; if (fileName == null) { return; } OutFile codeFile = new OutFile(fileName); i = 0; codeFile.WriteLine("ASSEM\nBEGIN"); while (i < codeLen) { int o = mem[i] % (PVM.nul + 1); // force in range codeFile.Write(" {"); codeFile.Write(i, 5); codeFile.Write(" } "); codeFile.Write(mnemonics[o], -8); switch (o) { case PVM.brn: case PVM.bze: case PVM.dsp: case PVM.lda: case PVM.ldl: case PVM.stl: case PVM.ldc: i = (i + 1) % memSize; codeFile.Write(mem[i]); break; case PVM.prns: i = (i + 1) % memSize; j = mem[i]; codeFile.Write(" \""); while (mem[j] != 0) { switch (mem[j]) { case '\\': codeFile.Write("\\\\"); break; case '\"': codeFile.Write("\\\""); break; case '\'': codeFile.Write("\\\'"); break; case '\b': codeFile.Write("\\b"); break; case '\t': codeFile.Write("\\t"); break; case '\n': codeFile.Write("\\n"); break; case '\f': codeFile.Write("\\f"); break; case '\r': codeFile.Write("\\r"); break; default: codeFile.Write((char)mem[j]); break; } j--; } codeFile.Write("\""); break; } i = (i + 1) % memSize; codeFile.WriteLine(); } codeFile.WriteLine("END."); codeFile.Close(); } // PVM.ListCode