aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-03-02 17:27:19 +0000
committers-ol <s-ol@users.noreply.github.com>2020-03-02 17:43:17 +0000
commitd8e2bad7d08bb96937815de4f3fd1bcbe2c440bd (patch)
tree64463299ae1b363a2addde0559f094193dd94759 /core
parentlift remaining libs to new op interface (diff)
downloadalive-d8e2bad7d08bb96937815de4f3fd1bcbe2c440bd.tar.gz
alive-d8e2bad7d08bb96937815de4f3fd1bcbe2c440bd.zip
dynamic scoping
Diffstat (limited to 'core')
-rw-r--r--core/builtin.moon1
-rw-r--r--core/invoke.moon4
-rw-r--r--core/scope.moon11
3 files changed, 11 insertions, 5 deletions
diff --git a/core/builtin.moon b/core/builtin.moon
index 5dd296a..c91e930 100644
--- a/core/builtin.moon
+++ b/core/builtin.moon
@@ -148,6 +148,7 @@ evaluates and continously updates expr1, expr2, ...
the last expression's value is returned."
eval: (scope, tail) =>
+ scope = Scope scope
Result children: [expr\eval scope for expr in *tail]
class if_ extends Action
diff --git a/core/invoke.moon b/core/invoke.moon
index 7b14e97..1fac7b8 100644
--- a/core/invoke.moon
+++ b/core/invoke.moon
@@ -39,12 +39,12 @@ class fn_invoke extends Action
assert #params == #tail, "argument count mismatch in #{@head}"
- fn_scope = Scope @, scope
+ fn_scope = Scope scope, outer_scope
children = for i=1,#params
name = params[i]\unwrap 'sym'
with L\push tail[i]\eval, outer_scope
- fn_scope\set name, .value
+ fn_scope\set name, \make_ref!
body = body\clone @tag
result = body\eval fn_scope
diff --git a/core/scope.moon b/core/scope.moon
index f795f97..5e73f64 100644
--- a/core/scope.moon
+++ b/core/scope.moon
@@ -1,7 +1,7 @@
import Result, Value from require 'core.value'
class Scope
- new: (@node, @parent) =>
+ new: (@parent, @dynamic_parent) =>
@values = {}
set_raw: (key, val) =>
@@ -11,8 +11,14 @@ class Scope
set: (key, val) =>
L\trace "setting #{key} = #{val} in #{@}"
assert val.__class == Result, "expected #{key}=#{val} to be Result"
+ assert not @values[key], "cannot redefine symbol #{key}!"
@values[key] = val
+ recurse: (key) =>
+ parent = if key\match '^%*.*%*$' then @dynamic_parent else @parent
+ parent or= @parent
+ return parent and L\push parent\get, key
+
get: (key, prefix='') =>
L\debug "checking for #{key} in #{@}"
if val = @values[key]
@@ -22,7 +28,7 @@ class Scope
start, rest = key\match '^(.-)/(.+)'
if not start
- return @parent and L\push -> @parent\get key
+ return @recurse key
child = @get start
assert child and child.value.type == 'scope', "#{start} is not a scope (looking for #{key})"
@@ -40,7 +46,6 @@ class Scope
__tostring: =>
buf = "<Scope"
- buf ..= "@#{@node}" if @node
depth = -1
parent = @parent