def test_apply(self): code = """ (define (sum lst) (if (eq? lst '()) 0 (+ (car lst) (sum (cdr lst))))) (define (map f lst) (if (eq? lst '()) '() (cons (f (car lst)) (map f (cdr lst))))) (sum (map car '((1 2 3) (4 5 6)))) """ ast = pyscm.parse(code) self.assertEqual(5, pyscm.evaluate(ast, pyscm.make_base())) code = """ ((lambda (x y) (+ x y)) 1 9) """ ast = pyscm.parse(code) self.assertEqual(10, pyscm.evaluate(ast, pyscm.make_base())) code = """ (define (func x y) (let ((z 3) (w 7)) (+ x w y z))) (func 9 9) """ ast = pyscm.parse(code) self.assertEqual(28, pyscm.evaluate(ast, pyscm.make_base()))
def test_single_list(self): self.assertEqual(['begin', ['+', '1', '2']], pyscm.parse('(+ 1 2)')) self.assertEqual(['begin', ['number?', 'x']], pyscm.parse('(number? x)')) self.assertEqual(['begin', ['lambda', ['x'], ['+', 'x', '3']]], pyscm.parse('(lambda (x) (+ x 3))')) self.assertEqual(['begin', ['define', 'a', '5']], pyscm.parse('(define a 5)')) self.assertEqual(['begin', ['if', 'a', '#f', '1', '3']], pyscm.parse('(if a #f 1 3)'))
def test_if(self): t = pyscm.make_boolean(True) f = pyscm.make_boolean(False) ast = pyscm.parse('(if (> 2 1) 9 10)')[1] self.assertEqual(9, pyscm.evaluate(ast, pyscm.make_base())) ast = pyscm.parse('(if (> 1 2) 9 10)')[1] self.assertEqual(10, pyscm.evaluate(ast, pyscm.make_base())) ast = pyscm.parse('(if (> 2 3) 10)')[1] self.assertEqual(None, pyscm.evaluate(ast, pyscm.make_base())) ast = pyscm.parse('(if (< 2 3) 10)')[1] self.assertEqual(10, pyscm.evaluate(ast, pyscm.make_base()))
def test_function(self): code = """ (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) false))""" ast = ['begin', ['define', ['tagged-list?', 'exp', 'tag'], ['if', ['pair?', 'exp'], ['eq?', ['car', 'exp'], 'tag'], 'false']]] self.assertEqual(ast, pyscm.parse(code))
def test_cond(self): t = pyscm.make_boolean(True) f = pyscm.make_boolean(False) code = """ (define (f x) (cond ((< x 3) 1) ((< x 5) 44) (else 99) )) (define a (f 2)) (define b (f 4)) (define c (f 33)) """ ast = pyscm.parse(code) base = pyscm.make_base() pyscm.evaluate(ast, base) self.assertEqual(1, base['a']) self.assertEqual(44, base['b']) self.assertEqual(99, base['c'])
def test_data_type(self): self.assertEqual(['begin', ['"hello"', '"world"', '#f', '#t', '1e4']], pyscm.parse('("hello" "world" #f #t 1e4)'))
def test_quote(self): self.assertEqual(['begin', ['quote', ['1', '2']]], pyscm.parse("'(1 2)")) self.assertEqual(['begin', ['quote', ['define', 'a', '1']]], pyscm.parse("'(define a 1)")) self.assertEqual(['begin', ['quote', 'a']], pyscm.parse("'a"))
def test_sequence_and_nested(self): self.assertEqual(['begin', ['+', ['a', '1', '2'], '4'], ['*', '1', '2']], pyscm.parse('(+ (a 1 2) 4) (* 1 2)'))
def test_sequence(self): self.assertEqual(['begin', ['+', '3', '4'], ['*', '1', '2']], pyscm.parse('(+ 3 4) (* 1 2)')) self.assertEqual(['begin', ['+', '3', '4'], ['*', '1', '2']], pyscm.parse('(+ 3 4) \n\n(* 1 2)'))
def test_let_to_lambda(self): ast = pyscm.parse('(let ((x 3) (y 4)) (+ x y))')[1] self.assertEqual( [['lambda', ['x', 'y'], ['+', 'x', 'y']], '3', '4'], pyscm.let_to_lambda(ast) )
def test_nested_list(self): self.assertEqual(['begin', ['+', ['*', 'a', 'b'], '4']], pyscm.parse('(+ (* a b) 4)')) self.assertEqual(['begin', ['+', ['*', 'a', ['/', '12.3', '4.3']], '4']], pyscm.parse('(+ (* a (/ 12.3 4.3)) 4)'))