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. --- containers.lua | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 1b3fd94..a7819ec 100644 --- a/containers.lua +++ b/containers.lua @@ -3,14 +3,50 @@ do local _obj_0 = table insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat end -local repr, stringify, equivalent, nth_to_last, size +local equivalent, nth_to_last, size do local _obj_0 = require('utils') - repr, stringify, equivalent, nth_to_last, size = _obj_0.repr, _obj_0.stringify, _obj_0.equivalent, _obj_0.nth_to_last, _obj_0.size + equivalent, nth_to_last, size = _obj_0.equivalent, _obj_0.nth_to_last, _obj_0.size end local lpeg = require('lpeg') local re = require('re') local List, Dict +local as_nomsu +as_nomsu = function(self) + if type(self) == 'number' then + return tostring(self) + end + do + local mt = getmetatable(self) + if mt then + do + local _as_nomsu = mt.as_nomsu + if _as_nomsu then + return _as_nomsu(self) + end + end + end + end + return error("Not supported: " .. tostring(self)) +end +local as_lua +as_lua = function(self) + if type(self) == 'number' then + return tostring(self) + end + do + local mt = getmetatable(self) + if mt then + do + local _as_lua = mt.as_lua + if _as_lua then + return _as_lua(self) + end + end + end + end + return error("Not supported: " .. tostring(self)) +end local _list_mt = { __eq = equivalent, __tostring = function(self) @@ -19,12 +55,36 @@ local _list_mt = { local _len_0 = 1 for _index_0 = 1, #self do local b = self[_index_0] - _accum_0[_len_0] = repr(b) + _accum_0[_len_0] = tostring(b) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ", ") .. "]" + end, + as_nomsu = function(self) + return "[" .. concat((function() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #self do + local b = self[_index_0] + _accum_0[_len_0] = as_nomsu(b) _len_0 = _len_0 + 1 end return _accum_0 end)(), ", ") .. "]" end, + as_lua = function(self) + return "_List{" .. concat((function() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #self do + local b = self[_index_0] + _accum_0[_len_0] = as_lua(b) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ", ") .. "}" + end, __lt = function(self, other) assert(type(self) == 'table' and type(other) == 'table', "Incompatible types for comparison") for i = 1, math.max(#self, #other) do @@ -162,7 +222,29 @@ local _dict_mt = { local _accum_0 = { } local _len_0 = 1 for k, v in pairs(self) do - _accum_0[_len_0] = tostring(repr(k)) .. ": " .. tostring(repr(v)) + _accum_0[_len_0] = tostring(tostring(k)) .. ": " .. tostring(tostring(v)) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ", ") .. "}" + end, + as_nomsu = function(self) + return "{" .. concat((function() + local _accum_0 = { } + local _len_0 = 1 + for k, v in pairs(self) do + _accum_0[_len_0] = tostring(as_nomsu(k)) .. ": " .. tostring(as_nomsu(v)) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ", ") .. "}" + end, + as_lua = function(self) + return "_Dict{" .. concat((function() + local _accum_0 = { } + local _len_0 = 1 + for k, v in pairs(self) do + _accum_0[_len_0] = "[ " .. tostring(as_lua(k)) .. "]= " .. tostring(as_lua(v)) _len_0 = _len_0 + 1 end return _accum_0 -- cgit v1.2.3 From 28d0792e69bbb9c467c616eb3dc89a60085c35f2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Sep 2018 00:30:53 -0700 Subject: Rebuild. --- containers.lua | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index a7819ec..d31c786 100644 --- a/containers.lua +++ b/containers.lua @@ -353,13 +353,17 @@ do reverse, upper, lower, find, byte, match, gmatch, gsub, sub, format, rep = _obj_0.reverse, _obj_0.upper, _obj_0.lower, _obj_0.find, _obj_0.byte, _obj_0.match, _obj_0.gmatch, _obj_0.gsub, _obj_0.sub, _obj_0.format, _obj_0.rep end local string2 = require('string2') - local lines, line, line_at, as_lua_id - lines, line, line_at, as_lua_id = string2.lines, string2.line, string2.line_at, string2.as_lua_id + local lines, line, line_at, as_lua_id, is_lua_id + lines, line, line_at, as_lua_id, is_lua_id = string2.lines, string2.line, string2.line_at, string2.as_lua_id, string2.is_lua_id local text_methods = { formatted_with_1 = format, byte_1 = byte, position_of_1 = find, position_of_1_after_2 = find, + as_a_lua_identifier = as_lua_id, + is_a_lua_identifier = is_lua_id, + as_a_lua_id = as_lua_id, + is_a_lua_id = is_lua_id, bytes_1_to_2 = function(self, start, stop) return List({ byte(tostring(self), start, stop) @@ -403,10 +407,18 @@ do matches_1 = function(self, patt) return match(self, patt) and true or false end, + matching_1 = function(self, patt) + return (match(self, patt)) + end, + matching_groups_1 = function(self, patt) + return { + match(self, patt) + } + end, [as_lua_id("* 1")] = function(self, n) return rep(self, n) end, - matching_1 = function(self, patt) + all_matches_of_1 = function(self, patt) local result = { } local stepper, x, i = gmatch(self, patt) while true do @@ -434,6 +446,9 @@ do return text_methods[i] end end + getmetatable("").__add = function(self, x) + return tostring(self) .. tostring(x) + end end return { List = List, -- cgit v1.2.3 From 692fae5416ce1f2702b599ffb27b2e3d2235eba7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 12:45:08 -0700 Subject: Incremental fixes and more nomnom ports. --- containers.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index d31c786..682932f 100644 --- a/containers.lua +++ b/containers.lua @@ -192,6 +192,22 @@ local _list_mt = { end end return nil + end, + slice_1_to_2 = function(self, start, stop) + local n = #self + if n < 0 then + start = (n + 1 - start) + end + if n < 0 then + stop = (n + 1 - stop) + end + local _accum_0 = { } + local _len_0 = 1 + for i = start, stop do + _accum_0[_len_0] = self[i] + _len_0 = _len_0 + 1 + end + return _accum_0 end }, __newindex = function(self, k, v) -- 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. --- containers.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 682932f..2018581 100644 --- a/containers.lua +++ b/containers.lua @@ -48,6 +48,7 @@ as_lua = function(self) return error("Not supported: " .. tostring(self)) end local _list_mt = { + __type = "List", __eq = equivalent, __tostring = function(self) return "[" .. concat((function() @@ -231,6 +232,7 @@ walk_items = function(self, i) end end local _dict_mt = { + __type = "Dict", __eq = equivalent, __len = size, __tostring = function(self) -- cgit v1.2.3 From 17dfd248640caae5eecc13be8974378c39953f39 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 26 Sep 2018 13:59:42 -0700 Subject: Added field to remember text methods. --- containers.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 2018581..1490a85 100644 --- a/containers.lua +++ b/containers.lua @@ -455,6 +455,7 @@ do setmetatable(text_methods, { __index = string2 }) + getmetatable("").__methods = text_methods getmetatable("").__index = function(self, i) if type(i) == 'number' then return sub(self, i, i) -- cgit v1.2.3 From 7b127fca6165399226afc831c07268da850c3d6a Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 18:35:37 -0700 Subject: Slight API tweaks. --- containers.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 1490a85..ab632c4 100644 --- a/containers.lua +++ b/containers.lua @@ -27,7 +27,7 @@ as_nomsu = function(self) end end end - return error("Not supported: " .. tostring(self)) + return tostring(self) end local as_lua as_lua = function(self) @@ -45,7 +45,7 @@ as_lua = function(self) end end end - return error("Not supported: " .. tostring(self)) + return tostring(self) end local _list_mt = { __type = "List", @@ -56,7 +56,7 @@ local _list_mt = { local _len_0 = 1 for _index_0 = 1, #self do local b = self[_index_0] - _accum_0[_len_0] = tostring(b) + _accum_0[_len_0] = as_nomsu(b) _len_0 = _len_0 + 1 end return _accum_0 @@ -194,7 +194,7 @@ local _list_mt = { end return nil end, - slice_1_to_2 = function(self, start, stop) + from_1_to_2 = function(self, start, stop) local n = #self if n < 0 then start = (n + 1 - start) @@ -240,7 +240,7 @@ local _dict_mt = { local _accum_0 = { } local _len_0 = 1 for k, v in pairs(self) do - _accum_0[_len_0] = tostring(tostring(k)) .. ": " .. tostring(tostring(v)) + _accum_0[_len_0] = tostring(as_nomsu(k)) .. ": " .. tostring(as_nomsu(v)) _len_0 = _len_0 + 1 end return _accum_0 -- cgit v1.2.3 From b7e768a2f826f6643e47b35272dbb35136d489b6 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 28 Sep 2018 22:15:24 -0700 Subject: Rebuild. --- containers.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index ab632c4..b51b133 100644 --- a/containers.lua +++ b/containers.lua @@ -216,6 +216,8 @@ local _list_mt = { return rawset(self, k, v) end } +_list_mt.__index.as_lua = _list_mt.as_lua +_list_mt.__index.as_nomsu = _list_mt.as_nomsu List = function(t) return setmetatable(t, _list_mt) end -- cgit v1.2.3 From 1a3137a210f7886677c6672c26081152318252a3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 1 Oct 2018 15:25:11 -0700 Subject: Fixed some misnamed functions. --- containers.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index b51b133..ab8647e 100644 --- a/containers.lua +++ b/containers.lua @@ -399,7 +399,7 @@ do return List(lines(self)) end, line_1 = line, - wrap_to_1 = function(self, maxlen) + wrapped_to_1 = function(self, maxlen) local _lines = { } local _list_0 = self:lines() for _index_0 = 1, #_list_0 do @@ -418,10 +418,10 @@ do line_at_1 = function(self, i) return (line_at(self, i)) end, - line_number_of_1 = function(self, i) + line_number_at_1 = function(self, i) return select(2, line_at(self, i)) end, - line_position_of_1 = function(self, i) + line_position_at_1 = function(self, i) return select(3, line_at(self, i)) end, matches_1 = function(self, patt) -- 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. --- containers.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index ab8647e..8ab6c80 100644 --- a/containers.lua +++ b/containers.lua @@ -186,6 +186,13 @@ local _list_mt = { end return false end, + remove_1 = function(self, item) + for i, x in ipairs(self) do + if x == item then + remove(self, i) + end + end + end, index_of_1 = function(self, item) for i, x in ipairs(self) do if x == item then -- 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. --- containers.lua | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'containers.lua') diff --git a/containers.lua b/containers.lua index 8ab6c80..3148dcf 100644 --- a/containers.lua +++ b/containers.lua @@ -134,15 +134,15 @@ local _list_mt = { return ret end, __index = { - add_1 = insert, - append_1 = insert, - add_1_at_index_2 = function(t, x, i) + add = insert, + append = insert, + add_1_at_index = function(t, x, i) return insert(t, i, x) end, - at_index_1_add_2 = insert, + at_index_1_add = insert, pop = remove, remove_last = remove, - remove_index_1 = remove, + remove_index = remove, last = (function(self) return self[#self] end), @@ -165,7 +165,7 @@ local _list_mt = { return _accum_0 end)()) end, - joined_with_1 = function(self, glue) + joined_with = function(self, glue) return table.concat((function() local _accum_0 = { } local _len_0 = 1 @@ -177,7 +177,7 @@ local _list_mt = { return _accum_0 end)(), glue) end, - has_1 = function(self, item) + has = function(self, item) for _index_0 = 1, #self do local x = self[_index_0] if x == item then @@ -186,14 +186,14 @@ local _list_mt = { end return false end, - remove_1 = function(self, item) + remove = function(self, item) for i, x in ipairs(self) do if x == item then remove(self, i) end end end, - index_of_1 = function(self, item) + index_of = function(self, item) for i, x in ipairs(self) do if x == item then return i @@ -201,7 +201,7 @@ local _list_mt = { end return nil end, - from_1_to_2 = function(self, start, stop) + from_1_to = function(self, start, stop) local n = #self if n < 0 then start = (n + 1 - start) @@ -383,20 +383,20 @@ do local lines, line, line_at, as_lua_id, is_lua_id lines, line, line_at, as_lua_id, is_lua_id = string2.lines, string2.line, string2.line_at, string2.as_lua_id, string2.is_lua_id local text_methods = { - formatted_with_1 = format, - byte_1 = byte, - position_of_1 = find, - position_of_1_after_2 = find, + formatted_with = format, + byte = byte, + position_of = find, + position_of_1_after = find, as_a_lua_identifier = as_lua_id, is_a_lua_identifier = is_lua_id, as_a_lua_id = as_lua_id, is_a_lua_id = is_lua_id, - bytes_1_to_2 = function(self, start, stop) + bytes_1_to = function(self, start, stop) return List({ byte(tostring(self), start, stop) }) end, - [as_lua_id("with 1 -> 2")] = gsub, + [as_lua_id("with 1 ->")] = gsub, bytes = function(self) return List({ byte(tostring(self), 1, -1) @@ -405,8 +405,8 @@ do lines = function(self) return List(lines(self)) end, - line_1 = line, - wrapped_to_1 = function(self, maxlen) + line = line, + wrapped_to = function(self, maxlen) local _lines = { } local _list_0 = self:lines() for _index_0 = 1, #_list_0 do @@ -422,22 +422,22 @@ do end return table.concat(_lines, "\n") end, - line_at_1 = function(self, i) + line_at = function(self, i) return (line_at(self, i)) end, - line_number_at_1 = function(self, i) + line_number_at = function(self, i) return select(2, line_at(self, i)) end, - line_position_at_1 = function(self, i) + line_position_at = function(self, i) return select(3, line_at(self, i)) end, - matches_1 = function(self, patt) + matches = function(self, patt) return match(self, patt) and true or false end, - matching_1 = function(self, patt) + matching = function(self, patt) return (match(self, patt)) end, - matching_groups_1 = function(self, patt) + matching_groups = function(self, patt) return { match(self, patt) } @@ -445,7 +445,7 @@ do [as_lua_id("* 1")] = function(self, n) return rep(self, n) end, - all_matches_of_1 = function(self, patt) + all_matches_of = function(self, patt) local result = { } local stepper, x, i = gmatch(self, patt) while true do -- cgit v1.2.3