aboutsummaryrefslogtreecommitdiffstats
path: root/scope.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-01 23:51:40 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-01 23:55:30 +0000
commit177cd265947bfd9db20ad1a99ae07c76b6b4f147 (patch)
tree49af71e06770925abb0ab1fd2e89be077281b74c /scope.moon
parentfirst-class scopes (diff)
downloadalive-177cd265947bfd9db20ad1a99ae07c76b6b4f147.tar.gz
alive-177cd265947bfd9db20ad1a99ae07c76b6b4f147.zip
macros and scopes
Diffstat (limited to 'scope.moon')
-rw-r--r--scope.moon45
1 files changed, 37 insertions, 8 deletions
diff --git a/scope.moon b/scope.moon
index a72a606..907269e 100644
--- a/scope.moon
+++ b/scope.moon
@@ -27,12 +27,14 @@ constify = (val, key) ->
switch ancestor klass
when Op
'op'
+ when Scope
+ 'scope'
when Const
return val
else
error "#{key}: cannot constify '#{klass.__name}' instance"
else
- return Scope.from_table val
+ return Const 'scope', Scope.from_table val
else
error "#{key}: cannot constify Lua type '#{type val}'"
@@ -42,26 +44,53 @@ class Scope
new: (@node, @parent) =>
@values = {}
+ log: (msg) => -- print msg .. " in #{@}"
+
set_raw: (key, val) => @values[key] = constify val, key
set: (key, val) =>
+ @log "setting #{key} = #{val}"
@values[key] = val
- get: (key) =>
+ get: (key, prefix='') =>
+ @log "checking for #{key}"
if val = @values[key]
+ @log "found #{val}"
return val
- if val = @parent and @parent\get key
- return val
+ start, rest = key\match '^(.-)/(.*)'
+
+ if not start
+ return @parent\get key
- part, key = key\match '^(.-)/(.*)'
- return unless part and key
- scope = assert (@get part), "no such scope: '#{key}'"
- scope\get key
+ scope = @get start
+ assert scope and scope.type == 'scope', "cant find '#{prefix}#{start}' for '#{prefix}#{key}'"
+ scope\getc!\get rest, "#{prefix}#{start}/"
+
+ use: (other) =>
+ for k, v in pairs other.values
+ @values[k] = v
from_table: (tbl) ->
with Scope!
.values = { k, constify v, k for k,v in pairs tbl }
+ __tostring: =>
+ buf = "<Scope"
+ buf ..= "@#{@node}" if @node
+
+ depth = -1
+ parent = @parent
+ while parent
+ depth += 1
+ parent = parent.parent
+ buf ..= " ^#{depth}" if depth != 0
+
+ buf ..= " [#{table.concat [key for key in pairs @values], ', '}]"
+
+ buf ..= ">"
+ buf
+
+
{
:Scope
}