Example #1
0
 public void resume(string code)
 {
     this.code = code;
     pos       = 0;
     lastnl    = 0;
     unit      = null;
 }
Example #2
0
        private ExpNode parseElmnt()
        {
            if (lex.Peek() == Token.NUM)
            {
                return(new NumExpNode(lex.Take()));
            }
            if (lex.Peek() == Token.ID)
            {
                LexUnit id = lex.Take();
                if (lex.Peek() == Token.LPN)
                {
                    lex.Take();
                    FncCallExpNode fnc = new FncCallExpNode();
                    fnc.ID   = id;
                    fnc.args = parseArgs();
                    Match(Token.RPN, "expected ')' for valid function call");
                    return(fnc);
                }
                else
                {
                    return(new IdExpNode(id));
                }
            }

            if (lex.Peek() == Token.LPN)
            {
                lex.Take();
                ExpNode exp = parseExp();
                Match(Token.RPN, "expected ')' for valid expression");
                return(exp);
            }
            throw new ParseException(ref lex.unit, "invalid in expression");
        }
Example #3
0
 public void init(string code)
 {
     this.code = code;
     pos       = 0;
     ln        = 1;
     lastnl    = 0;
     unit      = null;
 }
Example #4
0
        public LexUnit Take()
        {
            Peek();
            var tmp = unit;

            unit = null;
            return(tmp);
        }
Example #5
0
 static void error(LexUnit line, string msg)
 {
     errors.Add(string.Format("{0}: Symantic error : " + msg, line.line));
 }
Example #6
0
 public StringExpNode(LexUnit unit)
 {
     STR = unit;
 }
Example #7
0
 public IdExpNode(LexUnit id)
 {
     ID = id;
 }
Example #8
0
 public NumExpNode(LexUnit num)
 {
     NUM = num;
 }
Example #9
0
        public Token Peek()
        {
            if (unit != null)
            {
                return(unit.token);
            }
            unit = new LexUnit();
            skipSpace();

            if (isDigit())
            {
                start();
                while (isDigit())
                {
                    append();
                }
                if (isOneOf("."))
                {
                    append();
                    while (isDigit())
                    {
                        append();
                    }
                }
                unit.token = Token.NUM;
                return(Token.NUM);
            }

            if (isOneOf("()=+-*/^,\n"))
            {
                unit.token = (Token)code [pos];
                start();
                if (unit.token == Token.EOL)
                {
                    unit.lexeme = "EOL";
                }
                return(unit.token);
            }

            if (isAlpha())
            {
                start();
                while (isAlpha() || isDigit())
                {
                    append();
                }
                if (unit.lexeme == "let")
                {
                    unit.token = Token.LET;
                }
                else if (unit.lexeme == "fnc")
                {
                    unit.token = Token.FNC;
                }
                else if (unit.lexeme == "print")
                {
                    unit.token = Token.PRT;
                }
                else
                {
                    unit.token = Token.ID;
                }
                return(unit.token);
            }

            if (!end() && code [pos] == '"')
            {
                pos++;
                unit.lexeme = "";
                while (code [pos] != '"' && code [pos] != '\n')
                {
                    append();
                }
                if (code [pos] == '\n')
                {
                    throw new Exception(string.Format("{0},{1}: Lexical error : Expected (\") to end a string", ln, pos - lastnl, code[pos]));
                }
                pos++;
                unit.token = Token.STR;
                return(unit.token);
            }

            if (end())
            {
                unit.token  = Token.EOF;
                unit.lexeme = "EOF";
                return(unit.token);
            }
            throw new Exception(string.Format("{0},{1}: Lexical error : Unrecognized character '{2}'", ln, pos - lastnl, code[pos]));
        }
Example #10
0
 public ParseException(ref LexUnit unexpected, string msg = null) :
     base(string.Format("{0},{1}: Syntaxe error : unexpected '{2}'" + (msg != null?(", " + msg):""), unexpected.line, unexpected.colmn, unexpected.lexeme))
 {
 }