Beispiel #1
0
def test_or():
    expression = rd.parse("(or #t #f)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(or #t #t #t)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(or #f #f #f)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
Beispiel #2
0
def test_iota():
    define_iota = rd.parse("""(define iota (lambda (start end step) (begin (define helper (lambda (cur list) (if (= cur start) list (helper (- cur step) (cons cur list))))) (helper end '()))))""")
    define_test = rd.parse("(iota 10 1 -1)")

    ev.evaluate(define_iota)

    assert_equal(str(ev.evaluate(define_test)), "(9 8 7 6 5 4 3 2 1)")
Beispiel #3
0
def test_equals():
    expression = rd.parse("(= 3 3)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(= 3 1)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
    expression = rd.parse("(= 3 3 3)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(= 3 1 1)")
    assert_equals(ev.evaluate(expression), SchemeFalse())
Beispiel #4
0
def test_less():
    expression = rd.parse("(< 1 10)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(< 1 2 3 4 5 10)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(< 1 2 3 4 4 5 10)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
    expression = rd.parse("(< 1 2 3 4 3 1)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
Beispiel #5
0
def test_greater():
    expression = rd.parse("(> 10 1)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(> 10 5 4 3 2 1)")
    assert_equal(ev.evaluate(expression), SchemeTrue())
    expression = rd.parse("(> 10 5 4 4 2 1)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
    expression = rd.parse("(> 10 5 4 5 2 1)")
    assert_equal(ev.evaluate(expression), SchemeFalse())
Beispiel #6
0
def test_set_test():
    define_singleton_set = rd.parse("(define singletonSet (lambda (x) (lambda (y) (= y x))))")
    define_contains = rd.parse("(define contains (lambda (set_ y) (set_ y)))")
    define_test_sets = rd.parse("""
        (define s1 (singletonSet 1))
        (define s2 (singletonSet 2))
        (define s3 (lambda (x) (and (>= x 5) (<= x 15))))
        (define s4 (lambda (x) (and (<= x -5) (>= x -15))))
         """)

    test_1 = rd.parse("(contains s1 1)")
    test_2 = rd.parse("(contains s2 2)")
    test_3 = rd.parse("(contains s3 5)")
    test_4 = rd.parse("(contains s4 -5)")
    test_5 = rd.parse("(contains s4 -22)")

    ev.evaluate(define_singleton_set)
    ev.evaluate(define_contains)
    ev.evaluate(define_test_sets)

    assert_equal(ev.evaluate(test_1), SchemeTrue())
    assert_equal(ev.evaluate(test_2), SchemeTrue())
    assert_equal(ev.evaluate(test_3), SchemeTrue())
    assert_equal(ev.evaluate(test_4), SchemeTrue())
    assert_equal(ev.evaluate(test_5), SchemeFalse())
Beispiel #7
0
def direct():
    statement = request.args.get('statement')
    try:
        expression = rd.parse(statement)
        res = ev.evaluate(expression)
    except Exception as e:
        res = e
    return jsonify(result= str(res))
Beispiel #8
0
def test_list_no_exec():
    define_list = rd.parse("""
    (begin
            (define baz (lambda () (cons 1 2)))
            ((baz))
    )
    """)
    assert_raises(SchemeException, ev.evaluate, define_list)
Beispiel #9
0
def test_list_eval():
    define_list = rd.parse("""
    (begin (define foo (lambda () 4))
            (define bar (lambda () foo))

            ((bar)))
    """)
    assert_equal(ev.evaluate(define_list), SchemeNumber(4))
Beispiel #10
0
def evaluate():
    global env
    statement = request.args.get('statement')
    ev = imp.reload(schemepy.evaluator)
    env = ev.get_root_environment()
    try:
        start = time.time()
        expression = rd.parse(statement)
        current_app.logger.info(expression)
        res = ev.evaluate(expression, env)
        end = time.time() - start
        current_app.logger.info(end)
        res=  str(res)
    except Exception as e:
        expression = rd.parse(statement)
        res = e
        res=  str(res)
    return jsonify(result=res)
Beispiel #11
0
def evaluate():
    statement = request.args.get('statement')
    ev = imp.reload(schemepy.evaluator)
    try:
        expression = rd.parse(statement)
        res = ev.evaluate(expression)
    except Exception as e:
        res = e
    return jsonify(result= str(res))
Beispiel #12
0
def direct():
    statement = request.args.get('statement')
    try:
        expression = rd.parse(statement)
        res = ev.evaluate(expression, env)
        res = str(res)
        current_app.logger.info(res)
    except Exception as e:
        res = e
    return jsonify(result=res)
Beispiel #13
0
def test_multiline_expression_lambda():
    define_test =rd.parse("""
        ((lambda (x)
            (+ ((lambda ()
                (define x 1)
                x
            )) x)
        ) 2)
""")
    assert_equal(ev.evaluate(define_test), SchemeNumber(3))
Beispiel #14
0
def test_Y_combinator():
    define_y = rd.parse("""

            (define Y
             (lambda (f)
             ((lambda (x) (x x))
             (lambda (g)
             (f (lambda (x) ((g g) x)))))))
               (define fac
                 (Y
                   (lambda (f)
                     (lambda (x)
                       (if (< x 2)
                           1
                           (* x (f (- x 1))))))))
""")
    define_test = rd.parse("(fac 6)")
    ev.evaluate(define_y)

    assert_equal(ev.evaluate(define_test), SchemeNumber(720))
Beispiel #15
0
def test_divide():
    expression = rd.parse("(/ 100 10 5)")
    assert_equal(ev.evaluate(expression), SchemeNumber(2))
Beispiel #16
0
def test_times():
    expression = rd.parse("(* 5 2 3)")
    assert_equal(ev.evaluate(expression), SchemeNumber(30))
Beispiel #17
0
def test_minus():
    expression = rd.parse("(- 5 2 3)")
    assert_equal(ev.evaluate(expression), SchemeNumber(0))
Beispiel #18
0
def test_plus():
    expression = rd.parse("(+ 1 2 3)")
    assert_equal(ev.evaluate(expression), SchemeNumber(6))
Beispiel #19
0
def test_enviornments():
    expression = rd.parse("(begin (define b 3) (define fun (lambda (z) b)) (fun 1))")
    assert_equal(ev.evaluate(expression), SchemeNumber(3))
Beispiel #20
0
def test_quote():
    expression = rd.parse(''''(1 2 ;3)
        5 ; (2
        4)''')
    assert_equal(str(ev.evaluate(expression)),'(1 2 5 4)')
Beispiel #21
0
def test_lambda_no_params():
    expression = rd.parse("(begin (define func (lambda () (+ 1 1))) (func)) ")
    assert_equal(ev.evaluate(expression), SchemeNumber(2))
Beispiel #22
0
def test_lambda_exception():
    expression = rd.parse("(lambda (x y z) )")
    assert_raises(SchemeException, ev.evaluate, expression)
Beispiel #23
0
def test_lamba():
    expression = rd.parse("(begin (define z (lambda (x y) (+ x y))) (z 1 2))")
    assert_equal(ev.evaluate(expression), SchemeNumber(3))
Beispiel #24
0
def test_define_exception():
    expression = rd.parse("(define x)")
    assert_raises(SchemeException, ev.evaluate, expression)
Beispiel #25
0
def test_advanced_begin():
    expression = rd.parse("(begin (begin (define b 2)) b)")
    assert_raises(SchemeException, ev.evaluate, expression)

    expr = rd.parse("(begin (define b 1) (begin (define b 2)) b)")
    assert_equal(ev.evaluate(expr), SchemeNumber(1))
Beispiel #26
0
def test_modulo():
    expression = rd.parse("(% 10 9)")
    assert_equal(ev.evaluate(expression), SchemeNumber(1))
Beispiel #27
0
def test_higherorder_functions():
    expression = rd.parse("(begin (define scons (lambda (x y) (lambda (m) (m x y)))) (define scar (lambda (z) (z (lambda (p q) p)))) (scar (scons 10 11)))")
    assert_equal(ev.evaluate(expression), SchemeNumber(10))
Beispiel #28
0
def test_multi_line_evaluation():
    expression = rd.parse("(begin (+ 1 1) (+ 2 2))")
    assert_equal(ev.evaluate(expression), SchemeNumber(4))
Beispiel #29
0
def test_lambda_shorthand():
    expression = rd.parse("(define (add a b) (+ a b)) (add 2 3)")
    assert_equal(ev.evaluate(expression), SchemeNumber(5))
Beispiel #30
0
def test_wrong_number_of_args():
    exp = rd.parse("(define (test x) x) (test 1 2)")
    assert_raises(SchemeException, ev.evaluate, exp)
Beispiel #31
0
def test_variable_defines():
    expression = rd.parse("(begin (define x 5) x)")
    assert_equal(ev.evaluate(expression).value, 5)