Пример #1
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);
        }
Пример #2
0
 public void add(DECLARATION d)
 {
     body.Add(d);
 }
Пример #3
0
 public REFERENCE(DECLARATION d)
 {
     declaration = d;
 }
Пример #4
0
        /// <summary>
        /// The function processes all kinds of statements including
        /// assignments, calls, and variable/constant declarations.
        /// </summary>
        /// <param name="context"></param>
        /// <returns>true, if any construct (a statement, or a simple declaration)
        /// was processed, and false otherwise.</returns>
        public static bool parse(iSCOPE context, TokenCode stop1, TokenCode stop2, TokenCode stop3)
        {
            Debug.Indent();
            Debug.WriteLine("Entering STATEMENT.parse");

            bool result = true;

            Token token = get();
            Token begin = token;

            if (token.code == stop1 && stop1 != TokenCode.ERROR)
            {
                goto Finish;
            }
            if (token.code == stop2 && stop2 != TokenCode.ERROR)
            {
                goto Finish;
            }
            if (token.code == stop3 && stop3 != TokenCode.ERROR)
            {
                goto Finish;
            }

            switch (token.code)
            {
            // case TokenCode.Pure: -- doesn't make any sense
            case TokenCode.Safe:
            case TokenCode.Routine:
                ROUTINE.parse(token, false, false, false, 0, context);
                break;

            case TokenCode.If:
                IF.parse(context);
                break;

            case TokenCode.While:
            case TokenCode.Loop:
                LOOP.parse(null, context);
                break;

            // Break
            //     : break [ Label ]
            //
            // Label
            //     : Identifier
            case TokenCode.Break:
                BREAK.parse(context);
                break;

            // Statement
            //     : ...
            //     | raise [ Expression ]
            case TokenCode.Raise:
                forget();
                RAISE.parse(token.span, context);
                break;

            // Statement
            //     : ...
            //     | check PredicatesList end
            //     | ...
            case TokenCode.Check:
                CHECK.parse(context);
                break;

            // Statement
            //     : ...
            //     | return [ Expression ]
            //     | ...
            case TokenCode.Return:
                forget();
                EXPRESSION expr = EXPRESSION.parse(null, context);    // can be null
                RETURN     ret  = new RETURN(expr);
                if (expr != null)
                {
                    expr.parent = ret;
                    ret.setSpan(begin.span, expr.span);
                }
                else
                {
                    ret.setSpan(begin);
                }

                context.add(ret);
                break;

            // Statement
            //     : ...
            //     | Try
            //     | ...
            //
            // Try
            //     : try { Statement } Catches [ Else ] end
            //
            // Catches
            //     : catch [ "(" [ Identifier ":" ] Type ")" ] { Statement }
            //
            // Else
            //     : else { Statement }
            case TokenCode.Try:
                TRY.parse(context);
                break;

            // Statement
            //     : ...
            //     | ? Identifier
            //     | ...
            //
            case TokenCode.Question:

                break;

            case TokenCode.Init:
                // Initializer call
                forget();
                Token start = token;

                DECLARATION init = Context.find(INITIALIZER.initName);
                EXPRESSION  initRef;
                if (init != null)
                {
                    initRef = new REFERENCE(init);
                }
                else
                {
                    initRef = new UNRESOLVED(context, new IDENTIFIER(INITIALIZER.initName));
                }

                CALL call = new CALL(initRef);
                token = expect(TokenCode.LParen);
                while (true)
                {
                    EXPRESSION argument = EXPRESSION.parse(null, context);
                    call.add(argument);
                    argument.parent = call;

                    token = get();
                    if (token.code == TokenCode.Comma)
                    {
                        forget(); continue;
                    }
                    break;
                }
                token = expect(TokenCode.RParen);
                call.setSpan(start.span, token.span);
                context.add(call);
                break;

            case TokenCode.Identifier:
            {
                // Several different cases:
                //   - a label in front of while/loop
                //   - a declaration
                //   - a statement

                forget();
                Token next = get();
                if (next.code == TokenCode.LParen)
                {
                    forget();
                    TokenCode codeAfter = saveTokensUntilRightParenth(next);
                    switch (codeAfter)
                    {
                    case TokenCode.Colon:
                    case TokenCode.Is:
                    // case TokenCode.Do:
                    case TokenCode.Arrow:
                        //This as a routine declaration!!
                        ROUTINE.parse(token, false, false, false, 0, context);
                        goto Weiter;
                    }
                }
                EXPRESSION attempt = EXPRESSION.parse(token, context);
                if (attempt is UNRESOLVED)
                {
                    // Might be a label or a declaration...
                    Token idd = new Token(attempt.span, TokenCode.Identifier,
                                          (attempt as UNRESOLVED).name.identifier,
                                          new Category(CategoryCode.identifier));
                    token = get();
                    switch (token.code)
                    {
                    case TokenCode.Is:
                    case TokenCode.Comma:
                        // This is definitely a declaration
                        forget();
                        VARIABLE.parse(false, false, false, false, idd, token, context);
                        goto Weiter;

                    case TokenCode.Colon:
                        forget();
                        Token token2 = get();
                        if (token2.code == TokenCode.While || token2.code == TokenCode.Loop)
                        {
                            // This is a real label! Don't 'forget()'.
                            LOOP.parse(idd, context);
                        }
                        else
                        {
                            // This is definitely a variable declaration.
                            // Don't forget()
                            VARIABLE.parse(false, false, false, false, idd, token, context);
                        }
                        goto Weiter;

                    default:
                        // Nothing to do; just going further
                        break;
                    }
                }
                // 'attempt' is something else: a call or the left part of an assignment
                token = get();
                if (token.code == TokenCode.Assign)
                {
                    forget();
                    EXPRESSION right = EXPRESSION.parse(null, context);
                    ASSIGNMENT res   = new ASSIGNMENT(attempt, right);
                    res.setSpan(attempt.span, right.span);
                    context.add(res);
                }
                else
                {
                    if (!(attempt is CALL))       // something's wrong
                    {
                        result = false;
                    }
                    context.add(attempt);
                }
Weiter:
                break;
            }

            case TokenCode.Const:
                // Something like
                //     const a is 5...
                // OR
                //     const is a, b, ... end
                forget(); token = get();
                if (token.code == TokenCode.Is)
                {
                    forget();
                    CONSTANT.parse(context);
                }
                else
                {
                    VARIABLE.parse(false, false, true, false, null, null, context);
                }
                break;

            default:
                // Something else, e.g., (a... or this...
                // Either a function call or an assignment.
                //
                //     this := ...
                //     (if cond then a else b).f ...
                //     ^

                EXPRESSION e = EXPRESSION.parse(null, context);
                if (e == null)
                {
                    // Either an error or just the end of statement sequence
                    result = false;
                    break;
                }
                token = get();
                if (token.code == TokenCode.Assign)
                {
                    forget();
                    EXPRESSION right      = EXPRESSION.parse(null, context);
                    ASSIGNMENT assignment = new ASSIGNMENT(e, right);
                    assignment.setSpan(e.span, right.span);
                    context.add(assignment);
                }
                else
                {
                    context.add(e);
                }
                break;
            }
Finish:
            Debug.WriteLine("Exiting STATEMENT.parse");
            Debug.Unindent();

            return(result);
        }