From 72d699fe86ddb34473b54a0df27d21b4a9159284 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 5 Feb 2019 15:45:27 -0800 Subject: Bunch of changes: - Added shebangs to generated code output - SyntaxTree:map() -> SyntaxTree:with(), and corresponding changes to metaprogramming API - Added (return Lua 1) shorthand for (return (Lua 1)) - (1 and 2 and 3) compile rule mapping to -> (1 and (*extra arguments*)) - Don't scan for errors, just report them when compiling - Syntax changes: - Added prefix actions (e.g. #$foo) - Operator chars now include utf8 chars - Ditch "escaped nomsu" type (use (\ 1) compile action instead) --- syntax_tree.lua | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'syntax_tree.lua') diff --git a/syntax_tree.lua b/syntax_tree.lua index b7b7f83..aa15ef3 100644 --- a/syntax_tree.lua +++ b/syntax_tree.lua @@ -55,6 +55,9 @@ do if type(self) ~= type(other) or #self ~= #other or getmetatable(self) ~= getmetatable(other) then return false end + if self.type ~= other.type then + return false + end for i = 1, #self do if self[i] ~= other[i] then return false @@ -87,7 +90,24 @@ do get_source_code = function(self) return self.__class.source_code_for_tree[self]:sub(self.source.start, self.source.stop - 1) end, - map = function(self, fn) + add = function(self, ...) + local n = #self + for i = 1, select('#', ...) do + self[n + i] = select(i, ...) + end + self.stub = nil + end, + with = function(self, fn) + if type(fn) == 'table' then + local replacements = fn + fn = function(t) + for k, v in pairs(replacements) do + if k == t then + return v + end + end + end + end local replacement = fn(self) if replacement == false then return nil @@ -122,7 +142,7 @@ do repeat replacement[k] = v if SyntaxTree:is_instance(v) then - local r = v:map(fn) + local r = v:with(fn) if r == v or r == nil then _continue_0 = true break @@ -143,6 +163,19 @@ do end return replacement end, + contains = function(self, subtree) + if subtree == self then + return true + end + for k, v in pairs(self) do + if SyntaxTree:is_instance(v) then + if v:contains(subtree) then + return true + end + end + end + return false + end, get_args = function(self) assert(self.type == "Action" or self.type == "MethodCall", "Only actions and method calls have arguments") local args = { } -- cgit v1.2.3