| 1 |
#include <tads.h> |
| 2 |
#include <strbuf.h> |
| 3 |
#include <reflect.h> |
| 4 |
|
| 5 |
modify List |
| 6 |
myjoin() |
| 7 |
{ |
| 8 |
local s = new StringBuffer(); |
| 9 |
for (local i = 1, local len = length() ; i <= len ; ++i) |
| 10 |
{ |
| 11 |
if (i > 1) |
| 12 |
s.append(', '); |
| 13 |
s.append(self[i]); |
| 14 |
} |
| 15 |
return toString(s); |
| 16 |
} |
| 17 |
; |
| 18 |
|
| 19 |
g(b) |
| 20 |
{ |
| 21 |
f(b); |
| 22 |
} |
| 23 |
|
| 24 |
f(a:, b) |
| 25 |
{ |
| 26 |
"This is func! a=[<<a>>], b=[<<b>>] "; |
| 27 |
"(all named arguments: <<t3GetNamedArgList().mapAll( |
| 28 |
{ a: a + '=' + t3GetNamedArg(a, nil) }).myjoin()>>)\n"; |
| 29 |
} |
| 30 |
|
| 31 |
o: object |
| 32 |
m(x) { g(x); } |
| 33 |
; |
| 34 |
|
| 35 |
o2: o |
| 36 |
m(a:, x) { inherited(a: 'inherited ' + a, x); } |
| 37 |
; |
| 38 |
|
| 39 |
h(a:) |
| 40 |
{ |
| 41 |
"This is h! a=<<a>>\n"; |
| 42 |
return new function(a:) |
| 43 |
{ |
| 44 |
"This is anonymous! a=<<a>> "; |
| 45 |
"(all named arguments: <<t3GetNamedArgList().mapAll( |
| 46 |
{ a: a + '=' + t3GetNamedArg(a, nil) }).myjoin()>>)\n"; |
| 47 |
|
| 48 |
local t = t3GetStackTrace(nil, T3GetStackLocals); |
| 49 |
"Stack trace:\n"; |
| 50 |
foreach (local f in t) |
| 51 |
{ |
| 52 |
f.func_; |
| 53 |
local fn = [j->'j', k->'k', h->'h', main->'main', _main->'_main', |
| 54 |
*->'{anonymous}'][f.func_]; |
| 55 |
if (f.obj_ != nil && f.obj_.ofKind(AnonFuncPtr)) |
| 56 |
fn = '{anonymous}'; |
| 57 |
"<<fn>>("; |
| 58 |
if (f.namedArgs_ != nil) |
| 59 |
"<<f.namedArgs_.keysToList().mapAll( |
| 60 |
{key: key + ':' + f.namedArgs_[key]}).myjoin()>>"; |
| 61 |
")\n"; |
| 62 |
} |
| 63 |
}; |
| 64 |
// return ({ a: : "This is anonymous! a=<<a>>\n" }); |
| 65 |
} |
| 66 |
|
| 67 |
j(func) |
| 68 |
{ |
| 69 |
func(); |
| 70 |
} |
| 71 |
|
| 72 |
k(func) |
| 73 |
{ |
| 74 |
j(func, c: 'C from k()'); |
| 75 |
} |
| 76 |
|
| 77 |
def1(a: = 10, b:?) |
| 78 |
{ |
| 79 |
"This is def1: a=<<a>>, b=<<b>>\n"; |
| 80 |
} |
| 81 |
|
| 82 |
def2(a := 10, b := a*10, c := b*100) |
| 83 |
{ |
| 84 |
"This is def2: a=<<a>>, b=<<b>>, c=<<c>>\n"; |
| 85 |
} |
| 86 |
|
| 87 |
class Thing: object |
| 88 |
; |
| 89 |
class Container: Thing |
| 90 |
; |
| 91 |
|
| 92 |
mm(Thing t, a:, Thing o) |
| 93 |
{ |
| 94 |
"This is mm(Thing): a=<<a>>\n"; |
| 95 |
} |
| 96 |
|
| 97 |
mm(Container c, a:, Thing o) |
| 98 |
{ |
| 99 |
"This is mm(Container): a=<<a>>\n"; |
| 100 |
} |
| 101 |
|
| 102 |
main(args) |
| 103 |
{ |
| 104 |
try { f('B 1'); } catch (Exception e) { "<<e.displayException>>\n"; } |
| 105 |
f(a: 'A 2', 'B 2'); |
| 106 |
try { g('B 3'); } catch (Exception e) { "<<e.displayException>>\n"; } |
| 107 |
g(a: 'A 4', 'B 4', c: 'C 4'); |
| 108 |
|
| 109 |
local l = ['B 5']; |
| 110 |
g(a: 'A 5', l...); |
| 111 |
|
| 112 |
o.m(a: 'A 6', 'B 6'); |
| 113 |
o2.m(a: 'A 7', 'B 7'); |
| 114 |
|
| 115 |
local x = h(a: 'A 8'); |
| 116 |
j(x, a: 'A 9', b: 'B 9', c:'C 9'); |
| 117 |
k(x, a: 'A 10', b: 'B 10', c: 'C 10'); |
| 118 |
|
| 119 |
local t = new Thing(); |
| 120 |
mm(t, t, a:'thing'); |
| 121 |
|
| 122 |
t = new Container(); |
| 123 |
mm(t, t, a:'container'); |
| 124 |
|
| 125 |
[1, 2, 3].forEach(a: 'A 11', |
| 126 |
{ x, a: : "List.forEach: x=<<x>>, a=<<a>>\n" }); |
| 127 |
|
| 128 |
def1(); |
| 129 |
def1(a: 5); |
| 130 |
def1(b: 7); |
| 131 |
def1(a: 9, b:11); |
| 132 |
def2(); |
| 133 |
def2(a: 9); |
| 134 |
def2(b: 99); |
| 135 |
def2(c: 999); |
| 136 |
def2(a: 8, b: 9); |
| 137 |
def2(a: 11, b: 12, c: 13); |
| 138 |
def2(a: 14, c: 15); |
| 139 |
def2(b: 16, c: 17); |
| 140 |
} |