diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-15 19:41:22 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-07-15 19:43:28 -0700 |
| commit | be06fc096aa28358914bd6a76b4ea380a04b8873 (patch) | |
| tree | 1f6c300bc8fbeb6cd84008124fca1e43f5513616 /nomsu_tree.lua | |
| parent | 8a44869c4a548692a19afe5258e2540f7d351c8a (diff) | |
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.
Diffstat (limited to 'nomsu_tree.lua')
| -rw-r--r-- | nomsu_tree.lua | 40 |
1 files changed, 15 insertions, 25 deletions
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, { |
