def unparse(ast): if is_boolean(ast): return "#t" if value_of(ast) else "#f" elif is_integer(ast): return str(value_of(ast)) elif isinstance(ast, list): if len(ast) > 0 and ast[0] in quote_names: return "%s%s" % (quote_names[ast[0]], unparse(ast[1])) else: return "(%s)" % " ".join([unparse(x) for x in ast]) else: return str(ast) # string, integer or Closure
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)) })
def eval_cond(ast, env): for predicate, ast in ast[1:]: p = evaluate(predicate, env) _assert_boolean(p, predicate) if value_of(p) is True: return evaluate(ast, env)