home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / MyC / Src / Io.cs < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.6 KB  |  396 lines

  1. namespace MyC
  2. {
  3. using System;
  4. using System.Text;
  5. using System.IO;
  6. #if EXEGEN
  7. using System.Reflection;
  8. #endif
  9.  
  10. class Io
  11. {
  12. private FileStream ifile;
  13. private StreamReader rfile;
  14.  
  15. private FileStream lst_ofile;
  16. private StreamWriter lst_wfile;
  17.  
  18. private static String ifilename;
  19. private static String ofilename;
  20. private static String lst_ofilename;
  21. private static String classname;
  22.  
  23. public static bool gendebug = false;
  24. public static bool genexe = true;    // default to generating exe's
  25. public static bool gendll = false;
  26. public static bool genlist = false;
  27. public static string genpath = ".";    // default output directory to current dir
  28.  
  29. private String[] args;
  30. private char[] ibuf;
  31.  
  32. private int ibufidx;
  33. private int ibufread;
  34. private bool _eof;
  35.  
  36. /* variable declarations */
  37. private char look;    /* lookahead character */
  38. private StringBuilder buf;        /* buffer for comment tracking */
  39. int bufline = 0;        // current line matching buffer
  40. int curline = 0;        // current comment line number
  41.  
  42. public static string GetClassname()
  43.   {
  44.   return classname;
  45.   }
  46.  
  47. public String GetInputFilename()
  48.   {
  49.   return ifilename;
  50.   }
  51. /* read new character from input stream */
  52. public void ReadChar()
  53.   {
  54.   if (_eof)            // if already eof, nothing to do here
  55.     return;
  56.   if (ibuf == null || ibufidx >= MyC.MAXBUF)
  57.     {
  58.     ibuf = new char[MyC.MAXBUF];
  59.     _eof = false;
  60.     ibufread = rfile.Read(ibuf, 0, MyC.MAXBUF);
  61.     ibufidx = 0;
  62.     if (buf == null)
  63.       buf = new StringBuilder(MyC.MAXSTR);
  64.     }
  65.   look = ibuf[ibufidx++];
  66.   if (ibufread < MyC.MAXBUF && ibufidx > ibufread)
  67.     _eof = true;
  68.  
  69.   /*
  70.    * track the read characters
  71.    */
  72.   buf.Append(look);
  73.   if (look == '\n')
  74.     bufline++;
  75.   }
  76.  
  77. public char getNextChar()
  78.   {
  79.   return look;
  80.   }
  81.  
  82. public void Abort(string s)
  83.   {
  84.   StringBuilder sb = new StringBuilder();
  85.   sb.Append(ifilename);
  86.   sb.Append("(");
  87.   sb.Append(curline+1);
  88.   sb.Append(") : error M0000: ");
  89.   sb.Append(s);
  90.   Console.WriteLine(sb.ToString());
  91.   throw new ApplicationException("Aborting compilation");
  92.   }
  93.  
  94. public static void ICE(string s) // internal compiler error
  95.   {
  96.   StringBuilder sb = new StringBuilder();
  97.   sb.Append(ifilename);
  98.   sb.Append("(0) : error M9999: Internal Compiler Error: ");
  99.   sb.Append(s);
  100.   Console.WriteLine(sb.ToString());
  101.   throw new ApplicationException("Aborting compilation");
  102.   }
  103.  
  104. void ParseArgs()
  105.   {
  106.   int i = 1;
  107.  
  108.   if (args.Length < 2)
  109.     {
  110.     Abort("myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n");
  111.     }
  112.  
  113.   while (true)
  114.     {
  115.     if (args[i][0] != '/')
  116.       break;
  117.     if (args[i].Equals("/?"))
  118.       {
  119.       Console.WriteLine("Compiler options:\n  myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n");
  120.       Environment.Exit(1);
  121.       }
  122.     if (args[i].Equals("/debug"))
  123.       {
  124.       gendebug = true;
  125.       i++;
  126.       continue;
  127.       }
  128.     if (args[i].Equals("/nodebug"))
  129.       {
  130.       gendebug = false;
  131.       i++;
  132.       continue;
  133.       }
  134.     if (args[i].Equals("/exe"))
  135.       {
  136.       genexe = true;
  137.       gendll = false;
  138.       i++;
  139.       continue;
  140.       }
  141.     if (args[i].Equals("/dll"))
  142.       {
  143.       gendll = true;
  144.       genexe = false;
  145.       i++;
  146.       continue;
  147.       }
  148.     if (args[i].Equals("/list"))
  149.       {
  150.       genlist = true;
  151.       i++;
  152.       continue;
  153.       }
  154.     if (args[i].Length > 8 && args[i].Substring(0,8).Equals("/outdir:"))
  155.       {
  156.       genpath = args[i].Substring(8);
  157.       i++;
  158.       continue;
  159.       }
  160.     /*
  161.      * exit if no switch matched
  162.      */
  163.     Abort("Unmatched switch = '"+args[i]+"'\nArguments are:\nmyc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n");
  164.     }
  165.  
  166.   if (args.Length-i != 1)
  167.     {
  168.     Abort("myc [/debug] [/nodebug] [/list] [/dll] [/exe] [/outdir:path] filename.myc\n");
  169.     }
  170.   ifilename = args[args.Length-1]; // filename is last
  171.   }
  172.  
  173. public Io(String[] a)
  174.   {
  175.   int i;
  176.  
  177.   args = a;
  178.   ParseArgs();
  179.  
  180.   ifile = new FileStream(ifilename, FileMode.Open,
  181.              FileAccess.Read, FileShare.Read, 8192);
  182.   if (ifile == null)
  183.     {
  184.     Abort("Could not open file '"+ifilename+"'\n");
  185.     }
  186.   rfile = new StreamReader(ifile); // open up a stream for reading
  187.  
  188.   /*
  189.    * for now we are going to create a default class using the filename
  190.    */
  191.   i = ifilename.LastIndexOf('.');
  192.   if (i < 0)
  193.     Abort("Bad filename '"+ifilename+"'");
  194.   int j = ifilename.LastIndexOf('\\');
  195.   if (j < 0)
  196.     j = 0;
  197.   else
  198.     j++;
  199.  
  200.   classname = ifilename.Substring(j,i-j);
  201.   if (genexe)
  202.     ofilename = classname+".exe";
  203.   if (gendll)
  204.     ofilename = classname+".dll";
  205.   if (genlist)
  206.     {
  207.     lst_ofilename = classname+".lst";
  208.     lst_ofile = new FileStream(lst_ofilename, FileMode.Create,
  209.              FileAccess.Write, FileShare.Write, 8192);
  210.     if (lst_ofile == null)
  211.       Abort("Could not open file '"+ofilename+"'\n");
  212.     lst_wfile = new StreamWriter(lst_ofile);
  213.     }
  214.   }
  215.  
  216. public void Out(String s)
  217.   {
  218.   lst_wfile.Write(s);        // write the buffer
  219.   lst_wfile.Flush();        // slow, but useful
  220.   }
  221.  
  222. public void Finish()
  223.   {
  224.   rfile.Close();
  225.   ifile.Close();
  226.   if (genlist)
  227.     {
  228.     lst_wfile.Close();
  229.     lst_ofile.Close();
  230.     }
  231.   }
  232.  
  233. public bool EOF()
  234.   {
  235.   return _eof;
  236.   }
  237.  
  238.  
  239. public String commentEndPreTok(String s)
  240.   {
  241. #if DEBUG
  242.   Console.Write("commentEndPreTok1 S=["+s+"], buf=");
  243.   for (int _debug_i=0; _debug_i<buf.Length;_debug_i++)
  244.     {
  245.     int _debug_d = buf[_debug_i];
  246.     char _debug_c = (char) (_debug_d + 96);
  247.     if (_debug_d < 32)
  248.       Console.Write("^"+Char.ToString(_debug_c));
  249.     else
  250.       Console.Write(buf[_debug_i]);
  251.     Console.Write("[");
  252.     Console.Write(Int32.ToString(_debug_d));
  253.     Console.Write("],");
  254.     }
  255.   Console.WriteLine(";");
  256. #endif
  257.   /*
  258.    * many times we will already have parsed source code past the point
  259.    * that we want to emit.  We will use the token given to backup.
  260.    */
  261.   String b;
  262.   if (s == null)        // make sure we have something
  263.     return null;
  264.   b = buf.ToString();        // have to convert first
  265.   int i = b.LastIndexOf(s);    // find this token in buffer
  266.   String c = b.Substring(0,i).Trim(); // copy as comment
  267.   buf = new StringBuilder(b.Substring(i), MyC.MAXSTR); // remake buffer
  268.   /*
  269.    * need to update curline to be in synch with last emitted comment
  270.    */
  271.   curline = bufline;
  272.   for (int ci = 0; ci < buf.Length; ci++)
  273.     if (buf[ci] == '\n')
  274.       curline--;
  275. #if DEBUG
  276.   Console.Write("commentEndPreTok2 buf=");
  277.   for (int _debug_i=0; _debug_i<buf.Length;_debug_i++)
  278.     {
  279.     int _debug_d = buf[_debug_i];
  280.     char _debug_c = (char) (_debug_d + 96);
  281.     if (_debug_d < 32)
  282.       Console.Write("^"+Char.ToString(_debug_c));
  283.     else
  284.       Console.Write(buf[_debug_i]);
  285.     Console.Write("[");
  286.     Console.Write(Int32.ToString(_debug_d));
  287.     Console.Write("],");
  288.     }
  289.   Console.WriteLine(";");
  290. #endif
  291.   return c;
  292.   }
  293.  
  294. public String commentEndTok(String s)
  295.   {
  296. #if DEBUG
  297.   Console.Write("commentEndTok1 S=["+s+"], buf=");
  298.   for (int _debug_i=0; _debug_i<buf.Length;_debug_i++)
  299.     {
  300.     int _debug_d = buf[_debug_i];
  301.     char _debug_c = (char) (_debug_d + 96);
  302.     if (_debug_d < 32)
  303.       Console.Write("^"+Char.ToString(_debug_c));
  304.     else
  305.       Console.Write(buf[_debug_i]);
  306.     Console.Write("[");
  307.     Console.Write(Int32.ToString(_debug_d));
  308.     Console.Write("],");
  309.     }
  310.   Console.WriteLine(";");
  311. #endif
  312.  
  313.   /*
  314.    * variant to include this token at end of comment
  315.    */
  316.   String b;
  317.   if (s == null)        // make sure we have something
  318.     return null;
  319.   b = buf.ToString();        // have to convert first
  320.   int i = b.LastIndexOf(s);    // find this token in buffer
  321.   String c = b.Substring(0,i+s.Length).Trim(); // copy as comment
  322.   buf = new StringBuilder(b.Substring(i+s.Length), MyC.MAXSTR); // remake buffer
  323.   /*
  324.    * need to update curline to be in synch with last emitted comment
  325.    */
  326.   curline = bufline;
  327.   for (int ci = 0; ci < buf.Length; ci++)
  328.     if (buf[ci] == '\n')
  329.       curline--;
  330. #if DEBUG
  331.   Console.Write("commentEndTok2 buf=");
  332.   for (int _debug_i=0; _debug_i<buf.Length;_debug_i++)
  333.     {
  334.     int _debug_d = buf[_debug_i];
  335.     char _debug_c = (char) (_debug_d + 96);
  336.     if (_debug_d < 32)
  337.       Console.Write("^"+Char.ToString(_debug_c));
  338.     else
  339.       Console.Write(buf[_debug_i]);
  340.     Console.Write("[");
  341.     Console.Write(Int32.ToString(_debug_d));
  342.     Console.Write("],");
  343.     }
  344.   Console.WriteLine(";");
  345. #endif
  346.   return c;
  347.   }
  348.  
  349. public void commentBegin(String s)
  350.   {
  351.   String b;
  352.   if (s == null)        // make sure we have something
  353.     return;
  354.   b = buf.ToString();        // have to convert first
  355.   int i = b.IndexOf(s);        // find this token in buffer
  356.   if (i < 0)
  357.     i = b.Length;        // if not found, use whole string
  358.   buf = new StringBuilder(b.Substring(i), MyC.MAXSTR); // remake buffer from substr
  359.   /*
  360.    * need to update curline to be in synch with last emitted comment
  361.    */
  362.   curline = bufline;
  363.   for (int ci = 0; ci < buf.Length; ci++)
  364.     if (buf[ci] == '\n')
  365.       curline--;
  366. #if DEBUG
  367.   Console.Write("commentBegin S=["+s+"], buf=");
  368.   for (int _debug_i=0; _debug_i<buf.Length;_debug_i++)
  369.     {
  370.     int _debug_d = buf[_debug_i];
  371.     char _debug_c = (char) (_debug_d + 96);
  372.     if (_debug_d < 32)
  373.       Console.Write("^"+Char.ToString(_debug_c));
  374.     else
  375.       Console.Write(buf[_debug_i]);
  376.     Console.Write("[");
  377.     Console.Write(Int32.ToString(_debug_d));
  378.     Console.Write("],");
  379.     }
  380.   Console.WriteLine(";");
  381. #endif
  382.   }
  383.  
  384. public int commentGetCurrentLine()
  385.   {
  386.   return curline+1;
  387.   }
  388.  
  389. public static string GetOutputFilename()
  390.   {
  391.   return ofilename;
  392.   }
  393.  
  394. }
  395. }
  396.