From f4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03 Mon Sep 17 00:00:00 2001 From: s-ol Date: Sat, 1 Feb 2020 22:29:10 +0100 Subject: first-class scopes --- ast.moon | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'ast.moon') 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: => + "" 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,]] -- cgit v1.2.3