aboutsummaryrefslogtreecommitdiffstats
path: root/ast.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-01 21:29:10 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-01 21:29:10 +0000
commitf4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03 (patch)
tree636cbcbbcd8be60d39cba2e4cf04b4edc7011233 /ast.moon
parentfix README typo (diff)
downloadalive-f4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03.tar.gz
alive-f4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03.zip
first-class scopes
Diffstat (limited to 'ast.moon')
-rw-r--r--ast.moon26
1 files changed, 20 insertions, 6 deletions
diff --git a/ast.moon b/ast.moon
index c5bfd07..7996c0f 100644
--- a/ast.moon
+++ b/ast.moon
@@ -1,6 +1,15 @@
import Const from require 'base'
+import Scope from require 'scope'
unpack or= table.unpack
+hash = (tbl) ->
+ mt = getmetatable tbl
+ setmetatable tbl, nil
+ str = tostring tbl
+ setmetatable tbl, mt
+
+ '#' .. str\sub 10
+
class ASTNode
-- first pass (outin):
-- * expand macros
@@ -23,13 +32,13 @@ class Atom
str = str\gsub "\\\\", "\\"
str
expand: (scope) =>
- @value = Const switch @atom_type
+ @value = switch @atom_type
when 'num'
- tonumber @raw
- when 'sym'
- assert scope[@raw], "undefined reference to symbol '#{@raw}'"
+ Const 'num', tonumber @raw
when 'strq', 'strd'
- unescape @raw
+ Const 'str', unescape @raw
+ when 'sym'
+ assert (scope\get @raw), "undefined reference to symbol '#{@raw}'"
else
error "unknown atom type: '#{@atom_type}'"
@@ -53,7 +62,8 @@ class Atom
@make_strd: (match) -> Atom match, 'strd'
@make_strq: (match) -> Atom match, 'strq'
- __tostring: => @stringify!
+ __tostring: =>
+ "<Atom#{hash @} #{@stringify!}>"
class Xpr
type: 'Xpr'
@@ -76,8 +86,12 @@ class Xpr
@white[i/2] = parts[i+1]
expand: (scope) =>
+ scope = Scope @, scope
+ for child in *@
+ child\expand scope
link: =>
+ head = @head!
head: => @[1].value
tail: => unpack [p.value for p in *@[2,]]