From 79d4bd5125de7ff220fbf8a8a5493d437ed16963 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 18 Sep 2018 19:48:58 -0700 Subject: Got rid of repr() use and replaced with :as_lua() or :as_nomsu() in as many places as possible. --- lib/object.nom | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index b49b8f2..600ecf6 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -46,7 +46,7 @@ compile [my action %actions %body] to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end) - table.insert(\%args, \(\%me as lua id)) + table.insert(\%args, 1, \(\%me as lua id)) local lua = LuaCode(tree.source, "class.", fn_name, " = ", \(..) compile as (%args -> %body) ..) @@ -54,7 +54,7 @@ compile [my action %actions %body] to: local alias = \%actions[i] local alias_name = alias.stub:as_lua_id() local \%alias_args = table.map(alias:get_args(), function(a) return tostring(nomsu:compile(a)) end) - table.insert(\%alias_args, \(\%me as lua id)) + table.insert(\%alias_args, 1, \(\%me as lua id)) lua:append("\\nclass.", alias_name, " = ") if utils.equivalent(\%args, \%alias_args) then lua:append("class.", fn_name) @@ -110,3 +110,9 @@ compile [object %classname extends %parent %class_body] to: parse [object %classname %class_body] as (..) object %classname extends (nil) %class_body + +parse [%obj is a %class] as (..) + all of [..] + (type of %obj) == "table" + %obj's metatable + (%obj's metatable).__index == %class -- cgit v1.2.3 From f2048235f5cc7ff02db39a0e2fe5c79c7f390e0b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Sep 2018 00:30:28 -0700 Subject: Incremental checkin, currently not working, just saving progress. --- lib/object.nom | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index 600ecf6..65da2a1 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -3,7 +3,7 @@ This file contains the implementation of an Object-Oriented programming system. test: - object "Dog": + object (Dog): (Dog).genus = "Canus" my action [set up]: %me.barks or= 0 my action [bark, woof]: @@ -12,8 +12,10 @@ test: my action [get pissed off]: %me.barks += 1 - %d = (new Dog {barks:2}) - assume (%d.barks == 2) + %d = (Dog {barks:2}) + assume (type of %d) == "Dog" + assume (%d is a "Dog") + assume %d.barks == 2 assume ((%d::bark) == "Bark! Bark!") assume ((%d::woof) == "Bark! Bark!") %d::get pissed off @@ -23,24 +25,24 @@ test: assume ("\(%d.class)" == "Dog") assume (%d.genus == "Canus") assume (%d.barks == 3) - %d2 = (new Dog) + %d2 = (Dog {}) assume (%d2.barks == 0) or barf "Default initializer failed" - with {%d:new Dog {barks:1}}: + with {%d:Dog {barks:1}}: assume ((%d::bark) == "Bark!") - object "Corgi" extends (Dog): + object (Corgi) extends (Dog): my action [sploot] "splooted" my action [bark, woof]: %barks = ("Yip!" for % in 1 to %me.barks) return (%barks::joined with " ") - %corg = (new Corgi) + %corg = (Corgi {}) assume (%corg.barks == 0) - with {%d:new Corgi {barks:1}}: + with {%d:Corgi {barks:1}}: assume ((%d::sploot) == "splooted") or barf "subclass method failed" assume ((%d::bark) == "Yip!") or barf "inheritance failed" assume ((%d::woof) == "Yip!") - with {%d:new Dog {barks:2}}: + with {%d:Dog {barks:2}}: assume ((%d::bark) == "Bark! Bark!") compile [my action %actions %body] to: lua> "\ @@ -69,12 +71,18 @@ compile [my action %actions %body] to: return lua" compile [object %classname extends %parent %class_body] to: + unless (%classname.type == "Action"): + compile error at %classname "Expected this to be an action, not a \(%classname.type)" + for % in %classname: + unless (% is text): + compile error at % "Class names should not have arguments." return (..) Lua "\ ..do - local class = {name=\(%classname as lua expr)} + local class = {name=\(quote %classname.stub)} setmetatable(class, { __index=\(%parent as lua expr), + __type=class.name, __tostring=function(cls) return cls.name end, __call=function(cls, inst) inst = setmetatable(inst or {}, cls) @@ -84,24 +92,21 @@ compile [object %classname extends %parent %class_body] to: return inst end, }) - nomsu.environment[("new "..class.name):as_lua_id()] = class - nomsu.environment[("new "..class.name.." 1"):as_lua_id()] = class + nomsu.environment[(class.name.." 1"):as_lua_id()] = class nomsu.environment[class.name:as_lua_id()] = function() return class end class.__index = class class.class = class class.__tostring = function(inst) return inst.name..getmetatable(_Dict{}).__tostring(inst) end - \(%class_body as lua statements) - local metamethod_map = {["as text"]="__tostring", ["clean up"]="__gc", ["+ 1"]="__add", ["- 1"]="__sub", ["* 1"]="__mul", ["/ 1"]="__div", ["-"]="__unm", ["// 1"]="__idiv", ["mod 1"]="__mod", ["^ 1"]="__pow", ["& 1"]="__band", ["| 1"]="__bor", ["~ 1"]="__bxor", ["~"]="__bnot", ["<< 1"]="__bshl", [">> 1"]="__bshr", ["== 1"]="__eq", ["< 1"]="__lt", - ["<= 1"]="__le", ["set 1 = 2"]="__newindex", ["length"]="__len", - ["__ipairs"]="__ipairs", ["__pairs"]="__pairs", + ["<= 1"]="__le", ["set 1 = 2"]="__newindex", ["size"]="__len", + ["iterate"]="__ipairs", ["iterate all"]="__pairs", } for stub,metamethod in pairs(metamethod_map) do class[metamethod] = class[stub:as_lua_id()] @@ -111,8 +116,3 @@ compile [object %classname extends %parent %class_body] to: parse [object %classname %class_body] as (..) object %classname extends (nil) %class_body -parse [%obj is a %class] as (..) - all of [..] - (type of %obj) == "table" - %obj's metatable - (%obj's metatable).__index == %class -- cgit v1.2.3 From b43432e647fbb3bb76aa2836e3899d5e407c50f9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 13:05:28 -0700 Subject: Fixed all syntax errors, got original (non-nomnom) tests passing. --- lib/object.nom | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index 65da2a1..25ff644 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -80,9 +80,9 @@ compile [object %classname extends %parent %class_body] to: Lua "\ ..do local class = {name=\(quote %classname.stub)} + class.__type = class.name setmetatable(class, { __index=\(%parent as lua expr), - __type=class.name, __tostring=function(cls) return cls.name end, __call=function(cls, inst) inst = setmetatable(inst or {}, cls) -- cgit v1.2.3 From 23abab4f809e8d4b825746580082292db700036b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 29 Oct 2018 13:00:08 -0700 Subject: Some cleanups and fixes, made the parser more permissive of prematurely terminated files. --- lib/object.nom | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index 25ff644..bbc6e06 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -2,6 +2,15 @@ # This file contains the implementation of an Object-Oriented programming system. +%globals.METAMETHOD_MAP = {..} + "as text": "__tostring", "clean up": "__gc", + "+ 1": "__add", "- 1": "__sub", "* 1": "__mul", "/ 1": "__div", + "-": "__unm", "// 1": "__idiv", "mod 1": "__mod", "^ 1": "__pow", + "& 1": "__band", "| 1": "__bor", "~ 1": "__bxor", "~": "__bnot", + "<< 1": "__bshl", ">> 1": "__bshr", "== 1": "__eq", "< 1": "__lt", + "<= 1": "__le", "set 1 = 2": "__newindex", "size": "__len", + "iterate": "__ipairs", "iterate all": "__pairs", + test: object (Dog): (Dog).genus = "Canus" @@ -86,9 +95,7 @@ compile [object %classname extends %parent %class_body] to: __tostring=function(cls) return cls.name end, __call=function(cls, inst) inst = setmetatable(inst or {}, cls) - if inst.set_up then - inst:set_up() - end + if inst.set_up then inst:set_up() end return inst end, }) @@ -100,15 +107,7 @@ compile [object %classname extends %parent %class_body] to: return inst.name..getmetatable(_Dict{}).__tostring(inst) end \(%class_body as lua statements) - local metamethod_map = {["as text"]="__tostring", ["clean up"]="__gc", - ["+ 1"]="__add", ["- 1"]="__sub", ["* 1"]="__mul", ["/ 1"]="__div", - ["-"]="__unm", ["// 1"]="__idiv", ["mod 1"]="__mod", ["^ 1"]="__pow", - ["& 1"]="__band", ["| 1"]="__bor", ["~ 1"]="__bxor", ["~"]="__bnot", - ["<< 1"]="__bshl", [">> 1"]="__bshr", ["== 1"]="__eq", ["< 1"]="__lt", - ["<= 1"]="__le", ["set 1 = 2"]="__newindex", ["size"]="__len", - ["iterate"]="__ipairs", ["iterate all"]="__pairs", - } - for stub,metamethod in pairs(metamethod_map) do + for stub,metamethod in pairs(globals.METAMETHOD_MAP) do class[metamethod] = class[stub:as_lua_id()] end end" -- cgit v1.2.3 From ea3197aaffba00318920ed5e1e33ca5f2a5e6c5c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 30 Oct 2018 23:42:04 -0700 Subject: Fully working version of (action [foo]: baz) -> ((foo) means: baz) refactor and misc other changes. --- lib/object.nom | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index bbc6e06..4f45188 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -1,15 +1,13 @@ -#!/usr/bin/env nomsu -V4.8.8.6 +#!/usr/bin/env nomsu -V4.8.10 # This file contains the implementation of an Object-Oriented programming system. %globals.METAMETHOD_MAP = {..} - "as text": "__tostring", "clean up": "__gc", - "+ 1": "__add", "- 1": "__sub", "* 1": "__mul", "/ 1": "__div", - "-": "__unm", "// 1": "__idiv", "mod 1": "__mod", "^ 1": "__pow", - "& 1": "__band", "| 1": "__bor", "~ 1": "__bxor", "~": "__bnot", - "<< 1": "__bshl", ">> 1": "__bshr", "== 1": "__eq", "< 1": "__lt", - "<= 1": "__le", "set 1 = 2": "__newindex", "size": "__len", - "iterate": "__ipairs", "iterate all": "__pairs", + "as text":"__tostring", "clean up":"__gc", "+ 1":"__add", "- 1":"__sub" + "* 1":"__mul", "/ 1":"__div", "-":"__unm", "// 1":"__idiv", "mod 1":"__mod" + "^ 1":"__pow", "& 1":"__band", "| 1":"__bor", "~ 1":"__bxor", "~":"__bnot" + "<< 1":"__bshl", ">> 1":"__bshr", "== 1":"__eq", "< 1":"__lt", "<= 1":"__le" + "set 1 = 2":"__newindex", size:"__len", iterate:"__ipairs", "iterate all":"__pairs" test: object (Dog): @@ -53,13 +51,13 @@ test: with {%d:Dog {barks:2}}: assume ((%d::bark) == "Bark! Bark!") -compile [my action %actions %body] to: +(my action %actions %body) compiles to: lua> "\ ..local fn_name = \%actions[1].stub:as_lua_id() local \%args = table.map(\%actions[1]:get_args(), function(a) return tostring(nomsu:compile(a)) end) table.insert(\%args, 1, \(\%me as lua id)) local lua = LuaCode(tree.source, "class.", fn_name, " = ", \(..) - compile as (%args -> %body) + what (%args -> %body) compiles to ..) for i=2,#\%actions do local alias = \%actions[i] @@ -79,12 +77,13 @@ compile [my action %actions %body] to: end return lua" -compile [object %classname extends %parent %class_body] to: +(object %classname extends %parent %class_body) compiles to: unless (%classname.type == "Action"): compile error at %classname "Expected this to be an action, not a \(%classname.type)" for % in %classname: unless (% is text): compile error at % "Class names should not have arguments." + return (..) Lua "\ ..do @@ -112,6 +111,5 @@ compile [object %classname extends %parent %class_body] to: end end" -parse [object %classname %class_body] as (..) +(object %classname %class_body) parses as (..) object %classname extends (nil) %class_body - -- cgit v1.2.3 From 307dea18815ba4a06a3098edb170d7ad90708815 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 2 Nov 2018 14:38:24 -0700 Subject: Changed stub convention to (foo 1 baz 2) -> foo_1_baz instead of foo_1_baz_2, removed "smext", made some cleanup changes. --- lib/object.nom | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/object.nom') diff --git a/lib/object.nom b/lib/object.nom index 4f45188..d5555df 100644 --- a/lib/object.nom +++ b/lib/object.nom @@ -93,13 +93,13 @@ test: __index=\(%parent as lua expr), __tostring=function(cls) return cls.name end, __call=function(cls, inst) - inst = setmetatable(inst or {}, cls) + if inst == nil then return cls end + inst = setmetatable(inst, cls) if inst.set_up then inst:set_up() end return inst end, }) - nomsu.environment[(class.name.." 1"):as_lua_id()] = class - nomsu.environment[class.name:as_lua_id()] = function() return class end + nomsu.environment[class.name:as_lua_id()] = class class.__index = class class.class = class class.__tostring = function(inst) -- cgit v1.2.3