Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (67 sloc) 1.64 KB
// We'd like function parameters to not override global variables.
:(scenarios run)
:(scenario function_params_create_new_scope)
(<- x 3)
((fn (x) x) 23)
x
=> 3
:(before "End Globals")
map<cell*, stack<cell*> > Dynamics;
:(replace{} "lookup(cell*")
cell* lookup(cell* sym) {
if (Dynamics[sym].empty()) {
RAISE << "No binding for " << to_string(sym) << '\n';
return nil;
}
return Dynamics[sym].top();
}
:(replace{} "new_binding(cell*")
void new_binding(cell* sym, cell* val) {
trace("bind") << sym << ": " << val;
mkref(sym);
mkref(val);
Dynamics[sym].push(val);
}
void end_binding(cell* sym) {
stack<cell*>& bindings = Dynamics[sym];
if (bindings.empty()) {
RAISE << "No dynamic binding for " << sym << '\n';
return;
}
trace("unbind") << sym;
rmref(sym);
rmref(bindings.top());
bindings.pop();
}
void end_binding(const string& sym) {
end_binding(new_sym(sym));
}
:(after "Eval Cleanup(fn, expr, result)")
end_all_bindings(sig(fn));
:(code)
void end_all_bindings(cell* params) {
if (params == nil) return;
if (is_sym(params)) end_binding(params);
if (!is_tree(params)) return;
end_all_bindings(left(params));
end_all_bindings(right(params));
}
:(scenario cleanup_after_destructured_params)
((fn((a b))))
+unbind: a
+unbind: b
:(after "Test Teardown")
teardown_dynamics();
:(code)
void teardown_dynamics() {
for (map<cell*, stack<cell*> >::iterator p = Dynamics.begin(); p != Dynamics.end(); ++p) {
while (!p->second.empty()) {
rmref(p->first);
rmref(p->second.top());
p->second.pop();
}
}
}
:(delete{} "void teardown_bindings")
:(delete "teardown_bindings()")
:(delete "Bindings")