aboutsummaryrefslogtreecommitdiff
path: root/lib/object.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-06-15 00:17:01 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-06-15 00:17:09 -0700
commit1a6e28e8358e8ed9a6de4d4112da6b3506e6f6a3 (patch)
treee5539b2b20b25d08e6e884e53fbca6d3f0557c44 /lib/object.nom
parentb12744d831c4158671fc22401590eaac00f7c141 (diff)
Improvements to object system.
Diffstat (limited to 'lib/object.nom')
-rw-r--r--lib/object.nom82
1 files changed, 46 insertions, 36 deletions
diff --git a/lib/object.nom b/lib/object.nom
index 27ef6c9..37f661c 100644
--- a/lib/object.nom
+++ b/lib/object.nom
@@ -1,49 +1,59 @@
-use "core"
+#
+ This file contains the implementation of an Object-Oriented programming system.
-lua> "CLASSES = {}"
+use "core"
immediately
compile [@, me] to: Lua value "self"
-action [new %classname %inst]
- =lua "setmetatable(\%inst, CLASSES[\%classname])"
+ compile [set methods %methods] to
+ %lua <- (Lua "")
+ for %m in %methods
+ to %lua write "\nclass.\(%m as lua id) = \(%m as lua id)"
+ return %lua
+
immediately
- parse [new %classname] as: new %classname {}
+ parse [method %actions %body] as
+ with local %actions
+ action %actions %body
+ set methods %actions
immediately
- compile [call method %method] to: Lua value "self:\(%method as lua expr)"
-
-parse [as %instance %body] as
- result of
- %old_self <- (me)
- (me) <- %instance
- try
- %body
- ..and if it barfs %msg
- (me) <- %old_self
- barf %msg
- ..or if it succeeds
- (me) <- %old_self
-
-compile [object %classname %class_body] to
- %lua <-
+ parse [as %instance %body] as
+ result of
+ %old_self <- (me)
+ (me) <- %instance
+ try
+ %body
+ ..and if it barfs %msg
+ (me) <- %old_self
+ barf %msg
+ ..or if it succeeds
+ (me) <- %old_self
+
+ compile [object %classname extends %parent %class_body] to
Lua ".."
do
local class = {name=\(%classname as lua expr)}
- setmetatable(class, {__tostring=function(cls) return cls.name end})
- CLASSES[class.name] = class
+ 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
- if: %class_body.type != "Block"
- %class_body <- [%class_body]
- for %statement in %class_body
- assume: (%statement.type is "Action") and (%statement.stub is "action % %")
- to %lua write "\n class."
- to %lua write (%statement as lua)
-
- to %lua write ".."
-
- class.__tostring = class["A"..string.as_lua_id("as text")]
- end
-
- return %lua
+ \(%class_body as lua statements)
+
+ class.__tostring = class["A"..string.as_lua_id("as text")]
+ end
+
+parse [object %classname %class_body] as: object %classname extends (nil) %class_body