aboutsummaryrefslogtreecommitdiff
path: root/syntax_tree.lua
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-02-05 15:45:27 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-02-05 15:47:01 -0800
commit72d699fe86ddb34473b54a0df27d21b4a9159284 (patch)
treebdb4036181b5c15d920d1500f8f67407a44d2014 /syntax_tree.lua
parent0ff3219f355b4307783288800724213636920912 (diff)
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)
Diffstat (limited to 'syntax_tree.lua')
-rw-r--r--syntax_tree.lua37
1 files changed, 35 insertions, 2 deletions
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 = { }