self->ir_func = NULL;
self->curblock = NULL;
- self->breakblock = NULL;
- self->continueblock = NULL;
+ self->breakblocks = NULL;
+ self->continueblocks = NULL;
vtype->hasvalue = true;
vtype->constval.vfunc = self;
for (i = 0; i < vec_size(self->blocks); ++i)
ast_delete(self->blocks[i]);
vec_free(self->blocks);
+ vec_free(self->breakblocks);
+ vec_free(self->continueblocks);
mem_d(self);
}
ir_block *bcontinue = NULL;
ir_block *bbreak = NULL;
- ir_block *old_bcontinue = NULL;
- ir_block *old_bbreak = NULL;
-
ir_block *tmpblock = NULL;
(void)lvalue;
/* enter */
func->curblock = bbody;
- old_bbreak = func->breakblock;
- old_bcontinue = func->continueblock;
- func->breakblock = bbreak;
- func->continueblock = bcontinue;
- if (!func->continueblock)
- func->continueblock = bbody;
+ vec_push(func->breakblocks, bbreak);
+ if (bcontinue)
+ vec_push(func->continueblocks, bcontinue);
+ else
+ vec_push(func->continueblocks, bbody);
/* generate */
if (self->body) {
}
end_bbody = func->curblock;
- func->breakblock = old_bbreak;
- func->continueblock = old_bcontinue;
+ vec_pop(func->breakblocks);
+ vec_pop(func->continueblocks);
}
/* post-loop-condition */
self->expression.outr = (ir_value*)1;
if (self->is_continue)
- target = func->continueblock;
+ target = func->continueblocks[vec_size(func->continueblocks)-1-self->levels];
else
- target = func->breakblock;
+ target = func->breakblocks[vec_size(func->breakblocks)-1-self->levels];
if (!target) {
compile_error(ast_ctx(self), "%s is lacking a target block", (self->is_continue ? "continue" : "break"));
ir_value *dummy = NULL;
ir_value *irop = NULL;
- ir_block *old_break = NULL;
ir_block *bout = NULL;
ir_block *bfall = NULL;
size_t bout_id;
return false;
/* setup the break block */
- old_break = func->breakblock;
- func->breakblock = bout;
+ vec_push(func->breakblocks, bout);
/* Now create all cases */
for (c = 0; c < vec_size(self->cases); ++c) {
func->curblock = bout;
/* restore the break block */
- func->breakblock = old_break;
+ vec_pop(func->breakblocks);
/* Move 'bout' to the end, it's nicer */
vec_remove(func->ir_func->blocks, bout_id, 1);
ir_function *ir_func;
ir_block *curblock;
- ir_block *breakblock;
- ir_block *continueblock;
+ ir_block **breakblocks;
+ ir_block **continueblocks;
#if 0
/* In order for early-out logic not to go over
(void)block; /* not touching */
- /* skip the 'while' and check for opening paren */
- if (!parser_next(parser) || parser->tok != '(') {
- parseerror(parser, "expected 'while' condition in parenthesis");
- return false;
- }
/* parse into the expression */
if (!parser_next(parser)) {
parseerror(parser, "expected 'while' condition after opening paren");
return true;
}
+static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out);
static bool parse_switch(parser_t *parser, ast_block *block, ast_expression **out)
+{
+ bool rv;
+ char *label = NULL;
+
+ /* skip the 'while' and get the body */
+ if (!parser_next(parser)) {
+ if (OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "expected loop label or 'switch' operand in parenthesis");
+ else
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+
+ if (parser->tok == ':') {
+ if (!OPTS_FLAG(LOOP_LABELS))
+ parseerror(parser, "labeled loops not activated, try using -floop-labels");
+ if (!parser_next(parser) || parser->tok != TOKEN_IDENT) {
+ parseerror(parser, "expected loop label");
+ return false;
+ }
+ label = util_strdup(parser_tokval(parser));
+ if (!parser_next(parser)) {
+ mem_d(label);
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+ }
+
+ if (parser->tok != '(') {
+ parseerror(parser, "expected 'switch' operand in parenthesis");
+ return false;
+ }
+
+ vec_push(parser->breaks, label);
+
+ rv = parse_switch_go(parser, block, out);
+ if (label)
+ mem_d(label);
+ if (vec_last(parser->breaks) != label) {
+ parseerror(parser, "internal error: label stack corrupted");
+ rv = false;
+ ast_delete(*out);
+ *out = NULL;
+ }
+ else {
+ vec_pop(parser->breaks);
+ }
+ return rv;
+}
+
+static bool parse_switch_go(parser_t *parser, ast_block *block, ast_expression **out)
{
ast_expression *operand;
ast_value *opval;
(void)block; /* not touching */
(void)opval;
- /* parse over the opening paren */
- if (!parser_next(parser) || parser->tok != '(') {
- parseerror(parser, "expected switch operand in parenthesis");
- return false;
- }
-
/* parse into the expression */
if (!parser_next(parser)) {
parseerror(parser, "expected switch operand");