aboutsummaryrefslogtreecommitdiff
path: root/nomsu_tree.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-06-13 13:23:24 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-06-13 13:23:39 -0700
commit12d52f743c47c9fcb244b3d6221a18057d0a9e48 (patch)
tree5df489a3f222f03ca6a25925529158f4017a14e2 /nomsu_tree.moon
parent45e0a831fedc54365af619be578982dc3d3260a1 (diff)
Simplifying code.
Diffstat (limited to 'nomsu_tree.moon')
-rw-r--r--nomsu_tree.moon45
1 files changed, 13 insertions, 32 deletions
diff --git a/nomsu_tree.moon b/nomsu_tree.moon
index 89d51c4..b9bcdbd 100644
--- a/nomsu_tree.moon
+++ b/nomsu_tree.moon
@@ -8,29 +8,22 @@ AST = {}
AST.is_syntax_tree = (n)->
type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n)
--- Helper method:
-Tree = (name, methods)->
- cls = methods or {}
+types = {"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry",
+ "IndexChain", "Action"}
+for name in *types
+ cls = {}
with cls
- .type = name
.__class = cls
+ .__index = cls
.__name = name
+ .type = name
.is_instance = (x)=> getmetatable(x) == @
- .__index = cls
- .__tostring = => "#{@name}(#{table.concat([repr(v) for v in *@]), ', '})"
+ .__tostring = => "#{@name}(#{concat([repr(v) for v in *@], ', ')})"
.map = (fn)=>
if replacement = fn(@) then return replacement
- made_changes, new_vals = false, {}
- for i,v in ipairs @
- if AST.is_syntax_tree(v)
- if replacement = v\map(fn)
- if replacement ~= v
- made_changes = true
- v = replacement
- new_vals[i] = v
- return @ unless made_changes
- replacement = getmetatable(self)(@source, unpack(new_vals))
- return replacement
+ replacements = [AST.is_syntax_tree(v) and v\map(fn) or nil for v in *@]
+ return @ unless next(replacements)
+ return (@__class)(@source, unpack([replacements[i] or v for i,v in ipairs(@)]))
AST[name] = setmetatable cls,
__tostring: => @name
@@ -44,20 +37,8 @@ Tree = (name, methods)->
if inst.__init then inst\__init!
return inst
-Tree "Number"
-Tree "Var"
-Tree "Block"
-Tree "EscapedNomsu"
-Tree "Text"
-Tree "List"
-Tree "Dict"
-Tree "DictEntry"
-Tree "IndexChain"
-Tree "Action",
- __init: =>
- stub_bits = [type(a) == 'string' and a or '%' for a in *@]
- @stub = concat stub_bits, " "
- get_spec: =>
- concat [type(a) == "string" and a or "%#{a[1]}" for a in *@], " "
+AST.Action.__init = =>
+ stub_bits = [type(a) == 'string' and a or '%' for a in *@]
+ @stub = concat stub_bits, " "
return AST