Пример #1
0
    def test_vector_get_set(self):
        ast = to_ast(expand("""
        (letrec ([v (make-vector 1000 0)]
                 [fill-square-vector (lambda (n)
                                        (if (< n 0)
                                          (vector-ref v 999)
                                          (begin (vector-set! v n (* n n))
                                                 (fill-square-vector (- n 1)))))]
                 [sum-vector (lambda (n s)
                                (if (< n 0)
                                    s
                                    (sum-vector (- n 1) (+ s (vector-ref v n)))))])
         (begin (fill-square-vector 999) (sum-vector 999 0)))
        """))


        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return val.value

        assert interp_w() == sum(i ** 2 for i in range(1000))

        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)
Пример #2
0
def _make_ast(request, wrap=False, stdlib=False):
    from pycket.expand import expand, to_ast, _to_module
    assert request.function.__doc__ is not None
    code = request.function.__doc__
    e = expand(code, wrap, stdlib)
    ast = _to_module(e)
    return ast
Пример #3
0
    def test_vector_get_set(self):
        ast = to_ast(
            expand("""
        (letrec ([v (make-vector 1000 0)]
                 [fill-square-vector (lambda (n)
                                        (if (< n 0)
                                          (vector-ref v 999)
                                          (begin (vector-set! v n (* n n))
                                                 (fill-square-vector (- n 1)))))]
                 [sum-vector (lambda (n s)
                                (if (< n 0)
                                    s
                                    (sum-vector (- n 1) (+ s (vector-ref v n)))))])
         (begin (fill-square-vector 999) (sum-vector 999 0)))
        """))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return val.value

        assert interp_w() == sum(i**2 for i in range(1000))

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Пример #4
0
    def test_anormal_append(self):
        ast = to_ast(
            expand("""
        (let ()
        (define (append-anormal a b)
          (if (null? a)
              b
              (let* ([ca (car a)]
                     [cd (cdr a)]
                     [ap (append-anormal cd b)])
                (cons ca ap))))
         (append-anormal (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6)))
        """))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Object)
            return 1

        assert interp_w() == 1

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Пример #5
0
    def test_side_exit(self):
        ast = to_ast(
            expand("""
        (let ()
            (define (count-positive l sum)
                (if (null? l) sum
                    (if (> (car l) 0)
                        (count-positive (cdr l) (+ (car l) sum))
                        (count-positive (cdr l) sum)
                        )))
            (count-positive (list -1 1 1 1 1 -1 2 3 -5 1 2 2 -5 6 4 3 -5) 0))
        """))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 27

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Пример #6
0
 def test_let_append(self):
     ast = to_ast(expand("""
     (let ()
     (define (let-append a b)
       (let ([tst (null? a)])
           (if tst
               b
               (cons (car a) (let-append (cdr a) b)))))
      (let-append (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6)))
     """
     ))
Пример #7
0
 def test_let_append(self):
     ast = to_ast(
         expand("""
     (let ()
     (define (let-append a b)
       (let ([tst (null? a)])
           (if tst
               b
               (cons (car a) (let-append (cdr a) b)))))
      (let-append (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6)))
     """))
Пример #8
0
    def test_setbang(self):

        ast = to_ast(expand("(let ([n 1000]) (letrec ([countdown (lambda () (if (< n 0) 1 (begin (set! n (- n 1)) (countdown))))]) (countdown)))"))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 1

        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)
Пример #9
0
    def test_countdown_vector_allocation(self):
        ast = to_ast(expand("""
        (letrec ([countdown (lambda (n) (if (< n 0) 1 (let ([v (vector n 1)]) (countdown (- (vector-ref v 0) (vector-ref v 1))))))])
         (countdown 1000))"""))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 1

        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)
Пример #10
0
    def test_countdown_vector_allocation(self):
        ast = to_ast(
            expand("""
        (letrec ([countdown (lambda (n) (if (< n 0) 1 (let ([v (vector n 1)]) (countdown (- (vector-ref v 0) (vector-ref v 1))))))])
         (countdown 1000))"""))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 1

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Пример #11
0
    def test_setbang(self):

        ast = to_ast(
            expand(
                "(let ([n 1000]) (letrec ([countdown (lambda () (if (< n 0) 1 (begin (set! n (- n 1)) (countdown))))]) (countdown)))"
            ))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 1

        self.meta_interp(interp_w, [],
                         listcomp=True,
                         listops=True,
                         backendopt=True)
Пример #12
0
    def test_side_exit(self):
        ast = to_ast(expand("""
        (let ()
            (define (count-positive l sum)
                (if (null? l) sum
                    (if (> (car l) 0)
                        (count-positive (cdr l) (+ (car l) sum))
                        (count-positive (cdr l) sum)
                        )))
            (count-positive (list -1 1 1 1 1 -1 2 3 -5 1 2 2 -5 6 4 3 -5) 0))
        """))


        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Fixnum)
            return ov.value

        assert interp_w() == 27

        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)
Пример #13
0
    def test_anormal_append(self):
        ast = to_ast(expand("""
        (let ()
        (define (append-anormal a b)
          (if (null? a)
              b
              (let* ([ca (car a)]
                     [cd (cdr a)]
                     [ap (append-anormal cd b)])
                (cons ca ap))))
         (append-anormal (list 1 2 3 5 6 6 7 7 8 3 4 5 3 5 4 3 5 3 5 3 3 5 4 3) (list 4 5 6)))
        """
        ))

        def interp_w():
            val = interpret_one(ast)
            ov = check_one_val(val)
            assert isinstance(ov, W_Object)
            return 1

        assert interp_w() == 1

        self.meta_interp(interp_w, [], listcomp=True, listops=True, backendopt=True)