Ejemplo n.º 1
0
        public static EXPRESSION parse(iSCOPE context)
        {
            Token token = get();

            forget();
            Span begin = token.span;

            CONDITIONAL result   = new CONDITIONAL();
            EXPRESSION  elsePart = null;

            EXPRESSION condition = EXPRESSION.parse(null, context);

            expect(TokenCode.Then);
            EXPRESSION thenPart = EXPRESSION.parse(null, context);

            COND_IF_THEN cit = new COND_IF_THEN(condition, thenPart);

            condition.parent = result;
            thenPart.parent  = result;
            cit.parent       = result;
            result.add(cit);

            token = get();
            while (true)
            {
                if (token.code == TokenCode.Elsif)
                {
                    forget();
                    condition = EXPRESSION.parse(null, context);
                    expect(TokenCode.Then);
                    thenPart = EXPRESSION.parse(null, context);

                    cit = new COND_IF_THEN(condition, thenPart);
                    cit.setSpan(token.span, thenPart.span);

                    condition.parent = result;
                    thenPart.parent  = result;
                    cit.parent       = result;

                    result.add(cit);
                    token = get();
                    // go to next iteration
                }
                else
                {
                    break;
                }
            }
            token           = expect(TokenCode.Else);
            elsePart        = EXPRESSION.parse(null, context);
            elsePart.parent = result;
            result.add(elsePart);
            result.setSpan(begin, elsePart.span);

            // context.add(result);  ?? why ??
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static new EXPRESSION parse(Token first, iSCOPE context)
        {
            EXPRESSION result = null;
            Token      token  = (first == null) ? get() : first;
            Token      begin  = token;

            switch (token.code)
            {
            case TokenCode.This:
                forget();
                UNIT unit = Context.unit();
                if (unit == null)
                {
                    // Error message!
                    result = new THIS(null);
                }
                else
                {
                    result = new THIS(unit);
                    result.setSpan(token);
                }
                break;

            case TokenCode.Return:
                if (!ENTITY.weAreWithinEnsure)
                {
                    break;
                }
                forget();
                ROUTINE routine = Context.routine();
                if (routine == null)
                {
                }       // error
                else
                {
                    result = new RETURN_EXPR(routine);
                    result.setSpan(token);
                }
                break;

            case TokenCode.Old:
                forget();
                result = EXPRESSION.parse(null, context);
                Span end = result.span;
                OLD  old = new OLD(result);
                result.parent = old;
                result        = old;
                result.setSpan(begin.span, end);
                break;

            case TokenCode.If:
                result = CONDITIONAL.parse(context);
                break;

            case TokenCode.LParen:
                forget();
                Span start_tuple = token.span;
                result = EXPRESSION.parse(null, context);
                token  = get();
                if (token.code == TokenCode.Comma)
                {
                    // Seems to be a tuple
                    forget();
                    TUPLE_EXPR tuple = new TUPLE_EXPR();
                    tuple.add(result);
                    while (true)
                    {
                        EXPRESSION expr = EXPRESSION.parse(null, context);
                        tuple.add(expr);

                        token = get();
                        if (token.code != TokenCode.Comma)
                        {
                            break;
                        }
                        forget();
                    }
                    result = tuple;
                }
                end = expect(TokenCode.RParen).span;
                result.setSpan(token.span, end);
                break;

            case TokenCode.Identifier:
                if (first == null)
                {
                    forget();                      ////// perhaps the same condition should be added for all cases?
                }
                DECLARATION d = Context.find(token);
                if (d == null)
                {
                    result = new UNRESOLVED(context, new IDENTIFIER(token));
                }
                else
                {
                    result = new REFERENCE(d);
                }

                Token token2 = get();
                if (token2.code == TokenCode.LBracket)
                {
                    UNIT_REF unitRef = UNIT_REF.parse(token, false, context);
                    result = new NEW(unitRef);
                    result.setSpan(unitRef.span);
                }
                else
                {
                    result.setSpan(token);
                }
                break;

            case TokenCode.Integer:
            case TokenCode.Real:
            case TokenCode.String:
            case TokenCode.Character:
                result = new LITERAL(token.value, token.span, token.code);
                result.setSpan(token);
                forget();
                break;

            default:
                return(null);
            }
            // result.setSpan(token);
            return(result);
        }