After reading Lisp in Your Language, I've been trying to follow along in Python. Everything works until I get to variable assignment and scoping.
The final call has me stumped. It seems that in javascript, one can create a new scope object and assign or bind objects to that new scope, instead of using "this". In the example linked:
// use existing local scope or create a new one
scope = scope || {};
[...]
// call the function with these arguments
// and expose scope as this
return fn.apply(scope, args);
In Python, that doesn't seem to be possible in the same way. I had been calling return fn(*args)
before this point, but now I am stumped as to how I can write the equivalent code.
Any ideas or suggestions?
EDIT: I've found a solution for this problem, at least for now:
# defined outside of the function
global scope_dict
scope_dict = {}
def pl_def(name, value):
global scope_dict
scope_dict[name] = value
return scope_dict[name]
[...]
# use existing scope inside the function
global scope_dict
scope = scope_dict
# resolve all our new string names from our scope
def symbol_check(symbol):
if isinstance(symbol, list):
return symbol
elif symbol in scope:
return scope[symbol]
else:
return symbol
expression = [symbol_check(x) for x in rawExpr]
[...]
# call the function with these arguments
return fn(*args)