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

  1.  
  2. using System;
  3. using System.Collections;
  4. using LexToken;
  5.  
  6. enum States {
  7.     Number = 0,
  8.     Operator = 2
  9. }
  10.  
  11.  
  12. class Lexer{
  13.     public Queue tokens;
  14.     String file;
  15.     int FileSize;    
  16.     int index;
  17.     bool success;
  18.  
  19.     public Lexer(String f)
  20.     {
  21.     tokens = new Queue();
  22.     file = f;
  23.     FileSize = file.Length;
  24.     success = true;
  25.     }
  26.  
  27.     public bool Lex()
  28.     {
  29.     index = 0;
  30.     //This is a hack to avoid a overflow of buffer during lookahead
  31.     //Mind that the FileSize is only initialized in the beginning(constructor).
  32.     file = file + "\n";  
  33.     
  34.     while(index < FileSize){        
  35.         Token t = GetNextToken();
  36.         if (t !=null)
  37.         tokens.Enqueue(t);        
  38.     }
  39.     //This adds the EOFSYM for the Parser
  40.     tokens.Enqueue(new Token(TokenType.EOFSYM));
  41.     
  42.     return success;
  43.     
  44.     }
  45.  
  46.     Token GetNextToken()
  47.     {
  48.     String value;
  49.     
  50.         switch (file[index]){
  51.         case '\n':
  52.         case '\r':
  53.         case '\t':
  54.         case ' ':
  55.         index++;
  56.         return null;
  57.         case ';':
  58.         index++;
  59.         while (file[index] != '\n'){
  60.             index++;
  61.         }
  62.         return null;
  63.         
  64.         case '0':
  65.         case '1':
  66.         case '2':
  67.         case '3':
  68.         case '4':
  69.         case '5':
  70.         case '6':
  71.         case '7':
  72.         case '8':
  73.         case '9':
  74.         value = Convert.ToString(file[index]);
  75.         index++;
  76.         while (Char.IsDigit(file[index])){
  77.             value = value + file[index];
  78.             index++;
  79.         }
  80.         return new NumberToken(value);
  81.         case '<':
  82.         index++;
  83.         if (file[index] == '='){
  84.             index++;
  85.             return new Token(TokenType.LE);
  86.         }
  87.         else
  88.         return new Token(TokenType.LT);
  89.         case '>':
  90.         index++;
  91.         if (file[index] == '='){
  92.             index++;
  93.             return new Token(TokenType.GE);
  94.         }
  95.         else
  96.             return new Token(TokenType.GT);
  97.         case '=':
  98.         index++;
  99.         return new Token(TokenType.EQ);
  100.         case '+':
  101.         index++;
  102.         if (Char.IsDigit(file[index])){
  103.             value = Convert.ToString('+');
  104.             while(Char.IsDigit(file[index])){
  105.             value = value + file[index];
  106.             index++;
  107.             }
  108.             return new NumberToken(value);
  109.         }
  110.         else    
  111.             return new Token(TokenType.PLUS);
  112.  
  113.                case '-':
  114.         index++;
  115.         if (Char.IsDigit(file[index])){
  116.             value = Convert.ToString('-');
  117.             while(Char.IsDigit(file[index])){
  118.             value = value + file[index];
  119.             index++;
  120.             }
  121.             return new NumberToken(value);
  122.         }
  123.         else    
  124.             return new Token(TokenType.MINUS);
  125.  
  126.         case '*':
  127.         index++;
  128.         return new Token(TokenType.MUL);
  129.         case '/':
  130.         index++;
  131.         return new Token(TokenType.DIVIDE);
  132.         case '(':
  133.         index++;
  134.         return new Token(TokenType.LPAREN);
  135.         case ')':
  136.         index++;
  137.         return new Token(TokenType.RPAREN); 
  138.         case '\'':
  139.         index++;
  140.         value = null;
  141.         if (Char.IsLetterOrDigit(file[index])){
  142.             value = file[index].ToString();
  143.             index++;
  144.             while(Char.IsLetterOrDigit(file[index])){
  145.             value = value + file[index];
  146.             index++;
  147.             }
  148.             if (value.CompareTo("NIL") == 0)
  149.             return new Token(TokenType.NIL);
  150.             if (value.ToUpper().CompareTo(value.ToLower()) == 0)     //Hack to get int from something like '10
  151.             return new NumberToken(value);
  152.         }
  153.         else if(file[index] == '('){
  154.             value = "(";
  155.             index++;
  156.             int count = 1;
  157.             while(file[index] != ')' || --count > 0){
  158.             if(file[index] == '(')
  159.                 count++;
  160.             value = value + file[index];
  161.             index++;
  162.             }
  163.             value = value + ")";
  164.             index++;
  165.         }
  166.         return new StringToken(value);
  167.         
  168.         default:
  169.         if(Char.IsLetter(file[index])){
  170.                value = Convert.ToString(file[index]);
  171.                index++;               
  172.                while(Char.IsLetterOrDigit(file[index])){
  173.                value = value + file[index];
  174.                index++;
  175.                }
  176.                if(value.CompareTo("cdr") == 0)
  177.                return new Token(TokenType.CDR);
  178.                if(value.CompareTo("car") == 0)
  179.                return new Token(TokenType.CAR);
  180.                if (value.CompareTo("cons") == 0)
  181.                return new Token(TokenType.CONS);
  182.                if (value.CompareTo("subst") == 0)
  183.                return new Token(TokenType.SUBST);
  184.                if (value.CompareTo("defun") == 0)
  185.                return new Token(TokenType.DEFUN);
  186.                if (value.CompareTo("setq") == 0)
  187.                return new Token(TokenType.SETQ);
  188.                if (value.CompareTo("null") == 0)
  189.                return new Token(TokenType.NULL);
  190.                if (value.CompareTo("if") == 0)
  191.                return new Token(TokenType.IF);
  192.                if (value.CompareTo("atom") == 0)
  193.                return new Token(TokenType.ATOM);
  194.                if (value.CompareTo("do") == 0)
  195.                return new Token(TokenType.DO);
  196.                //else
  197.                return new AlphaToken(value);               
  198.                
  199.         }
  200.         else{
  201.             Console.WriteLine("Error in Input");
  202.             index++;
  203.             success = false;
  204.             return null;
  205.            }
  206.         
  207.         }
  208.     }
  209. }
  210.  
  211.