Added a bunch of metamethod stuff.
This commit is contained in:
parent
1334030285
commit
d02b4b8718
@ -48,10 +48,37 @@ compile [define object %classname %class_body] to:
|
|||||||
if: %class_identifier is ""
|
if: %class_identifier is ""
|
||||||
%class_identifier <- "class"
|
%class_identifier <- "class"
|
||||||
%methods <- []
|
%methods <- []
|
||||||
|
%__index <- %class_identifier
|
||||||
|
%__newindex <- "nil"
|
||||||
for %line in (%class_body's "value"):
|
for %line in (%class_body's "value"):
|
||||||
if: (%line's "type") is "Comment"
|
if: (%line's "type") is "Comment"
|
||||||
do next %line
|
do next %line
|
||||||
assume (((%line's "type") == "FunctionCall") and ((%line's "stub") == "action % %"))
|
if: ((%line's "type") is "FunctionCall") and ((%line's "stub") is "slots %")
|
||||||
|
%slot_index_clauses <- []
|
||||||
|
%slot_newindex_clauses <- []
|
||||||
|
%slots <- ("value" in (2nd in (%line's "value")))
|
||||||
|
for %slot_index = %slot_var in %slots:
|
||||||
|
to %slot_index_clauses add ".."
|
||||||
|
if key == \(repr (%slot_var's "value")) or key == \(repr (%slot_var as lua expr)) then
|
||||||
|
return rawget(self, \%slot_index);
|
||||||
|
end
|
||||||
|
to %slot_newindex_clauses add ".."
|
||||||
|
if key == \(repr (%slot_var's "value")) or key == \(repr (%slot_var as lua expr)) or key == \%slot_index then
|
||||||
|
rawset(self, \%slot_index, value);
|
||||||
|
end
|
||||||
|
|
||||||
|
%__index <- ".."
|
||||||
|
function(self, key)
|
||||||
|
\(%slot_index_clauses joined with "\n")
|
||||||
|
return \%class_identifier[key];
|
||||||
|
end
|
||||||
|
%__newindex <- ".."
|
||||||
|
function(self, key, value)
|
||||||
|
\(%slot_newindex_clauses joined with "\n")
|
||||||
|
error("Attempt to store data in "..repr(key)..", which is not a valid slot on "..tostring(self.class));
|
||||||
|
end
|
||||||
|
do next %line
|
||||||
|
assume (((%line's "type") is "FunctionCall") and ((%line's "stub") is "action % %"))
|
||||||
..or barf "Only action definitions are supported inside 'define object % %', not \(%line's "src")"
|
..or barf "Only action definitions are supported inside 'define object % %', not \(%line's "src")"
|
||||||
%actions <- (2nd in (%line's "value"))
|
%actions <- (2nd in (%line's "value"))
|
||||||
%body <- (3rd in (%line's "value"))
|
%body <- (3rd in (%line's "value"))
|
||||||
@ -98,10 +125,9 @@ compile [define object %classname %class_body] to:
|
|||||||
name=\(%classname as lua expr), instances=setmetatable({}, {__mode="k"}),
|
name=\(%classname as lua expr), instances=setmetatable({}, {__mode="k"}),
|
||||||
}, {
|
}, {
|
||||||
__tostring=function(c) return c.name; end,
|
__tostring=function(c) return c.name; end,
|
||||||
__call=function(cls, inst)
|
__call=function(cls, initial_values)
|
||||||
inst = inst or {};
|
local inst = setmetatable({}, cls.instance_metatable);
|
||||||
inst.id = tostring(inst):match('table: (.*)');
|
for k,v in pairs(initial_values) do inst[k] = v; end
|
||||||
setmetatable(inst, cls.instance_metatable);
|
|
||||||
cls.instances[inst] = true;
|
cls.instances[inst] = true;
|
||||||
if inst['set % up'] then
|
if inst['set % up'] then
|
||||||
inst['set % up'](inst);
|
inst['set % up'](inst);
|
||||||
@ -116,10 +142,27 @@ compile [define object %classname %class_body] to:
|
|||||||
|
|
||||||
-- Define class methods for instantiating and accessing instances:
|
-- Define class methods for instantiating and accessing instances:
|
||||||
\%class_identifier.instance_metatable = {
|
\%class_identifier.instance_metatable = {
|
||||||
__index=\%class_identifier,
|
__index=\%__index,
|
||||||
__tostring=\%class_identifier['% as text'] or function(inst)
|
__newindex=\%__newindex,
|
||||||
return "<"..inst.class.name..": "..inst.id..">";
|
__tostring=\%class_identifier['as text'] or function(inst)
|
||||||
|
return "<"..inst.class.name..": "..nomsu.ids[inst]..">";
|
||||||
end,
|
end,
|
||||||
|
__len=\%class_identifier['size of'],
|
||||||
|
__unm=\%class_identifier['-'],
|
||||||
|
__add=\%class_identifier['+ %'],
|
||||||
|
__sub=\%class_identifier['- %'],
|
||||||
|
__mul=\%class_identifier['* %'],
|
||||||
|
__div=\%class_identifier['/ %'],
|
||||||
|
__mod=\%class_identifier['wrapped around %'],
|
||||||
|
__pow=\%class_identifier['^ %'],
|
||||||
|
__band=\%class_identifier['AND %'],
|
||||||
|
__bor=\%class_identifier['OR %'],
|
||||||
|
__bxor=\%class_identifier['XOR %'],
|
||||||
|
__bshl=\%class_identifier['<< %'],
|
||||||
|
__bshr=\%class_identifier['>> %'],
|
||||||
|
__eq=\%class_identifier['= %'],
|
||||||
|
__lt=\%class_identifier['< %'],
|
||||||
|
__le=\%class_identifier['<= %'],
|
||||||
};
|
};
|
||||||
nomsu:define_action("instances of "..\%class_identifier.name, "lib/class.nom", function()
|
nomsu:define_action("instances of "..\%class_identifier.name, "lib/class.nom", function()
|
||||||
return utils.keys(\%class_identifier.instances);
|
return utils.keys(\%class_identifier.instances);
|
||||||
|
Loading…
Reference in New Issue
Block a user