aboutsummaryrefslogtreecommitdiff
path: root/syntax_tree.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-09-18 19:48:58 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-09-18 19:49:29 -0700
commit79d4bd5125de7ff220fbf8a8a5493d437ed16963 (patch)
treefa09e1a5ccb384373b50e27067a7cc3a25669550 /syntax_tree.moon
parentd11f9bc5d3971a68dd78cdaca0b046a40a329000 (diff)
Got rid of repr() use and replaced with :as_lua() or :as_nomsu() in as
many places as possible.
Diffstat (limited to 'syntax_tree.moon')
-rw-r--r--syntax_tree.moon23
1 files changed, 20 insertions, 3 deletions
diff --git a/syntax_tree.moon b/syntax_tree.moon
index 305a1ee..d2f5d37 100644
--- a/syntax_tree.moon
+++ b/syntax_tree.moon
@@ -1,6 +1,5 @@
-- This file contains the datastructures used to represent parsed Nomsu syntax trees,
-- as well as the logic for converting them to Lua code.
-{:repr} = require 'utils'
{:insert, :remove, :concat} = table
{:Source} = require "code_obj"
unpack or= table.unpack
@@ -9,6 +8,14 @@ AST = {}
AST.is_syntax_tree = (n, t=nil)->
type(n) == 'table' and getmetatable(n) and AST[n.type] == getmetatable(n) and (t == nil or n.type == t)
+as_lua = =>
+ if type(@) == 'number'
+ return tostring(@)
+ if mt = getmetatable(@)
+ if _as_lua = mt.as_lua
+ return _as_lua(@)
+ error("Not supported: #{@}")
+
types = {"Number", "Var", "Block", "EscapedNomsu", "Text", "List", "Dict", "DictEntry",
"IndexChain", "Action", "FileChunks", "Error", "Comment"}
for name in *types
@@ -19,8 +26,18 @@ for name in *types
.__name = name
.type = name
.is_instance = (x)=> getmetatable(x) == @
- .__tostring = => "#{@type}#{repr @, (->)}"
- .__repr = => "#{@type}#{repr @, (->)}"
+ .__tostring = =>
+ bits = [tostring(b) for b in *@]
+ for k,v in pairs(@)
+ unless bits[k]
+ table.insert(bits, "[ #{tostring(k)}]=#{tostring(v)}")
+ return "#{@type}{#{table.concat(bits, ", ")}}"
+ .as_lua = =>
+ bits = [as_lua(b) for b in *@]
+ for k,v in pairs(@)
+ unless bits[k]
+ table.insert(bits, "[ #{as_lua(k)}]=#{as_lua(v)}")
+ return "#{@type}{#{table.concat(bits, ", ")}}"
.source_code_for_tree = setmetatable({}, {__index:(t)=>
s = t.source
Files = require 'files'