Пример #1
0
 internal virtual int val()
 {
     if (lookahead.token == Tokens.Covert("VAR"))
     {
         Token v = lookahead;
         match(Tokens.Covert("VAR"));
         Token s = sym.get_symbol(v.value);
         if (s.token != Tokens.Covert("ERR"))
         {
             return(s.value);
         }
         else
         {
             error("VAR not found");
         }
         return(0);
     }
     else if (lookahead.token == Tokens.Covert("NUM"))
     {
         return(exp());
     }
     else
     {
         error("VAR or NUM expected");
         return(-1);
     }
 }
Пример #2
0
 public virtual void print_token(Token t)
 {
     if (t.token == Tokens.Covert("ERR"))
     {
         Console.WriteLine("<ERR>");
     }
     else if (t.token == Tokens.Covert("PLUS"))
     {
         Console.WriteLine("<PLUS>");
     }
     else if (t.token == Tokens.Covert("EQUALS"))
     {
         Console.WriteLine("<EQUALS>");
     }
     else if (t.token == Tokens.Covert("NUM"))
     {
         Console.WriteLine("<NUM," + t.value + ">");
     }
     else if (t.token == Tokens.Covert("VAR"))
     {
         Console.WriteLine("<VAR," + t.value + ">");
     }
     else if (t.token == Tokens.Covert("PRINT"))
     {
         Console.WriteLine("<PRINT>");
     }
 }
Пример #3
0
 internal virtual void @out()
 {
     if (lookahead.token == Tokens.Covert("PRINT"))
     {
         match(Tokens.Covert("PRINT"));
         int x = val();
         Console.WriteLine(x);
     }
 }
Пример #4
0
        public virtual Token get_symbol(int id)
        {
            if (id < symbols.Count)
            {
                return(symbols[id]);
            }
            Token s = new Token();

            s.token = Tokens.Covert("ERR");
            return(s);
        }
Пример #5
0
 internal virtual int exp()
 {
     if (lookahead.token == Tokens.Covert("NUM"))
     {
         int x = lookahead.value;
         match(Tokens.Covert("NUM"));
         int y = rst();
         return(x + y);
     }
     return(0);
 }
Пример #6
0
 internal virtual void pr2()
 {
     if (lookahead.token == Tokens.Covert("EOL"))
     {
         match(Tokens.Covert("EOL"));
         prg();
     }
     else if (lookahead.token != Tokens.Covert("EOF"))
     {
         error("EOL Expected");
     }
 }
Пример #7
0
 internal virtual void atr()
 {
     if (lookahead.token == Tokens.Covert("VAR"))
     {
         int v = lookahead.value;
         match(Tokens.Covert("VAR"));
         match(Tokens.Covert("EQUALS"));
         int x = val();
         if (!sym.update_symbol(v, x))
         {
             error("Impossible assign value");
         }
         else
         {
             error("VAR Expected");
         }
     }
 }
Пример #8
0
 internal virtual void cmd()
 {
     if (lookahead.token == Tokens.Covert("NUM"))
     {
         exp();
     }
     else if (lookahead.token == Tokens.Covert("VAR"))
     {
         atr();
     }
     else if (lookahead.token == Tokens.Covert("PRINT"))
     {
         @out();
     }
     else if (lookahead.token != Tokens.Covert("EOF"))
     {
         error("Unrecongnized Command");
     }
 }
Пример #9
0
        internal virtual Token next_Token()
        {
            Token   t      = new Token();
            Symbols symbol = new Symbols();

            t.token = -1;
            peek    = next_char();

            do
            {
                if (peek == ' ' || peek == '\t')
                {
                    continue;
                }
                else if (peek == '\n')
                {
                    line++;
                    char_pos = -1;
                }
                else
                {
                    break;
                }
            } while (peek == next_char());

            if (peek == '$')
            {
                string @var = "$";
                peek = next_char();
                while (isLetter(peek))
                {
                    @var += peek;
                    peek  = next_char();
                }
                input_pos--;
                t.token = Tokens.Covert("VAR");
                Token s = new Token();
                s.token = Tokens.Covert("VAR");
                s.value = 0;
                s.lexem = @var;
                t.value = symbol.add_symbol(s);
                return(t);
            }
            else if (isDigit(peek))
            {
                int x = 0;
                do
                {
                    x    = 10 * x + System.identityHashCode(peek);
                    peek = next_char();
                } while (isDigit(peek));
                input_pos--;
                t.token = Tokens.Covert("NUM");
                t.value = x;
                return(t);
            }
            else if (peek == 'p')
            {
                string print = "print";
                for (int i = 0; i < print.Length; i++)
                {
                    if (print[i] == peek)
                    {
                        peek = next_char();
                    }
                    else
                    {
                        t.token = Tokens.Covert("ERR");
                        return(t);
                    }
                }
                t.token = Tokens.Covert("PRINT");
                return(t);
            }
            else if (peek == ';')
            {
                t.token = Tokens.Covert("EOL");
                t.value = 0;
            }
            else if (peek == '+')
            {
                t.token = Tokens.Covert("PLUS");
                t.value = 0;
            }
            else if (peek == '=')
            {
                t.token = Tokens.Covert("EQUALS");
                t.value = 0;
            }
            return(t);
        }