def atomize(elem): if elem == "#f": return boolean(False) elif elem == "#t": return boolean(True) elif elem.isdigit(): return integer(int(elem)) else: return elem # symbols or lists
def get_builtin_env(): """Returns an environment with the builtin functions defined. You probably want to use moolisp.interpreter.default_env instead, which is this extended with the Moo Lisp core functions.""" return Environment({ '+': Builtin(lambda x, y: integer(value_of(x) + value_of(y))), '-': Builtin(lambda x, y: integer(value_of(x) - value_of(y))), '*': Builtin(lambda x, y: integer(value_of(x) * value_of(y))), '/': Builtin(lambda x, y: integer(value_of(x) / value_of(y))), 'mod': Builtin(lambda x, y: integer(value_of(x) % value_of(y))), '=': Builtin(lambda x, y: boolean(x == y)), '>': Builtin(lambda x, y: boolean(x > y)), '<': Builtin(lambda x, y: boolean(x < y)), '>=': Builtin(lambda x, y: boolean(x >= y)), '<=': Builtin(lambda x, y: boolean(x <= y)), 'cons': Builtin(lambda h, rest: [h] + rest), 'car': Builtin(lambda lst: lst[0]), 'cdr': Builtin(lambda lst: 'nil' if len(lst) == 1 else lst[1:]), 'list': Builtin(lambda *args: 'nil' if len(args) == 0 else list(args)) })