Beispiel #1
0
        public void TestTokenClass()
        {
            var tok_c = new Token('c');
            Assert.AreEqual('c', tok_c.TagValue);
            Assert.AreEqual("c", tok_c.ToString());

            var tok_9 = new Token('9');
            Assert.AreEqual('9', tok_9.TagValue);
            Assert.AreEqual("9", tok_9.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Read and return the current token
        /// </summary>
        /// <returns></returns>
        public Token Scan()
        {
            //for white spaces
            for (; !this.EofReached; this.ReadChar())
            {
                if (_curr == ' ' || _curr == '\t')
                { 
                    continue; 
                }
                else if (_curr == '\r')
                {
                    this.ReadChar();    //eat \r    
                    ++Line; 
                }
                else break;
            }

            if (this.EofReached) return null;

            //for operators like && !=, etc
            switch (_curr)
            {
                case '&':
                    return this.ReadChar('&') ? Word.and : new Token('&');
                case '|':
                    return this.ReadChar('|') ? Word.or : new Token('|');
                case '=':
                    return this.ReadChar('=') ? Word.eq : new Token('=');
                case '!':
                    return this.ReadChar('=') ? Word.ne : new Token('!');
                case '<':
                    return this.ReadChar('=') ? Word.le : new Token('<');
                case '>':
                    return this.ReadChar('=') ? Word.ge : new Token('>');
            }

            //for numbers
            if (char.IsDigit(_curr))
            {
                int v = 0;
                do{
                    v = 10 * v + (int)(_curr - '0');
                    this.ReadChar();
                } while (char.IsDigit(_curr));
                if (_curr != '.') return new Num(v);

                float f = v;
                for (float d = 10; ; d *= 10)
                {
                    this.ReadChar();
                    if (!char.IsDigit(_curr)) break;
                    f += (int)(_curr - 48) / d;
                }
                return new Real(f);
            }

            //for identifiers
            if (char.IsLetter(_curr))
            {
                var b = new StringBuilder();
                do
                {
                    b.Append(_curr); 
                    this.ReadChar();
                } while (char.IsLetterOrDigit(_curr));
                var s = b.ToString();
                if (KeyWords.ContainsKey(s)) return KeyWords[s];
                else return KeyWords[s] = new Word(s, Tag.ID);
            }

            //for the rest 
            var tok = new Token(_curr);
            if (!this.EofReached) _curr = ' ';
            return tok;
        }
Beispiel #3
0
 public void AddIdentifier(Token tok, Id id)
 {
     this.SymbolTable.Add(tok, id);
 }
Beispiel #4
0
 public Id Get(Token tok)
 {
     for(var e = this; e != null; e = e.Prev)
         if (e.SymbolTable.ContainsKey(tok)) return e.SymbolTable[tok];
     return null;
 }
Beispiel #5
0
 /// <summary>
 /// Recognize next token and save in _look;
 /// </summary>
 public void Move()
 {
     _look = _lexer.Scan();
 }