nondescript/tests/controlflow.nds

135 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

2022-02-05 02:45:29 +01:00
// tests for basic control flow constructs
// if, else
//expect:true
if (true) {
2022-02-07 05:35:07 +01:00
print ("true");
2022-02-05 02:45:29 +01:00
};
if (false) {
2022-02-07 05:35:07 +01:00
print ("false");
2022-02-05 02:45:29 +01:00
};
// return the condition if falsy
//expect:nil
2022-02-07 05:35:07 +01:00
print (if (nil) 5);
2022-02-05 02:45:29 +01:00
//expect:false
2022-02-07 05:35:07 +01:00
print (if (false) 5);
2022-02-05 02:45:29 +01:00
// return body if truthy
//expect:5.0
2022-02-07 05:35:07 +01:00
print (if (true) 5);
2022-02-05 02:45:29 +01:00
// return else body if falsey and present
//expect:6.0
2022-02-07 05:35:07 +01:00
print (if (false) 5 else 6);
2022-02-05 02:45:29 +01:00
// but still only return the if body if truthy
//expect:4.0
2022-02-07 05:35:07 +01:00
print (if (true) 4 else 6);
2022-02-05 02:45:29 +01:00
// elseif chains
//expect:4.0
2022-02-07 05:35:07 +01:00
print (if (false) 1 else if (false) 2 else if (false) 3 else if (true) 4 else if (false) 5 else 8);
2022-02-05 02:45:29 +01:00
// falsiness, truthiness
// false and nil are falsey
var uninitialized;
2022-02-07 05:35:07 +01:00
if (false) print ("don't see this");
if (nil) print ("don't see this");
if (uninitialized) print ("don't see this");
2022-02-05 02:45:29 +01:00
// the rest of the types are truthy
2022-02-07 05:35:07 +01:00
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");
2022-02-08 08:56:47 +01:00
if (@{["hi"] = 5}) print ("9");
2022-02-09 06:58:51 +01:00
if (proc(n) print (n)) print ("10");
2022-02-05 02:45:29 +01:00
//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
2022-02-07 05:35:07 +01:00
print (false and 5);
2022-02-05 02:45:29 +01:00
//expect:5.0
2022-02-07 05:35:07 +01:00
print (true and 5);
2022-02-05 02:45:29 +01:00
// or returns the leftmost truthy
//expect:5.0
2022-02-07 05:35:07 +01:00
print (false or false or false or false or 5 or false or 7);
2022-02-05 02:45:29 +01:00
//expect:true
2022-02-07 05:35:07 +01:00
print (true or false);
2022-02-05 02:45:29 +01:00
// while
// basic looping examples
var i = 1;
while (i < 300) {
i = i + 1;
};
2022-02-07 05:35:07 +01:00
print (i); //expect:300.0
2022-02-05 02:45:29 +01:00
i = 5;
while (i > 0)
2022-02-07 05:35:07 +01:00
print (i = i - 1)
2022-02-05 02:45:29 +01:00
;
//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
2022-02-07 05:35:07 +01:00
print (res);
2022-02-05 02:45:29 +01:00
// if no iterations are done, it returns nil
res = while (false)
i = 6
;
//expect:nil
2022-02-07 05:35:07 +01:00
print (res);