From be06fc096aa28358914bd6a76b4ea380a04b8873 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 15 Jul 2018 19:41:22 -0700 Subject: Major changes to how versioning and parsing work. This should be a better path going forward to handling upgrades. Old syntax files will stick around for compatibility purposes. Old syntax can be parsed into valid syntax trees via the old syntax (.peg) files, and then old syntax trees should be valid and can be upgraded via the normal code path. This change has lots of improvements to Nomsu codegen too. --- nomsu_tree.lua | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) (limited to 'nomsu_tree.lua') diff --git a/nomsu_tree.lua b/nomsu_tree.lua index d5875bc..2e733a2 100644 --- a/nomsu_tree.lua +++ b/nomsu_tree.lua @@ -26,7 +26,7 @@ local types = { "DictEntry", "IndexChain", "Action", - "File" + "FileChunks" } for _index_0 = 1, #types do local name = types[_index_0] @@ -52,35 +52,25 @@ for _index_0 = 1, #types do end)(), ', ')) .. ")" end cls.map = function(self, fn) - do - local replacement = fn(self) - if replacement then - return replacement - end + local replacement = fn(self) + if replacement ~= nil then + return replacement or nil end - local replacements - do - local _accum_0 = { } - local _len_0 = 1 - for _index_1 = 1, #self do - local v = self[_index_1] - _accum_0[_len_0] = AST.is_syntax_tree(v) and v:map(fn) or nil - _len_0 = _len_0 + 1 + local replacements = { } + local changes = false + for i, v in ipairs(self) do + if AST.is_syntax_tree(v) then + replacement = v:map(fn) + else + replacement = v end - replacements = _accum_0 + changes = changes or (replacement ~= v) + replacements[#replacements + 1] = replacement end - if not (next(replacements)) then + if not (changes) then return self end - return (self.__class)(self.source, unpack((function() - local _accum_0 = { } - local _len_0 = 1 - for i, v in ipairs(self) do - _accum_0[_len_0] = replacements[i] or v - _len_0 = _len_0 + 1 - end - return _accum_0 - end)())) + return (self.__class)(self.source, unpack(replacements)) end end AST[name] = setmetatable(cls, { -- cgit v1.2.3