135 lines
1.9 KiB
Plaintext
135 lines
1.9 KiB
Plaintext
// tests for basic control flow constructs
|
|
|
|
// if, else
|
|
|
|
//expect:true
|
|
if (true) {
|
|
print "true";
|
|
};
|
|
|
|
if (false) {
|
|
print "false";
|
|
};
|
|
|
|
// return the condition if falsy
|
|
|
|
//expect:nil
|
|
print if (nil) 5;
|
|
|
|
//expect:false
|
|
print if (false) 5;
|
|
|
|
// return body if truthy
|
|
|
|
//expect:5.0
|
|
print if (true) 5;
|
|
|
|
// return else body if falsey and present
|
|
|
|
//expect:6.0
|
|
print if (false) 5 else 6;
|
|
|
|
// but still only return the if body if truthy
|
|
|
|
//expect:4.0
|
|
print if (true) 4 else 6;
|
|
|
|
// elseif chains
|
|
|
|
//expect:4.0
|
|
print if (false) 1 else if (false) 2 else if (false) 3 else if (true) 4 else if (false) 5 else 8;
|
|
|
|
// falsiness, truthiness
|
|
|
|
// false and nil are falsey
|
|
|
|
var uninitialized;
|
|
|
|
if (false) print "don't see this";
|
|
if (nil) print "don't see this";
|
|
if (uninitialized) print "don't see this";
|
|
|
|
// the rest of the types are truthy
|
|
|
|
if (true) print "1";
|
|
if ("") print "2";
|
|
if ("hello") print "3";
|
|
if (0) print "4";
|
|
if (1) print "5";
|
|
if (@[]) print "6";
|
|
if (@["hi"]) print "7";
|
|
if (@{}) print "8";
|
|
if (@{"hi" = 5}) print "9";
|
|
if (funct(n) print n) print "10";
|
|
|
|
//expect:1
|
|
//expect:2
|
|
//expect:3
|
|
//expect:4
|
|
//expect:5
|
|
//expect:6
|
|
//expect:7
|
|
//expect:8
|
|
//expect:9
|
|
//expect:10
|
|
|
|
// and, or
|
|
|
|
// and returns the left one if it's falsey or the right one if the left one is truthy
|
|
|
|
//expect:false
|
|
print false and 5;
|
|
|
|
//expect:5.0
|
|
print true and 5;
|
|
|
|
// or returns the leftmost truthy
|
|
|
|
//expect:5.0
|
|
print false or false or false or false or 5 or false or 7;
|
|
|
|
//expect:true
|
|
print true or false;
|
|
|
|
// while
|
|
|
|
// basic looping examples
|
|
|
|
var i = 1;
|
|
|
|
while (i < 300) {
|
|
i = i + 1;
|
|
};
|
|
|
|
print i; //expect:300.0
|
|
|
|
|
|
i = 5;
|
|
while (i > 0)
|
|
print i = i - 1
|
|
;
|
|
//expect:4.0
|
|
//expect:3.0
|
|
//expect:2.0
|
|
//expect:1.0
|
|
//expect:0.0
|
|
|
|
// while returns the result of the last iteration
|
|
|
|
i = 5;
|
|
var res = while (i > 0)
|
|
i = i - 1
|
|
;
|
|
|
|
//expect:0.0
|
|
print res;
|
|
|
|
// if no iterations are done, it returns nil
|
|
|
|
res = while (false)
|
|
i = 6
|
|
;
|
|
|
|
//expect:nil
|
|
print res;
|