Implement the define and lookup methods of the Frame class. Each Frame object has the following instance attributes:
bindings is a dictionary representing the bindings in the frame. It maps Scheme symbols (represented as Python strings) to Scheme values.
parent is the parent Frame instance. The parent of the Global Frame is None.
Solution:
Class Frame
defdefine(self,symbol,value):"""Define Scheme SYMBOL to have VALUE."""# BEGIN PROBLEM 3"*** YOUR CODE HERE ***" self.bindings[symbol]= value# END PROBLEM 3deflookup(self,symbol):"""Return the value bound to SYMBOL. Errors if SYMBOL is not found."""# BEGIN PROBLEM 3"*** YOUR CODE HERE ***"if symbol in self.bindings:return self.bindings[symbol]elif self.parent:return self.parent.lookup(symbol)# END PROBLEM 3
Complete the apply method in the BuiltinProcedure class
Solution:
# BEGIN PROBLEM 4"*** YOUR CODE HERE ***"if self.use_env: python_args.append(env)try:return self.fn(*python_args)exceptTypeError:raiseSchemeError('Wrong number of arguments: {0}'.format(len(args)))# END PROBLEM 4
defdo_define_form(expressions,env):"""Evaluate a define form."""check_form(expressions, 2) target = expressions.firstifscheme_symbolp(target):check_form(expressions, 2, 2)# BEGIN PROBLEM 6"*** YOUR CODE HERE ***"iflen(expressions.second)==1: val =scheme_eval(expressions.second.first, env)else: val =scheme_eval(expressions.second, env) env.define(target, val)return target# END PROBLEM 6
defdo_quote_form(expressions,env):"""Evaluate a quote form."""check_form(expressions, 1, 1)# BEGIN PROBLEM 7"*** YOUR CODE HERE ***"return expressions.first# END PROBLEM 7