+ }
+
+ if (!parser_next(parser)) {
+ if (swcase.value) ast_unref(swcase.value);
+ ast_delete(switchnode);
+ parseerror(parser, "expected statements or case");
+ return false;
+ }
+ caseblock = ast_block_new(parser_ctx(parser));
+ if (!caseblock) {
+ if (swcase.value) ast_unref(swcase.value);
+ ast_delete(switchnode);
+ return false;
+ }
+ swcase.code = (ast_expression*)caseblock;
+ vec_push(switchnode->cases, swcase);
+ while (true) {
+ ast_expression *expr;
+ if (parser->tok == '}')
+ break;
+ if (parser->tok == TOKEN_KEYWORD) {
+ if (!strcmp(parser_tokval(parser), "case") ||
+ !strcmp(parser_tokval(parser), "default"))
+ {
+ break;
+ }
+ }
+ if (!parse_statement(parser, caseblock, &expr, true)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ if (!expr)
+ continue;
+ if (!ast_block_add_expr(caseblock, expr)) {
+ ast_delete(switchnode);
+ return false;
+ }
+ }
+ }
+
+ parser_leaveblock(parser);
+
+ /* closing paren */
+ if (parser->tok != '}') {
+ ast_delete(switchnode);
+ parseerror(parser, "expected closing paren of case list");
+ return false;
+ }
+ if (!parser_next(parser)) {
+ ast_delete(switchnode);
+ parseerror(parser, "parse error after switch");
+ return false;
+ }
+ *out = (ast_expression*)switchnode;
+ return true;
+}
+
+/* parse computed goto sides */
+static ast_expression *parse_goto_computed(parser_t *parser, ast_expression **side) {
+ ast_expression *on_true;
+ ast_expression *on_false;
+ ast_expression *cond;
+
+ if (!*side)
+ return NULL;
+
+ if (ast_istype(*side, ast_ternary)) {
+ ast_ternary *tern = (ast_ternary*)*side;
+ on_true = parse_goto_computed(parser, &tern->on_true);
+ on_false = parse_goto_computed(parser, &tern->on_false);
+
+ if (!on_true || !on_false) {
+ parseerror(parser, "expected label or expression in ternary");
+ if (on_true) ast_unref(on_true);
+ if (on_false) ast_unref(on_false);
+ return NULL;
+ }
+
+ cond = tern->cond;
+ tern->cond = NULL;
+ ast_delete(tern);
+ *side = NULL;
+ return (ast_expression*)ast_ifthen_new(parser_ctx(parser), cond, on_true, on_false);
+ } else if (ast_istype(*side, ast_label)) {
+ ast_goto *gt = ast_goto_new(parser_ctx(parser), ((ast_label*)*side)->name);
+ ast_goto_set_label(gt, ((ast_label*)*side));
+ *side = NULL;
+ return (ast_expression*)gt;
+ }
+ return NULL;
+}
+
+static bool parse_goto(parser_t *parser, ast_expression **out)
+{
+ ast_goto *gt = NULL;
+ ast_expression *lbl;
+
+ if (!parser_next(parser))
+ return false;
+
+ if (parser->tok != TOKEN_IDENT) {
+ ast_expression *expression;
+
+ /* could be an expression i.e computed goto :-) */
+ if (parser->tok != '(') {
+ parseerror(parser, "expected label name after `goto`");
+ return false;
+ }
+
+ /* failed to parse expression for goto */
+ if (!(expression = parse_expression(parser, false, true)) ||
+ !(*out = parse_goto_computed(parser, &expression))) {
+ parseerror(parser, "invalid goto expression");
+ ast_unref(expression);
+ return false;
+ }
+
+ return true;
+ }
+
+ /* not computed goto */
+ gt = ast_goto_new(parser_ctx(parser), parser_tokval(parser));
+ lbl = parser_find_label(parser, gt->name);
+ if (lbl) {
+ if (!ast_istype(lbl, ast_label)) {
+ parseerror(parser, "internal error: label is not an ast_label");
+ ast_delete(gt);
+ return false;
+ }
+ ast_goto_set_label(gt, (ast_label*)lbl);
+ }
+ else
+ vec_push(parser->gotos, gt);
+
+ if (!parser_next(parser) || parser->tok != ';') {
+ parseerror(parser, "semicolon expected after goto label");
+ return false;
+ }
+ if (!parser_next(parser)) {
+ parseerror(parser, "parse error after goto");
+ return false;
+ }
+
+ *out = (ast_expression*)gt;
+ return true;
+}
+
+static bool parse_skipwhite(parser_t *parser)
+{
+ do {
+ if (!parser_next(parser))
+ return false;
+ } while (parser->tok == TOKEN_WHITE && parser->tok < TOKEN_ERROR);
+ return parser->tok < TOKEN_ERROR;
+}