Ejemplo n.º 1
0
        Stmt *new_stmt_block(SrcPos pos, StmtList block)
        {
            var s = new_stmt(STMT_BLOCK, pos);

            s->block = block;
            return(s);
        }
Ejemplo n.º 2
0
        Stmt *new_stmt_do_while(SrcPos pos, Expr *cond, StmtList block)
        {
            var s = new_stmt(STMT_DO_WHILE, pos);

            s->while_stmt.cond  = cond;
            s->while_stmt.block = block;
            return(s);
        }
Ejemplo n.º 3
0
        Stmt *new_stmt_for(SrcPos pos, Stmt *init, Expr *cond, Stmt *next, StmtList block)
        {
            var s = new_stmt(STMT_FOR, pos);

            s->for_stmt.init  = init;
            s->for_stmt.cond  = cond;
            s->for_stmt.next  = next;
            s->for_stmt.block = block;
            return(s);
        }
Ejemplo n.º 4
0
        Stmt *new_stmt_if(SrcPos pos, Stmt *init, Expr *cond, StmtList then_block, ElseIf **elseifs, int num_elseifs,
                          StmtList else_block)
        {
            var s = new_stmt(STMT_IF, pos);

            s->if_stmt.cond        = cond;
            s->if_stmt.init        = init;
            s->if_stmt.then_block  = then_block;
            s->if_stmt.elseifs     = elseifs;
            s->if_stmt.num_elseifs = num_elseifs;
            s->if_stmt.else_block  = else_block;
            return(s);
        }
Ejemplo n.º 5
0
        void print_stmt_block(StmtList block)
        {
            printf("(block");
            _indent++;
            for (var it = block.stmts; it != block.stmts + block.num_stmts; it++)
            {
                print_newline();
                print_stmt(*it);
            }

            _indent--;
            printf(")");
        }
Ejemplo n.º 6
0
        SwitchCase parse_stmt_switch_case()
        {
            var patterns = Buffer <SwitchCasePattern> .Create();

            var is_default    = false;
            var is_first_case = true;

            while (is_keyword(case_keyword) || is_keyword(default_keyword))
            {
                if (match_keyword(case_keyword))
                {
                    if (!is_first_case)
                    {
                        warning_here("Use comma-separated expressions to match multiple values with one case label");
                        is_first_case = false;
                    }
                    patterns.Add(parse_switch_case_pattern());
                    while (match_token(TOKEN_COMMA))
                    {
                        patterns.Add(parse_switch_case_pattern());
                    }
                }
                else
                {
                    assert(is_keyword(default_keyword));
                    next_token();
                    if (is_default)
                    {
                        error_here("Duplicate default labels in same switch clause");
                    }

                    is_default = true;
                }

                expect_token(TOKEN_COLON);
            }

            var pos = token.pos;

            var buf = PtrBuffer.Create();

            while (!is_token_eof() && !is_token(TOKEN_RBRACE) && !is_keyword(case_keyword) &&
                   !is_keyword(default_keyword))
            {
                buf->Add(parse_stmt());
            }

            var block = new StmtList
            {
                pos       = pos,
                stmts     = (Stmt **)buf->_begin,
                num_stmts = buf->count
            };

            return(new SwitchCase {
                patterns = patterns,
                num_patterns = patterns.count,
                is_default = is_default,
                block = block
            });
        }
Ejemplo n.º 7
0
        Decl *parse_decl_func(SrcPos pos)
        {
            var       name         = parse_name();
            bool      has_varargs  = false;
            Typespec *varargs_type = null;

            expect_token(TOKEN_LPAREN);
            var buf = Buffer <FuncParam> .Create();

            if (!is_token(TOKEN_RPAREN))
            {
                buf.Add(parse_decl_func_param());
                while (match_token(TOKEN_COMMA))
                {
                    if (match_token(TOKEN_ELLIPSIS))
                    {
                        if (has_varargs)
                        {
                            error_here("Multiple ellipsis in function declaration");
                        }
                        if (!is_token(TOKEN_RPAREN))
                        {
                            varargs_type = parse_type();
                        }
                        has_varargs = true;
                    }
                    else
                    {
                        if (has_varargs)
                        {
                            error_here("Ellipsis must be last parameter in function declaration");
                        }
                        buf.Add(parse_decl_func_param());
                    }
                }
            }

            expect_token(TOKEN_RPAREN);
            Typespec *ret_type = null;

            if (match_token(TOKEN_COLON))
            {
                ret_type = parse_type();
            }

            StmtList block = default;
            bool     is_incomplete;

            if (match_token(TOKEN_SEMICOLON))
            {
                is_incomplete = true;
            }
            else
            {
                block         = parse_stmt_block();
                is_incomplete = false;
            }
            var decl = new_decl_func(pos, name, buf, buf.count, ret_type, has_varargs, varargs_type, block);

            decl->is_incomplete = is_incomplete;
            return(decl);
        }
Ejemplo n.º 8
0
        Decl *new_decl_func(SrcPos pos, char *name, FuncParam * @params, int num_params, Typespec *ret_type, bool has_varargs, Typespec *varargs_type, StmtList block)
        {
            var d = new_decl(DECL_FUNC, pos, name);

            d->func.@params      = @params;
            d->func.num_params   = num_params;
            d->func.ret_type     = ret_type;
            d->func.has_varargs  = has_varargs;
            d->func.varargs_type = varargs_type;
            d->func.block        = block;
            return(d);
        }