-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.hws
362 lines (320 loc) · 10.5 KB
/
class.hws
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
** OOP Class implementation
**
** By Samuel D. Crow
**
** See LICENSE file for licensing details
**
*/
;TODO add checking
;TODO add exception handling support
;TODO add static function support
Global _class={} ;namespace
;indicate that this file has been included
Const #CLASS = True
;_parent is deliberately left undefined for root class
_class["_interface"]={} ;global interface table
_class["_error"]={} ;locale specific error functions
_class["_static"]={} ;static members
_class["_name"]="Root Class"
_class["_func"]={} ;static functions
_class["_v"]={} ;vector table
_class["_data"]={} ;fields
_class["_implement"]={} ;interfaces implemented
;*****************************************************************************
;error messages
;*****************************************************************************
Switch(GetSystemLanguage())
Case #LANGUAGE_ENGLISH:
FallThrough
Default:
_class._error["MethodExists"]=Function(name)
Error(name .. " method already exists\nin class " .. self._name)
EndFunction
_class._error["MethodTable"]=Function()
Error("Method parameter defaults must be passed as a table")
EndFunction
_class._error["MethodNotFound"]=Function()
Error("Method not found")
EndFunction
_class._error["NoParent"]=Function()
Error("No parent class for method call")
EndFunction
_class._error["ImpliedTable"]=Function()
Error("Implied interfaces must be passed in a table")
EndFunction
_class._error["InterfaceName"]=Function()
Error("Illegal type name on interface")
EndFunction
_class._error["ProtectedInterface"]=Function()
Error("Protected interfaces not allowed")
EndFunction
_class._error["InterfaceDoesNotExist"]=Function(i, name)
Error("Non-existant interface " .. i .. "\nis implied by interface " .. name)
EndFunction
_class._error["Underscore"]=Function(name)
Error("Underscore indicates illegal protected member\nin " .. name)
EndFunction
_class._error["DuplicateDefinition"]=Function(i, name)
Error("Duplicate definition of method " .. i .. "\nin interface " .. name)
EndFunction
_class._error["InterfaceNoMethod"]=Function(i, name)
Error(i .. " method not implemented\nfrom implementation of interface\n" .. name)
EndFunction
_class._error["InterfaceUndefined"]=Function()
Error("Interface not defined")
EndFunction
_class._error["MissingMethod"]=Function(i)
Error("Does not implement all methods of interface " .. i)
EndFunction
_class._error["ProtectedGetter"]=Function()
Error("Protected field requested by public getter")
EndFunction
_class._error["ProtectedSetter"]=Function()
Error("Public setter attempted to write to a protected field")
EndFunction
_class._error["UnknownClass"]=Function()
Error("Unknown class encountered")
EndFunction
_class._error["UnknownType"]=Function()
Error("Unknown type encountered")
EndFunction
_class._error["TypeOfArgs"]=Function()
Error("Too many arguments in TypeOf function")
EndFunction
EndSwitch
;*****************************************************************************
;helper functions
;*****************************************************************************
;clear parameter number from vararg table to avoid namespace issues
;Function p_RawArg(a)
; RawSet(a, "n", Nil)
; Return(a)
;EndFunction
;*****************************************************************************
;methods
;*****************************************************************************
; method declaration
; params is table containing default values of each parameter key
; body is an actual function definition with self passed as first argument
Function _class:Method(name, params, body)
If RawGet(self, name) Then _class._error.MethodExists(name)
If GetType(params) <> #TABLE Then _class._error.MethodTable()
Local buf={}
buf["_body"]=body
buf["_params"]=params
self._v[name]=buf
self[name]=self._v[name]
EndFunction
; return method and containing class if they are available
Function _class:GetMethod(name)
Local c=self, m
Repeat
m=RawGet(c._v, name)
If m Then Return(m, c)
c=RawGet(c, "_parent") ; check for inheritance
Until IsNil(c)
Return(Nil, Nil)
EndFunction
Function _class:_MethodWorker(name, fn, params)
Local m, c=self.GetMethod(name)
If IsNil(c) Then _class._error.MethodNotFound()
;propogate default entries
For i In m._params
If IsNil(RawGet(params, i)) Then params[i]=m._pararms.i
Next
Return(fn(self, params))
EndFunction
;execute a method
Function _class:DoMethod(name, ...)
Return(self:_MethodWorker(name, self._v.m._body, p_RawArg(arg)))
EndFunction
;inherit from parent method
Function _class:DoParentMethod(name, ...)
Local _p=RawGet(self, "_parent")
If IsNil(_p) Then _class._error.NoMethod()
Return(self:_MethodWorker(_p._name, _p._v.m._body, p_RawArg(arg)))
EndFunction
;*****************************************************************************
;interfaces
;*****************************************************************************
;declare in the global interface table
;implied is a table containing the additional interfaces implied by this one
Function _class:Interface(name, implied, ...)
If GetType(implied)<>#TABLE Then _class._error.ImpliedTable()
If GetType(name)<>#STRING Then _class._error.InterfaceName()
If LeftStr(name, 1) = "_" Then _class._error.ProtectedInterface()
Local iface={}, buf={}, s, s2, i, j
; check for illegal implied interfaces
For s, i In Pairs(implied)
Local ibuf=RawGet(_class._interface, i) ; implementation buffer
If IsNil(ibuf) Then _class._error.InterfaceDoesNotExist(i, name)
; add methods to buf so duplicates can be detected later
For j, s2 In Pairs(ibuf._body)
RawSet(buf, j, True)
Next
Next
For s, i In IPairs(arg)
If LeftStr(i,1)="_" Then _class._error.Underscore("interface " .. name)
If RawGet(buf, i) Then _class._error.DuplicateDefinition(i, name)
RawSet(buf, i, True)
Next
iface["_body"]=buf
iface["_imply"]=implied
_class._interface[name]=iface
EndFunction
; interface implementation access
Function _class:GetImplementation(name)
Local iface = RawGet(_class._interface, name), buf={}, iter, problem
If IsNil(iface) Then _class._error.UndefinedInterface()
buf["_implementation"]=name
buf["_target"]=self
Local iter
For i In iface._body
iter=self.GetMethod(i)
If IsNil(iter) Then _class._error.InterfaceNoMethod(i, name)
;wrap method to fix self pointer
buf[i]=Function(...)
local a=arg
a.n=Nil
a[0]=buf._target
Return(iter(Unpack(a)))
EndFunction
Next
Return(buf)
EndFunction
;check if current class implements an interface
;returns boolean result
Function _class:DoesImplement(interface)
local iface = _class._interface[interface], buf={}, problem
For i In iface._imply
buf, problem=_class._IFaceWorkLoop(name, buf, i)
If problem Then Return(False)
Next
buf, problem=_class._IFaceWorkLoop(name, buf, iface._body)
Return(Not(problem))
EndFunction
;*****************************************************************************
;allocation of new classes
;*****************************************************************************
;inheritance allocation worker method
;iface is a set of names of interfaces implemented
Function _class:_inherit(name, parent, iface)
Local c={}
c["_v"]=CopyTable(parent._v)
c["_data"]=CopyTable(parent._data)
c["_implement"]=CopyTable(parent._implement)
c["_func"]=CopyTable(parent._func)
c["_name"]=name
c["_parent"]=parent
;implement additional interfaces
For i In iface
If IsNil(RawGet(_class._interface, i))
_class._error.InterfaceUndefined()
EndIf
If Not(c.DoesImplement(i)) Then _class._error.MissingMethod(i)
_class._interface[i]=i._body
Next
Return(c)
EndFunction
;inherit from parent and implement additional interfaces
Function _class:Extend(name, ...)
Return(self._inherit(name, self._parent, p_RawArg(arg)))
EndFunction
;new class inherits only from root class
Function New(name, ...)
Return(_class._inherit(name, _class, p_RawArg(arg)))
EndFunction
;*****************************************************************************
;data members
;*****************************************************************************
; member declaration
; value is a default and must be associated with a type
Function _class:Member(name, value)
self._data[name]=value
EndFunction
; static member declaration
Function StaticMember(name, value)
_class._static[name]=value
EndFunction
;*****************************************************************************
;accessor methods
;*****************************************************************************
;default getter method
_class._v["Get"]=Function(self, name)
If (LeftStr(name, 1)="_") Then _class._error.ProtectedGetter()
Local value=RawGet(self._data, name), p=self._parent
While (IsNil(value) And p)
value=RawGet(p, name)
p=RawGet(p, "_parent")
Wend
Return(value)
EndFunction
;default setter method
_class._v["Set"]=Function(self, name, value)
If (LeftStr(name,1)="_") Then _class._error.ProtectedSetter()
self._data[name]=value
EndFunction
;*****************************************************************************
;type checking
;*****************************************************************************
Const #TYPE_NIL=0
Const #TYPE_NUMBER=1
Const #TYPE_STRING=2
Const #TYPE_FUNCTION=3
Const #TYPE_TABLE=4
Const #TYPE_CLASS=5
Const #TYPE_INTERFACE=6
Const #TYPE_UNDEFINED=7
Const #TYPE_METHOD=8
Function _class:TypeOf(...)
Switch (arg.n)
Case 0:
; type of self as class or interface
Local n=RawGet(self, "_name")
If n
Return(#TYPE_CLASS)
Else
n=RawGet(self, "_implementation")
If n Then Return(#TYPE_INTERFACE)
_class._error.UnknownClass()
EndIf
Case 1:
; type of primitive
Local a=arg[0], n=self.Get(a)
If n
Switch (GetType(n))
Case #NUMBER:
Return(#TYPE_NUMBER)
Case #FUNCTION:
Return(#TYPE_FUNCTION)
Case #STRING:
Return(#TYPE_STRING)
Case #TABLE:
Return(#TYPE_TABLE)
Default:
_class._error.UnknownType()
EndSwitch
Else
Local f, c=self.GetMethod(a) ; NOTE: two return codes
If f Then Return(#TYPE_METHOD)
Return(#TYPE_UNDEFINED)
EndIf
Default:
_class._error.TypeOfArgs()
EndSwitch
EndFunction
; subclass type check
Function _class:IsTypeOf(query)
Local i=self
While Not IsNil(i)
If i=query Then Return(True)
i=RawGet(i, "_parent")
Wend
Return(False)
EndFunction
;*****************************************************************************
;standard interface definitions
;*****************************************************************************
;note iterator contents are stored under the "value" key by convention
_class:Interface("Iteratable", {}, "Start", "GetNext")