| 1 |
/* |
| 2 |
* String template tests |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <tads.h> |
| 6 |
|
| 7 |
string template <<* in words>> inWords; |
| 8 |
string template <<twice *>> twice; |
| 9 |
string template <<thrice *>> thrice; |
| 10 |
string template <<player location>> playerLoc; |
| 11 |
string template <<*>> embed; |
| 12 |
|
| 13 |
twice(x) |
| 14 |
{ |
| 15 |
return x * 2; |
| 16 |
} |
| 17 |
|
| 18 |
thrice(x) |
| 19 |
{ |
| 20 |
return x * 3; |
| 21 |
} |
| 22 |
|
| 23 |
playerLoc() |
| 24 |
{ |
| 25 |
return 'In the Kitchen'; |
| 26 |
} |
| 27 |
|
| 28 |
embed(x) |
| 29 |
{ |
| 30 |
if (dataType(x) == TypeObject && x.ofKind(Thing)) |
| 31 |
return x.name; |
| 32 |
|
| 33 |
return x; |
| 34 |
} |
| 35 |
|
| 36 |
class Thing: object |
| 37 |
name = 'thing' |
| 38 |
; |
| 39 |
|
| 40 |
book: Thing |
| 41 |
name = 'blue book' |
| 42 |
; |
| 43 |
|
| 44 |
inWords(x) |
| 45 |
{ |
| 46 |
local small = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', |
| 47 |
'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', |
| 48 |
'fourteen', 'fifteen', 'sixteen', 'seventeen', |
| 49 |
'eighteen', 'nineteen']; |
| 50 |
local tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', |
| 51 |
'seventy', 'eighty', 'ninety']; |
| 52 |
|
| 53 |
if (x < 0) |
| 54 |
return 'minus <<inWords(-x)>>'; |
| 55 |
if (x == 0) |
| 56 |
return 'zero'; |
| 57 |
|
| 58 |
local ret = '', q; |
| 59 |
if (x > 1000000000) |
| 60 |
{ |
| 61 |
q = x/1000000000; |
| 62 |
x %= 1000000000; |
| 63 |
ret += '<<q in words>> billion '; |
| 64 |
} |
| 65 |
if (x > 1000000) |
| 66 |
{ |
| 67 |
q = x/1000000; |
| 68 |
x %= 1000000; |
| 69 |
ret += '<<q in words>> million '; |
| 70 |
} |
| 71 |
if (x > 1000) |
| 72 |
{ |
| 73 |
q = x/1000; |
| 74 |
x %= 1000; |
| 75 |
ret += '<<q in words>> thousand '; |
| 76 |
} |
| 77 |
if (x > 100) |
| 78 |
{ |
| 79 |
q = x/100; |
| 80 |
x %= 100; |
| 81 |
ret += '<<small[q]>> hundred '; |
| 82 |
} |
| 83 |
if (x > 20) |
| 84 |
{ |
| 85 |
q = x/10; |
| 86 |
x %= 10; |
| 87 |
ret += tens[q-1]; |
| 88 |
if (x != 0) |
| 89 |
ret += '-<<small[x]>>'; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
ret += small[x]; |
| 94 |
} |
| 95 |
|
| 96 |
if (ret.endsWith(' ')) |
| 97 |
ret = ret.substr(1, -1); |
| 98 |
|
| 99 |
return ret; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
main(args) |
| 104 |
{ |
| 105 |
local a = 123; |
| 106 |
"Let's try some tests: twice a is <<twice a>>, twice a in words |
| 107 |
is <<twice a in words>>, thrice f(a) is <<thrice f(a)>>, thrice f(a) in |
| 108 |
words is <<thrice f(a) in words>>! By the way, the player's location |
| 109 |
is <<player location>>. And finally, book=<<book>>.\n"; |
| 110 |
} |
| 111 |
|
| 112 |
f(x) |
| 113 |
{ |
| 114 |
return x*1234567; |
| 115 |
} |
| 116 |
|