-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cp.yue
303 lines (213 loc) · 6.44 KB
/
test_cp.yue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
require = require
tostring = tostring
stdout = io.stdout
print = (...) ->
args = { ... }
outs = ""
first = true
for arg in *args
repr = tostring
if type(arg) == "table"
if mt = arg.<>
if r = mt.__repr
repr = r
if first
first = false
else
outs ..= ", "
outs ..= repr(arg)
stdout::write(outs, "\n")
_print_header_count = 0
print_h1 = (text) ->
bar_top = "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
bar_bottom = "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
bar_length = 65
padding = ""
padding_length = (math.floor(bar_length / 2) - 1 - math.floor(#text / 2))
for i = 1, padding_length
padding ..= " "
print("#{(_print_header_count >= 1) and "\n" or ""}\027[1;34m#{bar_top}\n┃#{padding}#{text}#{((#text % 2) == 0) and " " or ""}#{padding}┃\n#{bar_bottom}\027[0m")
_print_header_count += 1
print_h2 = (text) ->
print("#{(_print_header_count >= 1) and "\n" or ""}\027[1;33m ❱❱❱ #{text} ❰❰❰\027[0m")
_print_header_count += 1
print_h3 = (text) ->
print("#{(_print_header_count >= 1) and "\n" or ""}\027[1;32m❱ #{text}\027[0m")
_print_header_count += 1
print_h4 = (text) ->
print("#{(_print_header_count >= 1) and "\n" or ""}\027[1;31m #{text}\027[0m")
_print_header_count += 1
print_h1("Testing slimeos.lib.ClassPlus")
import "slimeos.lib.ClassPlus"
local MyObject
MyObject = ClassPlus
name: "MyObject"
print(">>> Module: #{ClassPlus}")
print(">>> Class: #{MyObject}")
print(">>> Instance: #{MyObject()}")
print_h1("Basic inheritance")
local Foo
Foo = ClassPlus
name: "Foo"
new: (cls, ...) ->
print("Created a new '#{cls.__name}' instance")
ClassPlus.super(Foo).__new(cls, ...)
init: (...) =>
print("Initialized the previously created '#{@__class.__name}' instance")
ClassPlus.super(Foo).__init(@, ...)
body:
foo: "bar"
x = Foo()
print(">>> Before definiton of 'Bar':")
print("x.foo = \"#{x.foo}\"")
local Bar
Bar = ClassPlus
name: "Bar"
parents: { Foo }
new: (cls, ...) ->
print("<<<Hello from '#{cls.__name}'s constructor!>>>")
ClassPlus.super(Bar).__new(cls, ...)
body:
foo: "Something else"
print(">>> After definiton of 'Bar':")
print("x.foo = \"#{x.foo}\"")
y = Bar()
print(">>> After creation of 'y':")
print("x.foo = \"#{x.foo}\"")
print("y.foo = \"#{y.foo}\"")
print("x = #{x}")
print("y = #{y}")
print()
local Biz
Biz = ClassPlus
name: "Biz"
parents: { Bar }
body:
foo: "Something else... again!"
z = Biz()
print(">>> After creation of 'z':")
print("x.foo = \"#{x.foo}\"")
print("y.foo = \"#{y.foo}\"")
print("z.foo = \"#{z.foo}\"")
print("x = #{x}")
print("y = #{y}")
print("z = #{z}")
print_h1("Diamond problem")
local Top
Top = ClassPlus
name: "Top"
body:
some_field: "Value from 'Top'"
local Left
Left = ClassPlus
name: "Left"
parents: { Top }
body:
some_field: "Value from 'Left'"
local Right
Right = ClassPlus
name: "Right"
parents: { Top }
body:
some_field: "Value from 'Right'"
local Bottom
Bottom = ClassPlus
name: "Bottom"
parents: { Left, Right }
--body:
-- some_field: "Value from 'Bottom'"
top, left, right, bottom = Top(), Left(), Right(), Bottom()
print(" Top = #{Top} -> top = #{top}") --- Top = <class 'Top' at 0x7ff2972eb370> -> top = <instance of 'Top' at 0x7ff2972dec40>
print(" Left = #{Left} -> left = #{left}") --- Left = <class 'Left' at 0x7ff2972e9ca8> -> left = <instance of 'Left' at 0x7ff2972f3108>
print(" Right = #{Right} -> right = #{right}") --- Right = <class 'Right' at 0x7ff2972f4008> -> right = <instance of 'Right' at 0x7ff2972f0030>
print("Bottom = #{Bottom} -> bottom = #{bottom}") --- Bottom = <class 'Bottom' at 0x7ff2972ece60> -> bottom = <instance of 'Bottom' at 0x7ff2972efc50>
print(" top.some_field = #{top.some_field}") --- top.some_field = Value from 'Top'
print(" left.some_field = #{left.some_field}") --- left.some_field = Value from 'Left'
print(" right.some_field = #{right.some_field}") --- right.some_field = Value from 'Right'
print("bottom.some_field = #{bottom.some_field}") --- bottom.some_field = Value from 'Left'
print_h1("Mixins")
iterable = () ->
ret = {
get_pairs: () =>
local k
() ->
k, v = next(@, k)
k, v
get_keys: () =>
local k
() ->
k, v = next(@, k)
k
get_values: () =>
local k
() ->
k, v = next(@, k)
v
}
do
local k
ret.__call = () =>
k, v = next(@, k)
k, v
ret
local List
List = ClassPlus
name: "List"
mixins: { iterable() }
init: (items={}) =>
for k, v in pairs(items)
@[k] = v
test_list = List({"foo", "bar", "biz", "baz"})
print_h2("Pairs (implicit)")
for k, v in test_list
print("test_list[#{k}] = '#{v}'")
print_h2("Pairs (explicit)")
for k, v in test_list::get_pairs()
print("test_list[#{k}] = '#{v}'")
print_h2("Keys")
for k in test_list::get_keys()
print("test_list[#{k}] = '#{test_list[k]}'")
print_h2("Values")
for v in test_list::get_values()
print("'#{v}'")
--- test_list[k] = 'foo'
--- test_list[k] = 'bar'
--- test_list[k] = 'biz'
--- test_list[k] = 'baz'
print_h1("Inhertiance (but practical)")
local String
String = ClassPlus
name: "String"
init: (value) =>
with t = type(value)
assert(t == "string", "Invalid argument #1 to #{@@__name} (expected a string, got a #{t})")
@value = value
ClassPlus.super(String).__init
body:
__get: (key) => rawget(@, "value")::sub(key, key)
__repr: () => '"' .. @value::gsub([["]], [[\"]]) .. '"'
__tostring: () => @value
local UppercasedString
UppercasedString = ClassPlus
name: "UppercasedString"
parents: { String }
init: (value="") =>
value = value::upper()
ClassPlus.super(UppercasedString).__init(@, value)
print(String("FooBar")) --- 'FooBar'
print(UppercasedString("FooBar")) --- 'FOOBAR'
print_h1("Static class fields")
do
prettify = require("slimeos.lib.util.tts").prettify
pretty_print = (obj, raw=true) -> stdout::write(prettify(obj, raw), "\n")
SomeClass = ClassPlus
name: "SomeClass"
print("SomeClass =")
for k, v in pairs(SomeClass)
print(" * #{k}:\t'#{v}'")
print()
print("SomeClass.__body =")
for k, v in pairs(SomeClass.__body)
print(" * #{k}:\t'#{v}'")
print_h2("With `pretty_print()`:")
pretty_print(SomeClass)