nomsu/lib/object.nom

62 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-07-17 23:37:20 -07:00
#!/usr/bin/env nomsu -V2.2.4.3
2018-06-15 00:17:01 -07:00
#
This file contains the implementation of an Object-Oriented programming system.
2018-06-15 00:17:01 -07:00
use "core"
2018-07-17 23:37:20 -07:00
compile [@, me] to (Lua value "self")
compile [method %actions %body] to:
%lua <- (compile as (action %actions %body))
add free vars ((% as lua id) for % in %actions) to %lua
declare locals in %lua
2018-07-17 23:37:20 -07:00
for % in %actions:
to %lua write "\nclass.\(% as lua id) = \(% as lua id)"
2018-07-17 23:37:20 -07:00
return (..)
Lua ".."
2018-07-17 23:37:20 -07:00
do -- Method: \(%actions.(1).stub)
\%lua
end
2018-06-18 15:44:29 -07:00
2018-07-17 23:37:20 -07:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parse [as %instance %body] as (..)
result of:
2018-06-18 15:44:29 -07:00
%old_self <- (me)
(me) <- %instance
2018-07-17 23:37:20 -07:00
try %body and if it barfs %msg:
2018-06-18 15:44:29 -07:00
(me) <- %old_self
barf %msg
2018-07-17 23:37:20 -07:00
..or if it succeeds: (me) <- %old_self
2018-06-18 15:44:29 -07:00
2018-07-17 23:37:20 -07:00
compile [object %classname extends %parent %class_body] to (..)
2018-06-18 15:44:29 -07:00
Lua ".."
do
local class = {name=\(%classname as lua expr)}
setmetatable(class, {
__index=\(%parent as lua expr),
__tostring=function(cls) return cls.name end,
__call=function(cls, inst)
inst = setmetatable(inst or {}, cls)
if cls.A_initialize_1 then
cls.A_initialize_1(inst)
end
return inst
end,
})
_ENV["A"..string.as_lua_id("new "..class.name)] = class
_ENV["A"..string.as_lua_id("new "..class.name.." 1")] = class
_ENV["A"..string.as_lua_id("class "..class.name)] = function() return class end
class.__index = class
class.class = class
2018-07-17 23:37:20 -07:00
2018-06-18 15:44:29 -07:00
\(%class_body as lua statements)
2018-07-17 23:37:20 -07:00
2018-06-18 15:44:29 -07:00
class.__tostring = class["A"..string.as_lua_id("as text")]
end
2018-07-17 23:37:20 -07:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
parse [object %classname %class_body] as (..)
object %classname extends (nil) %class_body