aboutsummaryrefslogtreecommitdiff
path: root/nomsu_tree.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-06-04 17:56:09 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-06-04 17:57:07 -0700
commit563e415e07ea45df8c80fc9a2afc652e3e6d8c83 (patch)
tree1f752ef1009b5417e887ba56fe56559fa10e5165 /nomsu_tree.moon
parente7bdc35aa82339f0734ec7014d2da0459ff0462d (diff)
Switched from Node(Tuple(values...), source) to Node(source, values...),
thanks to support from immutable-tables for mixed tables.
Diffstat (limited to 'nomsu_tree.moon')
-rw-r--r--nomsu_tree.moon57
1 files changed, 29 insertions, 28 deletions
diff --git a/nomsu_tree.moon b/nomsu_tree.moon
index 4e9ec3d..fe365c2 100644
--- a/nomsu_tree.moon
+++ b/nomsu_tree.moon
@@ -13,18 +13,19 @@ Types.is_node = (n)->
type(n) == 'userdata' and getmetatable(n) and Types[n.type] == getmetatable(n)
-- Helper method:
-Tree = (name, kind, methods)->
+Tree = (name, fields, methods)->
methods or= {}
- assert((kind == 'single') or (kind == 'multi'))
- is_multi = (kind == 'multi')
+ is_multi = true
+ for f in *fields do is_multi and= (f != "value")
with methods
.type = name
.name = name
- .__new or= (value, source)=>
+ .__new or= (source, ...)=>
assert source
if type(source) == 'string'
source = Source\from_string(source)
- return value, source
+ --assert Source\is_instance(source)
+ return source, ...
.is_multi = is_multi
.map = (fn)=>
if type(fn) == 'table'
@@ -33,43 +34,43 @@ Tree = (name, kind, methods)->
fn = (k)-> _replacements[k]
return @_map(fn)
if is_multi
- .__tostring = => "#{@name}(#{table.concat [repr(v) for v in *@value], ', '})"
+ .__tostring = => "#{@name}(#{table.concat [repr(v) for v in *@], ', '})"
._map = (fn)=>
if ret = fn(@)
return ret
- new_vals = [v._map and v\_map(fn) or v for v in *@value]
- ret = getmetatable(self)(Tuple(unpack(new_vals)), @source)
+ new_vals = [v._map and v\_map(fn) or v for v in *@]
+ ret = getmetatable(self)(@source, unpack(new_vals))
return ret
- .__ipairs = => error!
else
.__tostring = => "#{@name}(#{repr(@value)})"
._map = (fn)=>
fn(@) or @
- if name == "Action"
- Types[name] = immutable {"value", "source", "stub"}, methods
- else
- Types[name] = immutable {"value", "source"}, methods
+ Types[name] = immutable fields, methods
-Tree "Block", 'multi'
-Tree "EscapedNomsu", 'multi'
-Tree "Text", 'multi'
-Tree "List", 'multi'
-Tree "Dict", 'multi'
-Tree "DictEntry", 'multi'
-Tree "IndexChain", 'multi'
-Tree "Number", 'single'
-Tree "Comment", 'single'
-Tree "Var", 'single'
+Tree "Block", {"source"}
+Tree "EscapedNomsu", {"source"}
+Tree "Text", {"source"}
+Tree "List", {"source"}
+Tree "Dict", {"source"}
+Tree "DictEntry", {"source"}
+Tree "IndexChain", {"source"}
+Tree "Number", {"source", "value"}
+Tree "Var", {"source", "value"}
-Tree "Action", 'multi',
- __new: (value, source)=>
+Tree "Action", {"source", "stub"},
+ __new: (source, ...)=>
assert source
if type(source) == 'string'
source = Source\from_string(source)
- stub = concat [type(a) == "string" and a or "%" for a in *value], " "
- return value, source, stub
+ --assert Source\is_instance(source)
+ stub_bits = {}
+ for i=1,select("#",...)
+ a = select(i, ...)
+ stub_bits[i] = type(a) == 'string' and a or "%"
+ stub = concat stub_bits, " "
+ return source, stub, ...
get_spec: =>
- concat [type(a) == "string" and a or "%#{a.value}" for a in *@value], " "
+ concat [type(a) == "string" and a or "%#{a.value}" for a in *@], " "
return Types