Ejemplo n.º 1
0
def test_function_defcall_count(env):
    func_prog = textwrap.dedent("""\
        (begin
            (define count
                (lambda (item L)
                    (if L
                        (+ (equal? item (car L)) (count item (cdr L)))
                        0)))
            (count 0 (list 0 1 2 3 0 0)))
        """)
    assert yalispy.eval(yalispy.parse(func_prog)) == 3
Ejemplo n.º 2
0
def test_function_defcall_account(env):
    func_prog = textwrap.dedent("""\
        (begin
            (define make-account
                (lambda (balance)
                    (lambda (amt)
                        (begin (set! balance (+ balance amt))
                               balance))))
            (define account1 (make-account 100.00))
            (account1 -20.00))
        """)
    assert yalispy.eval(yalispy.parse(func_prog)) == 80
Ejemplo n.º 3
0
def test_function_defcall_circle(env):
    func_prog = "(define circle-area (lambda (r) (* pi (* r r))))"
    call_prog = "(circle-area 10)"
    assert yalispy.eval(yalispy.parse(func_prog), env) is None
    assert yalispy.eval(yalispy.parse(call_prog), env) == 314.1592653589793
Ejemplo n.º 4
0
def test_to_string(program):
    assert yalispy.to_string(yalispy.parse(program)) == program
Ejemplo n.º 5
0
def test_eval_definition(env):
    assert "foo" not in env
    assert yalispy.eval(yalispy.parse("(define foo (+ 1 2))"), env) is None
    assert env["foo"] == 3
Ejemplo n.º 6
0
def test_eval_conditional():
    assert yalispy.eval(yalispy.parse("(if (> 1 0) (+ 1 1) (- 1 1))")) == 2
    assert yalispy.eval(yalispy.parse("(if (< 1 0) (+ 1 1) (- 1 1))")) == 0
Ejemplo n.º 7
0
def test_eval_quote_str():
    assert yalispy.to_string(yalispy.eval(
        yalispy.parse('(quote (* 3 r))'))) == '(* 3 r)'
Ejemplo n.º 8
0
def test_parse():
    program = "(begin (define r 10) (* pi (* r r)))"
    ast = ["begin", ["define", "r", 10], ["*", "pi", ["*", "r", "r"]]]
    assert yalispy.parse(program) == ast
Ejemplo n.º 9
0
def test_function_defcall_fact(env):
    func_prog = "(define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))"
    assert yalispy.eval(yalispy.parse(func_prog), env) is None
    assert yalispy.eval(yalispy.parse("(fact 10)"), env) == 3628800