Example #1
0
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
Example #2
0
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))
    })
Example #3
0
def eval_eq(ast, env):
    _assert_exp_length(ast, 3)
    v1, v2 = evaluate(ast[1], env), evaluate(ast[2], env)
    return boolean(True) if v1 == v2 and is_atom(v1) else boolean(False)
Example #4
0
def eval_atom(ast, env):
    arg = evaluate(ast[1], env)
    return boolean(is_atom(arg))