Пример #1
0
def test_macro():
    e = Env()
    p = parse('(def t (macro (x) (+ x x)))')
    c = parse('(t (+ 1 2))')
    eval_lisp(p, e)
    assert eval_lisp(c, e) == 6

    print('macro passed')
Пример #2
0
def input_test():
    e = Env()

    inp1 = parse('(1 2)')
    assert show(inp1) == '(1 2 ())'

    inp2 = parse('(+ 1 2)')
    assert show(inp2) == '(+ 1 2 ())'
    assert eval_lisp(inp2, e) == 3

    inp3 = parse('(1 2 3 4)')
    assert show(inp3) == '(1 2 3 4 ())'

    inp4 = parse('(+ (+ 3 2) 3)')
    assert eval_lisp(inp4, e) == 8
    assert show(inp4) == '(+ (+ 3 2 ()) 3 ())'

    inp5 = parse('(1 (4 5))')
    assert show(inp5) == '(1 (4 5 ()) ())'

    eval_lisp(parse('(def a 10)'), e)
    assert eval_lisp(parse('(+ a 3)'), e) == 13

    eval_lisp(parse('(def b (lambda (x) (* x x)))'), e)
    assert eval_lisp(parse('(b 3)'), e) == 9

    print('input tests passed')
Пример #3
0
def test_fib():
    e = Env()
    fib = parse('(def fib (lambda (n) ('
                'cond ((== n 1) 1)'
                ' ((== n 0) 0)'
                ' (else (+ (fib (- n 1)) (fib (- n 2)))))'
                '))')
    assert show(
        fib) == '(def fib (lambda (n ()) (cond ((== n 1 ()) (1 ())) ((== n 0 ()) (0 ())) (else (+ (fib (- n 1 ()) ()) (fib (- n 2 ()) ()) ()) ()) ()) ()) ())'
    eval_lisp(fib, e)
    exec_fib = parse('(fib 10)')
    assert eval_lisp(exec_fib, e) == 55
Пример #4
0
def test_quote():
    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) `g)'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    print('quote passed')
Пример #5
0
def test_high_order_func():
    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g () (+ 3 x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 1))')
    assert eval_lisp(c, e) == 4

    print('high order passed')
Пример #6
0
def test_simple_coms():
    e = Env()

    emp = parse('(defn empty? (x) (and (== (car x) None) (== (cdr x) None)))')
    f = parse('(empty? (` (1 2)))')
    t = parse('(empty? ())')
    qua = parse(
        '(defn qua (x)('
        'if (empty? x)'
        '()'
        '(cons (* 2 (car x)) (qua (cdr x)))'
        '))'
    )
    q = parse('(qua (qua (` (1 3))))')
    eval_lisp(emp, e)
    eval_lisp(qua, e)
    assert not eval_lisp(f, e)
    assert eval_lisp(t, e)
    assert show(eval_lisp(q, e)) == '(4 12 ())'

    print('simple tests passed')
Пример #7
0
def test_cond():
    e = Env()
    p = parse('(cond ((< 3 1) 1) ((< 2 3) 2))')
    assert eval_lisp(p, e) == 2

    print('cond test passed')
Пример #8
0
def test_if():
    assert eval_lisp(parse('(if (< 3 5) (12) (22))'), Env()) == 12