nondescript/tests/sugar.nds

108 lines
1.6 KiB
Plaintext
Raw Permalink Normal View History

// testing syntactic sugars
2022-02-08 10:10:07 +01:00
2022-02-09 06:58:51 +01:00
// :: piping procion call
2022-02-09 06:58:51 +01:00
var double = proc(num) num * 2;
var four = 2 :: double();
2022-02-09 06:58:51 +01:00
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 = @{
2022-02-08 08:56:47 +01:00
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
2022-02-08 09:23:21 +01:00
// 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 ->
2022-02-08 09:23:21 +01:00
var class = @{
2022-02-09 06:58:51 +01:00
method = proc(self) print ("i was called")
2022-02-08 09:23:21 +01:00
};
// no args needed then no parentheses needed
class:method;
2022-02-08 09:23:21 +01:00
//expect:i was called
var multiplier = @{
multiple = 5,
2022-02-09 06:58:51 +01:00
do = proc(self, arg) self.multiple * arg,
2022-02-08 09:23:21 +01:00
};
multiplier:do(7) :: print;
2022-02-08 09:23:21 +01:00
//expect:35.0
// -> method call syntax with :: - check for precedence
var returner = @{
2022-02-09 06:58:51 +01:00
method = proc(self) proc(n) n * 2
2022-02-08 09:23:21 +01:00
};
1.7 :: (returner:method) :: print;
2022-02-08 09:23:21 +01:00
//expect:3.4
print("finish");
2022-02-10 00:35:29 +01:00
//expect:finish
2022-12-02 21:13:36 +01:00
//convoluted :: test
2022-12-03 09:48:31 +01:00
(proc() print("merry christmas")) :: proc(it) it();
2022-12-02 21:13:36 +01:00
//expect:merry christmas
2022-12-03 09:02:43 +01:00
// proc declaration
2022-02-10 00:35:29 +01:00
2022-12-03 09:02:43 +01:00
proc globalProc(a, b, c)
2022-02-10 00:35:29 +01:00
a + b * c
;
{
2022-12-03 09:02:43 +01:00
proc localProc(d, e, f)
2022-02-10 00:35:29 +01:00
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
};