Ejemplo n.º 1
0
 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());
     results.Close();
 }
Ejemplo n.º 2
0
        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);

              /* ------------- 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) {
            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.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();
        }
Ejemplo n.º 3
0
 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];
       if (reply != 'Q') {
     bool tracing = reply == '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);
     results.Close();
     data.Close();
       }
 }