-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathluamop.lua
195 lines (169 loc) · 3.79 KB
/
luamop.lua
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
function __blessClass(class)
local meta = getmetatable(class)
meta.__newindex = class.methods
end
function __createInstance(class)
local object = {}
__blessInstance(class, object)
return object
end
function __inheritSearch(self, name)
local class = getClass(self)
local impl = class:search(name)
self[name] = impl
return impl
end
function __blessInstance(class, object)
--print("BI: class is ", class)
local meta = { __index = __inheritSearch, __newindex = rawset, class = class }
setmetatable(object, meta)
end
function getClass(object)
local meta = getmetatable(object)
return meta.class
end
ObjectClass = {
name = "ObjectClass",
methods = {
}
}
ObjectClass.super = ObjectClass
__classSearch = function(class, name)
local impl = class.methods[name]
if impl then
return impl
end
if class.super == class then
return nil
else
return class.super:search(name)
end
end
ClassClass = {
name = "ClassClass",
methods = {
search = __classSearch
},
search = __classSearch,
super = ObjectClass
}
__blessInstance(ClassClass, ObjectClass)
__blessInstance(ClassClass, ClassClass)
__blessClass(ObjectClass)
__blessClass(ClassClass)
function ObjectClass.BUILDALL(self, ...)
local recurse = nil
recurse = function(class, ...)
if not(class.super == class) then
recurse(class.super, ...)
end
if class.methods.BUILD then
class.methods.BUILD(...)
end
end
recurse(getClass(self), self, ...)
end
function ClassClass.newinstance(class, ...)
--print("Instantiating ", class)
local inst = {}
__blessInstance(class, inst)
inst:BUILDALL(...)
return inst
end
function ClassClass.BUILD(self, args)
if not(args) then
args = {}
end
local super = args.super
if not(super) then
super = ObjectClass
end
if args.name then
self.name = name
else
self.name = "Unnamed"
end
self.super = super
self.methods = {}
__blessClass(self)
end
function ClassClass.around(self, name, wrapper)
local base = self:search(name)
local function impl(...)
return wrapper(base, ...)
end
self.methods[name] = impl
end
function ClassClass.before(self, name, wrapper)
self:around(name, function(base, ...)
wrapper(...)
return base(...)
end)
end
function ClassClass.after(self, name, wrapper)
self:around(name, function(base, ...)
local ret = base(...)
wrapper(ret, ...)
return ret
end)
end
--print("Creating a new class using ClassClass=", ClassClass)
Animal = ClassClass:newinstance()
function Animal.getSpecies(self)
return "animal"
end
function Animal.BUILD(self, args)
if args and args.name then
self.name = args.name
else
self.name = false
end
end
function Animal.barkstr(self, args)
local str = "The " .. self:getSpecies()
if self.name then
str = str .. " named " .. self.name
end
str = str .. " barks"
if args and args.target then
str = str .. " at " .. args.target
end
str = str .. "!"
return str
end
function Animal.bark(self, ...)
print(self:barkstr(...))
end
rawset(Animal, "newspecies", function(self, speciesname)
local class = getClass(self):newinstance{super = self}
function class.getSpecies(self)
return speciesname
end
return class
end)
Wolf = Animal:newspecies("wolf")
Wolf:around("barkstr", function(base, self, ...)
local retval = base(self, ...)
retval = string.gsub(retval, "barks", "howls")
return retval
end)
lupin = Wolf:newinstance{name = "Lupin"}
lupin:bark{target = "the moon"}
Dog = Animal:newspecies("dog")
Dog:after("bark", function(self, ...)
print("It then slobbers on your leg.")
end)
fido = Dog:newinstance{name = "Fido"}
fido:bark()
Dog:newinstance():bark()
Bear = Animal:newspecies("bear")
yogi = Bear:newinstance{name = "Yogi"}
yogi:bark()
fido:bark()
Grue = Animal:newspecies("grue")
Grue:before("bark", function(self, ...)
print("It is dark.");
end)
grue = Grue:newinstance()
grue:bark()
-- Animal:bark() -- should fail as bark is an instance method