Ejemplo n.º 1
0
 void reserve(Word w)
 {
     words.Add(w.lexeme, w);
 }
Ejemplo n.º 2
0
        public Token scan()
        {
            for (; ; readch())
            {
                if (peek == ' ' || peek == '\t')
                    continue;
                else if (peek == '\n')
                    line++;
                else
                    break;
            }

            if (peek == '/')
            {
                readch();
                if (peek == '/')
                {
                    while (peek != '\n')
                        readch();
                    line++;
                }
                else if (peek == '*')
                {
                    readch();
                    while (true)
                    {
                        if (peek == '*')
                        {
                            readch();
                            if (peek == '/')
                                break;
                        }
                        else if (peek == 65535)
                            return new Word("_THE_END_", Tag._THE_END_);
                        else
                            readch();
                    }
                }
                else
                    return new Token('/');
            }

            /*            switch (peek)
                        {
                            case '&':
                                if (readch('&')) return Word.and; else return new Token('&');
                            case '|':
                                if (readch('|')) return Word.or; else return new Token('|');
                            case '=':
                                if (readch('=')) return Word.eq; else return new Token('=');
                            case '!':
                                if (readch('=')) return Word.ne; else return new Token('!');
                            case '<':
                                if (readch('=')) return Word.le; else return new Token('<');
                            case '>':
                                if (readch('=')) return Word.ge; else return new Token('>');
                        }
            */
            if (Char.IsDigit(peek))
            {
                int v = 0;
                do
                {
                    v = 10 * v + peek;
                    readch();
                } while (Char.IsDigit(peek));

                if (peek != '.') return new Num(v);
                double x = v;
                double d = 10.0;
                for (; ; )
                {
                    readch();
                    if (!Char.IsDigit(peek))
                        break;
                    x = x + peek / d;
                    d *= 10;
                }

                return new Real(x);
            }

            if (Char.IsLetter(peek) || peek == '.')
            {
                StringBuilder b = new StringBuilder();
                do
                {
                    if (peek == '.')
                        b.Append('_');
                    else
                        b.Append(peek);
                    readch();
                } while (Char.IsLetter(peek) || peek == '_');

                string s = b.ToString();
                Word w = (Word)words[s];
                if (w != null) return w;
                w = new Word(s, Tag.ID);
                words.Add(s, w);

                return w;
            }

            Token token = new Token(peek);
            peek = ' ';
            return token;
        }