nondescript/tests/sugar.nds

108 lines
1.6 KiB
Plaintext

// testing syntactic sugars
// :: piping procion call
var double = proc(num) num * 2;
var four = 2 :: double();
var multiply = proc(num, factor) num * factor;
var six = 2 :: multiply(3);
print (four);
print (six);
//expect:4.0
//expect:6.0
print ((1 + 2 * 3 :: tostring()) + "2");
//expect:7.02
// indexing tables with .
var nested = @{
lvl1 = @{
lvl2 = @{
lvl3 = 5
}
}
};
nested.lvl1.lvl2.lvl3 = 6;
print(nested.lvl1.lvl2.lvl3);
//expect:6.0
nested["lvl1"].lvl2["lvl3"] = 2;
print(nested.lvl1["lvl2"].lvl3);
//expect:2.0
nested["lvl1"]["lvl2"]["lvl3"] = -5.4;
nested .lvl1 .lvl2 .lvl3 :: print;
//expect:-5.4
// creating tables with : and no [] idents
var mix = @{
ident = 5,
ident2: 6,
[3] = 7,
["ident4"]: 8,
};
print(mix.ident, mix["ident2"], mix[3], mix.ident4);
//expect:5.0 6.0 7.0 8.0
// : method call syntax
// formerly ->
var class = @{
method = proc(self) print ("i was called")
};
// no args needed then no parentheses needed
class:method;
//expect:i was called
var multiplier = @{
multiple = 5,
do = proc(self, arg) self.multiple * arg,
};
multiplier:do(7) :: print;
//expect:35.0
// -> method call syntax with :: - check for precedence
var returner = @{
method = proc(self) proc(n) n * 2
};
1.7 :: (returner:method) :: print;
//expect:3.4
print("finish");
//expect:finish
//convoluted :: test
(proc() print("merry christmas")) :: proc(it) it();
//expect:merry christmas
// proc declaration
proc globalProc(a, b, c)
a + b * c
;
{
proc localProc(d, e, f)
2 * d + 3 * e + 4 * f
;
print(globalProc(2, 3, 4));
//expect:14.0
print(localProc(0.2, 0.3, 0.5));
//expect:3.3
};