• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/20_list_expressions

  1 Similar to ES5, ucode's language grammar allows comma separated list expressions
  2 in various contexts. Unless such lists happen to be part of a function call
  3 or array construction expression, only the last result of such an expression
  4 list should be used while still evaluating all sub-expressions, triggering
  5 side effects such as function calls or variable assignments.
  6 
  7 -- Expect stdout --
  8 4
  9 [ 1, 3 ]
 10 { "a": true, "b": 1 }
 11 function call
 12 [ "test", "assigment" ]
 13 true
 14 true
 15 true
 16 [ 2, 3 ]
 17 -- End --
 18 
 19 -- Testcase --
 20 {%
 21         // only the last value is considered
 22         print(1 + (2, 3), "\n");
 23 
 24         // in array constructors, parenthesized lists are reduced to the last value
 25         print([ (0, 1), (2, 3) ], "\n");
 26 
 27         // in object constructors, parenthesized lists are reduced to the last value
 28         print({ a: (false, true), b: (0, 1) }, "\n");
 29 
 30         // all list expressions are evaluated and may have side effects, even if
 31         // results are discareded
 32         x = (print("function call\n"), y = "assigment", "test");
 33         print([x, y], "\n");
 34 
 35         // property access operates on the last value of a parenthesized list expression
 36         print(({foo: false}, {foo: true}).foo, "\n");
 37         print(({foo: false}, {foo: true})["foo"], "\n");
 38 
 39         // computed property access uses the last list expression value
 40         print(({foo: true})["bar", "baz", "foo"], "\n");
 41 
 42         // same list semantics apply to function call parameters
 43         ((...args) => print(args, "\n"))((1, 2), 3);
 44 %}
 45 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt