aboutsummaryrefslogtreecommitdiff
path: root/lib/object.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-08-27 13:38:58 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-08-27 13:39:22 -0700
commit930d522fbc3ab57faa926ed85f0d35d661722402 (patch)
tree8c91c71728b43b4e1817ed182c39bf991cb4b2a9 /lib/object.nom
parentc6a7b0be9ebdddb58a03749705987cb3058060d3 (diff)
Bunch of miscellaneous changes. Paved the way a little bit for having
different compiler domains.
Diffstat (limited to 'lib/object.nom')
-rw-r--r--lib/object.nom77
1 files changed, 45 insertions, 32 deletions
diff --git a/lib/object.nom b/lib/object.nom
index dd99109..ddbb2b5 100644
--- a/lib/object.nom
+++ b/lib/object.nom
@@ -35,23 +35,34 @@ test:
assume ((me).barks == 101) or barf ".."
Error in nested 'as % %' failed to properly reset 'self'
- object "Corgi" extends (class Dog) (method [sploot] "splooted")
+ object "Corgi" extends (class Dog):
+ method [sploot] "splooted"
+ method [bark, woof]:
+ %barks = ("Yip!" for % in 1 to (me).barks)
+ return (%barks joined with " ")
+
%corg = (new Corgi)
assume (%corg.barks == 0)
as (new Corgi {barks:1}):
assume ((sploot) == "splooted") or barf "subclass method failed"
- assume ((bark) == "Bark!") or barf "inheritance failed"
- assume ((woof) == "Bark!")
+ assume ((bark) == "Yip!") or barf "inheritance failed"
+ assume ((woof) == "Yip!")
+
+ as (new Dog {barks:2}):
+ assume ((bark) == "Bark! Bark!")
+
compile [@, me] to (Lua value "self")
compile [method %actions %body] to:
- %lua = (compile as (local action %actions %body))
+ %lua = (\(local action \[%actions.1] %body) as lua)
+ declare locals in %lua
for % in %actions:
- to %lua write "\nclass.\(% as lua id) = \(% as lua id)"
- return (..)
+ to %lua write "\n\(\%class as lua id).\(% as lua id) = \(%actions.1 as lua id)"
+ %lua = (..)
Lua ".."
do -- Method: \(%actions.(1).stub)
\%lua
end
+ return %lua
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -64,33 +75,35 @@ parse [as %instance %body] as (..)
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, {
- __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
-
- \(%class_body as lua statements)
-
- class.__tostring = class["A"..string.as_lua_id("as text")] or function(inst)
- return inst.name..getmetatable(dict{}).__tostring(inst)
+compile [object %classname extends %parent %class_body] to:
+ %class = (\%class as lua id)
+ return (..)
+ 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
+
+ \(%class_body as lua statements)
+
+ \%class.__tostring = \%class["A"..string.as_lua_id("as text")] or function(inst)
+ return inst.name..getmetatable(dict{}).__tostring(inst)
+ end
end
- end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~