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

  1. namespace MyC
  2. {
  3. using System;
  4. using System.Text;
  5. using System.Collections;
  6.  
  7. class Tok
  8.   {
  9.   public const int T_LEFT_ASSIGN    = 10001;
  10.   public const int T_RIGHT_ASSIGN    = 10002;
  11.   public const int T_LEFT_OP        = 10003;
  12.   public const int T_LE_OP        = 10004;
  13.   public const int T_RIGHT_OP        = 10005;
  14.   public const int T_GE_OP        = 10006;
  15.   public const int T_EQ_OP        = 10007;
  16.   public const int T_NEQ_OP        = 10008;
  17.   public const int T_INC_OP        = 10009;
  18.   public const int T_ADD_ASSIGN        = 10010;
  19.   public const int T_DEC_OP        = 10011;
  20.   public const int T_SUB_ASSIGN        = 10012;
  21.   public const int T_PTR_OP        = 10013;
  22.   public const int T_MUL_ASSIGN        = 10014;
  23.   public const int T_DIV_ASSIGN        = 10015;
  24.   public const int T_DIV_OP        = 10016;
  25.   public const int T_LOG_OR_OP        = 10017;
  26.   public const int T_LOG_AND_OP        = 10018;
  27.   public const int T_LT_OP        = 10019;
  28.   public const int T_GT_OP        = 10020;
  29.   public const int T_ASSIGN        = 10021;
  30.   public const int T_ADD_OP        = 10022;
  31.   public const int T_SUB_OP        = 10023;
  32.   public const int T_MUL_OP        = 10024;
  33.   public const int T_OR_OP        = 10025;
  34.   public const int T_AND_OP        = 10026;
  35.   public const int T_MOD_OP        = 10027;
  36.   public const int T_XOR_OP        = 10028;
  37.   public const int T_COMPL_OP        = 10029;
  38.   public const int T_NOT_OP        = 10030;
  39.  
  40.   public const int T_IF            = 20001;
  41.   public const int T_ELSE        = 20002;
  42.   public const int T_WHILE        = 20003;
  43.   public const int T_FOR        = 20004;
  44.   public const int T_DO            = 20005;
  45.   public const int T_BREAK        = 20006;
  46.   public const int T_CONTINUE        = 20007;
  47.   public const int T_RETURN        = 20008;
  48.   public const int T_FLOW_MIN        = T_IF;
  49.   public const int T_FLOW_MAX        = T_RETURN;
  50.  
  51.   public const int T_EXTERN        = 30001;
  52.   public const int T_STATIC        = 30002;
  53.   public const int T_AUTO        = 30003;
  54.   public const int T_SIGNED        = 30004;
  55.   public const int T_UNSIGNED        = 30005;
  56.   public const int T_DEFCLASS        = 30006;
  57.   public const int T_STORAGE_MIN    = T_EXTERN;
  58.   public const int T_STORAGE_MAX    = T_DEFCLASS;
  59.   public const int T_PARAM        = 30007    /* special for tagging parameters */;
  60.  
  61.   public const int T_SHORT        = 40001;
  62.   public const int T_CHAR        = 40002;
  63.   public const int T_INT        = 40003;
  64.   public const int T_LONG        = 40004;
  65.   public const int T_FLOAT        = 40005;
  66.   public const int T_DOUBLE        = 40006;
  67.   public const int T_VOID        = 40007;
  68.   public const int T_DEFTYPE        = 40008;
  69.   public const int T_TYPE_MIN        = T_SHORT;
  70.   public const int T_TYPE_MAX        = T_DEFTYPE;
  71.  
  72.   public const int T_IDENT         = 50001;
  73.   public const int T_DIGITS         = 50002;
  74.   public const int T_UNKNOWN        = 99999;
  75.   public const int T_EOF         = -1;
  76.  
  77.   static Hashtable tokens;
  78.   StringBuilder value;
  79.   int token_id;
  80.   Io io;
  81.  
  82.   public static void AddTok(int i, String s)
  83.     {
  84.     tokens.Add(s, i);
  85.     tokens.Add(i, s);
  86.     }
  87.  
  88.   public void InitHash()
  89.     {
  90.     tokens = new Hashtable();
  91.     AddTok(T_LEFT_ASSIGN,    "<<=");
  92.     AddTok(T_RIGHT_ASSIGN,    ">>=");
  93.     AddTok(T_LEFT_OP,        "<<");
  94.     AddTok(T_LE_OP,        "<=");
  95.     AddTok(T_RIGHT_OP,        ">>");
  96.     AddTok(T_GE_OP,        ">=");
  97.     AddTok(T_EQ_OP,        "==");
  98.     AddTok(T_NEQ_OP,        "!=");
  99.     AddTok(T_INC_OP,        "++");
  100.     AddTok(T_ADD_ASSIGN,    "+=");
  101.     AddTok(T_DEC_OP,        "--");
  102.     AddTok(T_SUB_ASSIGN,    "-=");
  103.     AddTok(T_PTR_OP,        "->");
  104.     AddTok(T_MUL_ASSIGN,    "*=");
  105.     AddTok(T_DIV_ASSIGN,    "/=");
  106.     AddTok(T_DIV_OP,        "/");
  107.     AddTok(T_LOG_OR_OP,        "||");
  108.     AddTok(T_LOG_AND_OP,    "&&");
  109.     AddTok(T_LT_OP,        "<");
  110.     AddTok(T_GT_OP,        ">");
  111.     AddTok(T_ASSIGN,        "=");
  112.     AddTok(T_ADD_OP,        "+");
  113.     AddTok(T_SUB_OP,        "-");
  114.     AddTok(T_MUL_OP,        "*");
  115.     AddTok(T_OR_OP,        "|");
  116.     AddTok(T_AND_OP,        "&");        /* could be addr of, or bitwise and */
  117.     AddTok(T_MOD_OP,        "%");
  118.     AddTok(T_XOR_OP,        "^");
  119.     AddTok(T_COMPL_OP,        "~");
  120.     AddTok(T_NOT_OP,        "!");
  121.  
  122.     AddTok(T_IF,        "if");
  123.     AddTok(T_ELSE,        "else");
  124.     AddTok(T_WHILE,        "while");
  125.     AddTok(T_FOR,        "for");
  126.     AddTok(T_DO,        "do");
  127.     AddTok(T_BREAK,        "break");
  128.     AddTok(T_CONTINUE,        "continue");
  129.     AddTok(T_RETURN,        "return");
  130.  
  131.     AddTok(T_EXTERN,        "extern");
  132.     AddTok(T_STATIC,        "static");
  133.     AddTok(T_AUTO,        "auto");
  134.     AddTok(T_SIGNED,        "signed");
  135.     AddTok(T_UNSIGNED,        "unsigned");
  136.  
  137.     AddTok(T_INT,        "int");
  138.     AddTok(T_LONG,        "long");
  139.     AddTok(T_CHAR,        "char");
  140.     AddTok(T_FLOAT,        "float");
  141.     AddTok(T_DOUBLE,        "double");
  142.     AddTok(T_VOID,        "void");
  143.     }
  144.  
  145.   public Tok(Io ihandle)
  146.     {
  147.     io = ihandle;
  148.     InitHash();            // initialize the tokens hashtable
  149.     io.ReadChar();
  150.     scan();
  151.     }
  152.  
  153.   /*
  154.    * table lookup
  155.    *
  156.    * if the input string matches a table entry, return the entry token_id
  157.    * if not, return -1.
  158.    */
  159.   int lookup_id()
  160.     {
  161.     String s = value.ToString();
  162.     Object k = tokens[s];
  163.     if (k == null)
  164.       return 0;
  165.     return (int) k;
  166.     }
  167.  
  168.   /* constant declaration */
  169.   const char TAB = '\t';
  170.   const char CR = '\r';
  171.   const char LF = '\n';
  172.  
  173.   /* recognize any operator */
  174.   bool isOp(char c)
  175.     {
  176.     return (c == '+' || c == '-' || c == '*' || c == '/' ||
  177.         c == '<' || c == '>' || c == '=' ||
  178.         c == '&' || c == '|' || c == '^' || c == '!'
  179.         );
  180.     }
  181.  
  182.   bool isAddOp(char c)
  183.     {
  184.     return ((c == '+' || c == '-'));
  185.     }
  186.  
  187.   bool isMulOp(char c)
  188.     {
  189.     return ((c == '*' || c == '/'));
  190.     }
  191.  
  192.   bool isOrOp(char c)
  193.     {
  194.     return ((c == '|') || (c == '~'));
  195.     }
  196.  
  197.   bool isRelOp(char c)
  198.     {
  199.     return ((c == '=') || (c == '!') || (c == '<') || (c == '>'));
  200.     }
  201.  
  202.   void skipWhite()
  203.     {
  204.     while (Char.IsWhiteSpace(io.getNextChar()))
  205.       io.ReadChar();
  206.     }
  207.  
  208. /* get an identifier */
  209. void LoadName()
  210.   {
  211.   value = new StringBuilder(MyC.MAXSTR);
  212.   skipWhite();
  213.   if (!Char.IsLetter(io.getNextChar()))
  214.     throw new ApplicationException("?Expected Name");
  215.   while (Char.IsLetterOrDigit(io.getNextChar()))
  216.     {
  217.     value.Append(io.getNextChar());
  218.     io.ReadChar();
  219.     }
  220.   token_id = lookup_id();
  221.   if (token_id <= 0)
  222.     token_id = T_IDENT;
  223.   skipWhite();
  224.   }
  225.  
  226. /* get a number */
  227. void LoadNum()
  228.   {
  229.   value = new StringBuilder(MyC.MAXSTR);
  230.   skipWhite();
  231.   if (!Char.IsDigit(io.getNextChar()))
  232.     throw new ApplicationException("?Expected Integer");
  233.   while (Char.IsDigit(io.getNextChar()))
  234.     {
  235.     value.Append(io.getNextChar());
  236.     io.ReadChar();
  237.     }
  238.   token_id = T_DIGITS;
  239.   skipWhite();
  240.   }
  241.  
  242. /* get an operator */
  243. void LoadOp()
  244.   {
  245.   value = new StringBuilder(MyC.MAXSTR);
  246.   skipWhite();
  247.   if (!isOp(io.getNextChar()))
  248.     throw new ApplicationException("?Expected operator");
  249.   while (isOp(io.getNextChar()))
  250.     {
  251.     value.Append(io.getNextChar());
  252.     io.ReadChar();
  253.     }
  254.   token_id = lookup_id();
  255.   skipWhite();
  256.   }
  257.  
  258. /* get an identifier */
  259.   public void scan()
  260.     {
  261.     skipWhite();
  262.     if (Char.IsLetter(io.getNextChar()))
  263.       LoadName();
  264.     else if (Char.IsDigit(io.getNextChar()))
  265.       LoadNum();
  266.     else if (isOp(io.getNextChar()))
  267.       LoadOp();
  268.     else if (io.EOF())
  269.       {
  270.       value = null;
  271.       token_id = T_EOF;
  272.       }
  273.     else
  274.       {
  275.       value = new StringBuilder(MyC.MAXSTR);
  276.       value.Append(io.getNextChar());
  277.       token_id = T_UNKNOWN;
  278.       io.ReadChar();
  279.       }
  280.     skipWhite();
  281. #if DEBUG
  282.     Console.WriteLine("[tok.scan tok=["+this+"]");
  283. #endif
  284.     }
  285.  
  286.   public char getFirstChar()
  287.     {
  288.     if (value == null)
  289.       return '\0';
  290.     return value[0];
  291.     }
  292.  
  293.   public String getValue()
  294.     {
  295.     if (value == null)
  296.       return "";
  297.     return value.ToString();
  298.     }
  299.  
  300.   public int getId()
  301.     {
  302.     return (token_id);
  303.     }
  304.  
  305.   public bool NotEOF()
  306.     {
  307.     return (token_id != T_EOF);
  308.     }
  309.  
  310.   public bool IsDeclKeyword()
  311.     {
  312.     if ((token_id >= T_TYPE_MIN && token_id <= T_TYPE_MAX) ||
  313.     (token_id >= T_STORAGE_MIN && token_id <= T_STORAGE_MAX))
  314.       return true;
  315.     return false;
  316.     }
  317.  
  318.   public override string ToString()
  319.     {
  320.     StringBuilder sb = new StringBuilder(getValue());
  321.     sb.Append("(");
  322.     sb.Append(token_id);
  323.     sb.Append(")");
  324.     return sb.ToString();
  325.     }
  326.  
  327.   }
  328. }
  329.