diff --git a/sh.c b/sh.c index 341d6da9..01476a25 100644 --- a/sh.c +++ b/sh.c @@ -1866,8 +1866,14 @@ bool comp_switch(ast node) { node = get_child_(SWITCH_KW, node, 1); - if (node == 0 || get_op(node) != '{') fatal_error("comp_statement: switch without body"); + if(get_op(node) == CASE_KW) { + // This is for the edge case where the entire 'statement' part of < switch ( expression ) statement > + // is a single < case constant-expression : statement > + // therefore we wrap the case statement with a block statement to simplify down to the typical syntax + node = new_ast2('{', node, 0); + } + if (node == 0 || get_op(node) != '{') fatal_error("comp_statement: switch without body"); while (get_op(node) == '{') { statement = get_child_('{', node, 0); node = get_child_('{', node, 1); diff --git a/tests/_exe/switch.c b/tests/_exe/switch.c index 851b4409..843f727a 100644 --- a/tests/_exe/switch.c +++ b/tests/_exe/switch.c @@ -215,6 +215,17 @@ void state_machine_switch() { } } +void bodiless_switch() { + switch (123) + case 123: + putchar('E'); + + switch (456) + case 123: + case 456: + putchar('E'); +} + int main() { empty_switch(); putchar('\n'); no_case_switch(); putchar('\n'); @@ -229,5 +240,6 @@ int main() { duff_device_switch(15); putchar('\n'); state_machine_switch(); state_machine_switch(); putchar('\n'); + bodiless_switch(); putchar('\n'); return 0; } diff --git a/tests/_exe/switch.golden b/tests/_exe/switch.golden index 1fcef192..db32f51e 100644 --- a/tests/_exe/switch.golden +++ b/tests/_exe/switch.golden @@ -10,3 +10,4 @@ ABCDE DT BCDEFGHABCDEFGH ABA +EE diff --git a/tests/_sh/switch.c b/tests/_sh/switch.c index ba57458e..4f4b5ba0 100644 --- a/tests/_sh/switch.c +++ b/tests/_sh/switch.c @@ -93,9 +93,21 @@ void switch_in_while(){ } } +void bodiless_switch() { + switch (123) + case 123: + putchar('F'); + + switch (456) + case 123: + case 456: + putchar('E'); +} + void main() { simple_switch(); simple_switch_fall_through(); multiple_labels(); switch_in_while(); + bodiless_switch(); } diff --git a/tests/_sh/switch.golden b/tests/_sh/switch.golden index ff841f31..3134ac1d 100644 --- a/tests/_sh/switch.golden +++ b/tests/_sh/switch.golden @@ -1 +1 @@ -BCCBABCDE \ No newline at end of file +BCCBABCDEFE \ No newline at end of file