summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-09-22 14:23:01 +0200
committerJo-Philipp Wich <jo@mein.io>2020-09-22 14:23:01 +0200
commitb4c605e8ab49e35dfbb7caa179eaa273c696ac9f (patch)
tree1b9e58a2c473709f7ef18f7746284c92c1218d43
parentd36709f63e4bae54d5c9c3defd35eb3e23e64260 (diff)
syntax: allow empty switch statements
Also add testcases for switch/case blocks. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--parser.y2
-rw-r--r--tests/02_runtime/04_switch_case179
2 files changed, 181 insertions, 0 deletions
diff --git a/parser.y b/parser.y
index 1d71a16..938a341 100644
--- a/parser.y
+++ b/parser.y
@@ -177,6 +177,8 @@ try_catch_block(A) ::= empty_object. { A = 0; }
switch_stmt(A) ::= T_SWITCH(B) T_LPAREN exp(C) T_RPAREN T_LBRACE switch_cases(D) T_RBRACE.
{ A = wrap_op(B, C, D); }
+switch_stmt(A) ::= T_SWITCH T_LPAREN exp(B) T_RPAREN empty_object.
+ { A = B; }
switch_cases(A) ::= switch_cases(B) switch_case(C). { A = append_op(B, C); }
switch_cases(A) ::= switch_case(B). { A = B; }
diff --git a/tests/02_runtime/04_switch_case b/tests/02_runtime/04_switch_case
new file mode 100644
index 0000000..9a871fd
--- /dev/null
+++ b/tests/02_runtime/04_switch_case
@@ -0,0 +1,179 @@
+Testing utpl switch statements.
+
+
+1. Ensure that execution starts at the first matching case.
+
+-- Expect stdout --
+1a
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ case 1:
+ print("1a\n");
+ break;
+
+ case 1:
+ print("1b\n");
+ break;
+
+ case 2:
+ print("2\n");
+ break;
+ }
+%}
+-- End --
+
+
+2. Ensure that default case is only used if no case matches,
+ even if declared first.
+
+-- Expect stdout --
+1
+default
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1, 3]) {
+ switch (n) {
+ default:
+ print("default\n");
+ break;
+
+ case 1:
+ print("1\n");
+ break;
+
+ case 2:
+ print("2\n");
+ break;
+ }
+ }
+%}
+-- End --
+
+
+3. Ensure that cases without break fall through into
+ subsequent cases.
+
+-- Expect stdout --
+1
+2
+default
+1
+2
+-- End --
+
+-- Testcase --
+{%
+ for (n in [1, 3]) {
+ switch (n) {
+ default:
+ print("default\n");
+
+ case 1:
+ print("1\n");
+
+ case 2:
+ print("2\n");
+ }
+ }
+%}
+-- End --
+
+
+4. Ensure that duplicate default cases emit a syntax
+ error during parsing.
+
+-- Expect stderr --
+Syntax error: more than one switch default case
+In line 6, byte 9:
+
+ ` default:`
+ Near here -----^
+
+
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ default:
+ print("default1\n");
+
+ default:
+ print("default2\n");
+ }
+%}
+-- End --
+
+
+5. Ensure that case values use strict comparison.
+
+-- Expect stdout --
+b
+b
+-- End --
+
+-- Testcase --
+{%
+ switch (1.0) {
+ case 1:
+ print("a\n");
+ break;
+
+ case 1.0:
+ print("b\n");
+ break;
+ }
+
+ switch ("123") {
+ case 123:
+ print("a\n");
+ break;
+
+ case "123":
+ print("b\n");
+ break;
+ }
+%}
+-- End --
+
+
+6. Ensure that case values may be complex expressions.
+
+-- Expect stdout --
+2, 3, 1
+-- End --
+
+-- Testcase --
+{%
+ switch (1) {
+ case a = 2, b = 3, c = 1:
+ print(join(", ", [ a, b, c ]), "\n");
+ break;
+ }
+%}
+-- End --
+
+
+7. Ensure that empty switch statements are accepted by the
+ parser and that the test expression is evaluated.
+
+-- Expect stdout --
+true
+-- End --
+
+-- Testcase --
+{%
+ x = false;
+
+ switch (x = true) {
+
+ }
+
+ print(x, "\n");
+%}
+-- End --