Exemple #1
0
def test_or_shortcircuit():
    source = ['(or #t never-reached)']
    exp = parse_tokens(tokenize(source))[0]
    try:
        eval_in_env(exp, Environment([]))
    except Exception as e:
        assert str(e) != 'unknown variable "never-reached"'
Exemple #2
0
def test_eval_recursion():
    env = Environment([['sumto', ['closure', ['lambda', ['n'],
                                    ['if',  ['<', 'n', 1],
                                            0,
                                            ['+', 'n', ['sumto', ['-', 'n', 1]]]]],
                                 []]]])
    exp = ['sumto', 3]
    assert eval_in_env(exp, env) == 6
Exemple #3
0
def repl():
    color_print('****************************************', 'blue')
    color_print('lis.py - a simple Lisp written in Python', 'blue')
    color_print('(c) Nick Collins, 2013', 'blue')
    color_print('****************************************', 'blue')
    env = Environment([])
    while True:
        try:
            user_input = raw_input('lis.py> ')
            exp = parse_tokens(tokenize([user_input]))[0]
            tmp_env = Environment([], env)
            color_print(eval_in_env(exp, tmp_env), 'green')
            env = tmp_env
        except EOFError:
            color_print('\nLeaving lis.py.', 'blue')
            break
        except LisNameError as e:
            color_print(e, 'red')
        except LisSyntaxError as e:
            color_print(e, 'red')
        except Exception as e:
            color_print(e, 'red')
            print('*** Invalid input ***')
Exemple #4
0
def test_eval_if():
    exp = ['if', True, 3, 4]
    assert eval_in_env(exp, Environment([])) == 3
Exemple #5
0
def test_eval_cond_false():
    exp = ['cond', [['>', 4, 4], 1], ['else', 0]]
    assert eval_in_env(exp, Environment([])) == 0
Exemple #6
0
def test_eval_set():
    env = Environment([['a', 0]])
    exp = ['set!', 'a', 10]
    eval_in_env(exp, env)
    assert env.lookup('a') == 10
Exemple #7
0
def test_nullcheck_2():
    source = ['(null? (cons 1 null))']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, []) == False
Exemple #8
0
def test_list():
    source = ['(list 1 2 (+ 1 2) 4)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, []) == [1, 2, 3, 4]
Exemple #9
0
def test_eval_recursion_2():
    env = Environment([['factorial', ['closure', ['lambda', ['n'],
                          ['if', ['<', 'n', 1], 1, ['*', 'n', ['factorial', ['-', 'n', 1]]]]], []]]])
    exp = ['factorial', 5]
    assert eval_in_env(exp, env) == 120
Exemple #10
0
def test_gt():
    source0, source1 = ['(> 2 2)'], ['(> 3 2)']
    exp0 = parse_tokens(tokenize(source0))[0]
    exp1 = parse_tokens(tokenize(source1))[0]
    assert eval_in_env(exp0, Environment([])) == False
    assert eval_in_env(exp1, Environment([])) == True
Exemple #11
0
def test_eval_closure():
    env = Environment([['add3', ['closure', ['lambda', ['x'], ['+', 'x', 3]], []]]])
    exp = ['add3', 10]
    assert eval_in_env(exp, env) == 13
Exemple #12
0
def test_subtract():
    source = ['(- 10 14)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == -4
Exemple #13
0
def test_divide():
    source = ['(/ 10 3)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == 3
Exemple #14
0
def test_multiply():
    source = ['(* 1 3 5)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == 15
Exemple #15
0
def test_cons():
    source = ['(cons (+ 1 3 5) null)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == [9]
Exemple #16
0
def test_nullcheck_3():
    source = ['(null? (cdr (list 1)))']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, []) == True
Exemple #17
0
def test_eval_let():
    exp = ['let', [['x', 3], ['y', 10]], ['+', 'x', 'y']]
    assert eval_in_env(exp, Environment([])) == 13
Exemple #18
0
def test_add():
    source = ['(+ 1 3 5)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == 9
Exemple #19
0
def test_eval_define():
    env = Environment([])
    exp = ['define', 'a', 3]
    eval_in_env(exp, env)
    #assert env._env == [['a', 3]]
    assert env.lookup('a') == 3
Exemple #20
0
def test_and_false():
    source = ['(and (> 2 1) (= 1 2) #t)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == False
Exemple #21
0
def test_eval_closure_2():
    env = Environment([['ifthen', ['closure', ['lambda', ['x'], ['if', True, 'x', 3]], []]]])
    exp = ['ifthen', 10]
    assert eval_in_env(exp, env) == 10
Exemple #22
0
def test_or_false():
    source = ['(or (> 2 10) (= 1 2) #f)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == False
Exemple #23
0
def test_eval_add_const():
    exp = ['+', 3, 4]
    res = eval_in_env(exp, Environment([]))
    assert res == 7
Exemple #24
0
def test_eval_modulo():
    assert 0 == eval_in_env(['modulo', 9, 3], Environment([]))
    assert 1 == eval_in_env(['modulo', 10, 3], Environment([]))
    assert 2 == eval_in_env(['modulo', 11, 3], Environment([]))
Exemple #25
0
def test_eval_anon():
    exp = [['lambda', ['x'], ['+', 'x', 'x']], 7]
    assert eval_in_env(exp, Environment([])) == 14
Exemple #26
0
def test_cdr():
    source = ['(cdr (cons 1 null))']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, []) == []
Exemple #27
0
def test_eval_begin():
    exp = ['begin', ['define', 'a', 0], ['set!', 'a', 10], 'a']
    assert eval_in_env(exp, Environment([])) == 10
Exemple #28
0
def test_nullcheck():
    source = ['(null? null)']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, []) == True
Exemple #29
0
def test_eval_not():
    assert True == eval_in_env(['not', False], Environment([]))
    assert False == eval_in_env(['not', True], Environment([]))
    assert False == eval_in_env(['not', 'null'], Environment([]))
Exemple #30
0
def test_car():
    source = ['(car (cons 1 null))']
    exp = parse_tokens(tokenize(source))[0]
    assert eval_in_env(exp, Environment([])) == 1