def getOptCode(body,env, param_list): if isinstance(body,LispSymbol): index = _getParameter(param_list,body) if index > -1: return lispList(new(LispSymbol,"getParam"),LispInteger(index)) else: index = _getLocal(env,body) if index > -1: return lispList(new(LispSymbol,"getLocal"),LispInteger(index)) else: (index,super) =_getSuperParam(env,body) if index >-1: return lispList(new(LispSymbol,"getSuperParam"),LispInteger(index),LispInteger(super)) else: (index,super) =_getSuperLocal(env,body) if index >-1: return lispList(new(LispSymbol,"getSuperLocal"),LispInteger(index),LispInteger(super)) else: index = _getGlobal(env,body) if index > -1: return lispList(new(LispSymbol,"getGlobal"),LispInteger(index)) if isinstance(body,LispCons): return __getOptFunction(body, env, param_list) return body
def __getOptFunction(func,env,param_list): if func.first == new(LispSymbol,"lambda"): first = getOptCode(func.first, env, param_list) return LispCons(first,func.rest) if func.first == new(LispSymbol,"define"): env.put(func.rest.first,new(LispNull)) first = getOptCode(func.first, env, param_list) symb = func.rest.first rest = getOptCode(func.rest.rest, env, param_list) return lispList(first,symb,rest) if func.first == new(LispSymbol,"+"): func = _normalizeFunction(func) print func first = getOptCode(func.first, env, param_list) rest = getOptCode(func.rest, env, param_list) return LispCons(first,rest)
def _normalizeFunction(func): if func.rest.rest.rest == new(LispNull): return func else: return _normalizeFunction(lispList(func.first, func.second(), _normalizeFunction(LispCons(func.first,func.rest.rest))))