def eval(self, exp, env): length = Special.util.length(exp) if(length != 3): self._error('invalid number of arguments for define') return Nil.getInstance() args = exp.getCdr().getCar() body = exp.getCdr().getCdr() if(args.isPair()): funcName = args.getCar() args = args.getCdr() if(not (funcName.isSymbol)): self._error('invalid function name') return Nil.getInstance() params = args while(params.isPair()): if(not (params.getCar().isSymbol())): self._error('invalid parameter') return Nil.getInstance() params = params.getCdr() function = Cons(Ident('lambda'), Cons(args, body)) function = function.eval(env) env.define(funcName, function) return Nil.getInstance() if(args.isSymbol): env.define(args, body.getCar().eval(env)) return Nil.getInstance() self._error('invalid expression with define')
def eval(self, exp, env): #sys.stdout.write('inside define.eval') t = Special.util.length(exp) if t < 3: self._error("define error") return Nil.getInstance() elif exp.getCdr().getCar().isPair(): vars = exp.getCdr().getCar() parms = vars.getCdr() sym = vars.getCar() rest = exp.getCdr().getCdr() func = Cons(Ident("lambda"), Cons(parms, rest)) env.define(sym, func.eval(env)) return Nil.getInstance() else: vars = exp.getCdr().getCar() val = exp.getCdr().getCdr().getCar() if vars.isSymbol(): if t == 3: env.define(vars, val.eval(env)) return Nil.getInstance()