Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass4Lua.lua
More file actions
Latest commit
executable file
·132 lines (117 loc) · 3.73 KB
/
Copy pathClass4Lua.lua
File metadata and controls
executable file
·132 lines (117 loc) · 3.73 KB
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
#!/usr/bin/lua
localargs= {...}
localpath=args[1]
localregistry=setmetatable({ class= {}, instance= {} }, { __mode='k' })
localclasspath= {}
localinstantiate
localbaseClass= {
__index=function(self, key)
ifregistry.class[self].vars[key].privatethen
error(key..' has private access in '..registry.class[self].name..'.', 2)
elseifnotregistry.class[self].vars[key].staticthen
error('Non static '..(type(registry.class[self].vars[key].value) =='function' and'method' or'variable')..]' cannot be referenced from a static context.', 2)
end
returnregistry.class[self].vars[key].value
end,
__newindex=function(self, key, val)
ifregistry.class[self].vars[key].privatethen
error(key..' has private access in '..registry.class[self].name..'.', 2)
elseifregistry.class[self].vars[key].finalthen
error('Cannot assign a value to final variable'..key..'.', 2)
end
registry.class[self].vars[key].value=val
end,
__call=function(self, ...)
returninstantiate(self, ...)
end,
__tostring=function(self)
error('Non static method cannot be referenced from a static context.', 2)
end,
__metatable=false
}
localbaseInstance= {
__index=function(self, key)
ifregistry.instance[self].vars[key].privatethen
error(key..' has private access in '..registry.class[registry.instance[self].superClass].name..'.', 2)
elseifregistry.class[self].vars[key].staticthen
returnregistry.class[registry.instance[self].superClass].vars[key].value
end
returnregistry.class[self].vars[key].value
end,
__newindex=function(self, key, val)
ifregistry.class[self].vars[key].privatethen
error(key..' has private access in '..registry.class[registry.instance[self].superClass].name..'.', 2)
elseifregistry.class[self].vars[key].finalthen
error('Cannot assign a value to final variable'..key..'.', 2)
end
registry.class[self].vars[key].value=val
end,
__tostring=function(self)
ifregistry.class[self].meta.tostringthen
returnregistry.class[self].meta.tostring()
else
returnregistry.class[registry.instance[self].superClass].name..'@'..registry.class[self].system.addr
end
end,
__metatable=false
}
localfunctionassert(condition, msg, lvl)
ifnotconditionthenerror(msg, lvl+1) end
end
fork,vinipairs({'add', 'sub', 'mul', 'div', 'mod', 'pow', 'unm', 'concat', 'eq', 'lt', 'le'}) do
baseClass['__'..v] =function(self, ...)
registry.class[self].meta[v](...)
end
end
localfunctionclass(qualifiedpath, public, final, abstract, static)
localclass= {}
registry.class[class] = {
name=qualifiedpath:match('([^%.]+)$'),
package=qualifiedpath:match('(.-)[^%.]+$'):gsub('.$','')
qualifiedpath=qualifiedpath
system= {
type="Class",
public=publicorfalse,
final=finalorfalse,
abstract=abstractorfalse,
static=staticorfalse,
superClass=false,
subClasses= {},
interfaces= {},
addr=tostring(class)
},
vars= {},
meta= {}
}
localcurrpkg=classpath
forseginqualifiedpath:gmatch('[^%.]+') do
ifseg==registry.class[class].namethen
currpkg[seg] =class
break
else
currpkg[seg] = {}
currpkg=currpkg[seg]
end
end
returnsetmetatable(class,baseClass)
end
localfunctioninstantiate(class, ...)
assert(registry.class[self].__system.__abstract, 'Cannot instantiate from abstract class.', 3)
localinstance= {}
registry.instance[instance] = {
system= {
type="Instance",
superClass=class,
addr=tostring(instance)
},
vars= {
toString= {value=baseInstance.__tostring, public=true}
},
meta= {}
}
localinstance=setmetatable(instance, baseInstance)
ifregistry.class[class].constructorthen
registry.class[class].constructor(instance, ...)
end
returninstance
end