summaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2020-11-05 22:56:14 +0100
committerJo-Philipp Wich <jo@mein.io>2020-11-05 23:17:03 +0100
commitf6869ee3b02a60b202c703f7caef165ee3845e5a (patch)
treee618e1bbb93c10fc45100afada4ed537e5655733 /tests
parentfeb815bc1a058b91eed9dea3c5cad8ea52d51806 (diff)
eval: rework handling of list expressions
Tune the grammar and rework the VM to properly yield the last result of list expressions in various contexts. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/00_syntax/20_list_expressions41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/00_syntax/20_list_expressions b/tests/00_syntax/20_list_expressions
new file mode 100644
index 0000000..ef1b0c4
--- /dev/null
+++ b/tests/00_syntax/20_list_expressions
@@ -0,0 +1,41 @@
+Similar to ES5, utpl's language grammar allows comma separated list expressions
+in various contexts. Unless such lists happen to be part of a function call
+or array construction expression, only the last result of such an expression
+list should be used while still evaluating all sub-expressions, triggering
+side effects such as function calls or variable assignments.
+
+-- Expect stdout --
+4
+[ 1, 3 ]
+{ "a": true, "b": 1 }
+function call
+[ "test", "assigment" ]
+true
+true
+true
+-- End --
+
+-- Testcase --
+{%
+ // only the last value is considered
+ print(1 + (2, 3), "\n");
+
+ // in array constructors, parenthesized lists are reduced to the last value
+ print([ (0, 1), (2, 3) ], "\n");
+
+ // in object constructors, parenthesized lists are reduced to the last value
+ print({ a: (false, true), b: (0, 1) }, "\n");
+
+ // all list expressions are evaluated and may have side effects, even if
+ // results are discareded
+ x = (print("function call\n"), y = "assigment", "test");
+ print([x, y], "\n");
+
+ // property access operates on the last value of a parenthesized list expression
+ print(({foo: false}, {foo: true}).foo, "\n");
+ print(({foo: false}, {foo: true})["foo"], "\n");
+
+ // computed property access uses the last list expression value
+ print(({foo: true})["bar", "baz", "foo"], "\n");
+%}
+-- End --