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_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_set(self): env = {} pyscm.evaluate(['define', 'a', '3'], env) self.assertEqual(3, env['a']) pyscm.evaluate(['set!', 'a', '4'], env) self.assertEqual(4, env['a']) env = {} pyscm.evaluate(['define', 'a', '3'], env) pyscm.evaluate(['define', ['f', 'x'], ['set!', 'a', 'x']], env) pyscm.evaluate(['f', '9'], env) self.assertEqual(9, env['a'])
def test_sequence(self): env = {} pyscm.evaluate(['begin', ['define', 'a', '3'], ['define', 'foo', '"bar"'], ['define', ['func', 'p1', 'p2'], ['+', 'p1', 'p2']]], env) self.assertEqual(3, env['a']) self.assertEqual("bar", env['foo']) l = env['func'] self.assertTrue(isinstance(l, pyscm.Lambda)) self.assertEqual(['p1', 'p2'], l.get_paras()) self.assertEqual([['+', 'p1', 'p2']], l.get_body()) self.assertEqual(env, l.get_parent_env())
def test_equal(self): t = pyscm.make_boolean(True) f = pyscm.make_boolean(False) self.assertTrue(t is pyscm.eq([2, 2])) self.assertTrue(t is pyscm.eq_question_mark([2, 2])) self.assertTrue(f is pyscm.eq_question_mark([2, 2.0])) self.assertTrue(t is pyscm.eq([2, 2.0])) self.assertTrue(t is pyscm.eq_question_mark([t,\ pyscm.make_boolean(True)])) self.assertTrue(t is pyscm.eq_question_mark([ pyscm.make_symbol('foo'), pyscm.make_symbol('foo') ])) self.assertTrue(t is pyscm.eq_question_mark([ pyscm.evaluate(['quote', []], {}), pyscm.evaluate(['quote', []], {}) ]))
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_lambda(self): l = pyscm.evaluate(['lambda', ['a', 'b', 'c'], ['+', 'a', 'b']], {}) self.assertTrue(isinstance(l, pyscm.Lambda)) self.assertEqual(['a', 'b', 'c'], l.get_paras()) self.assertEqual([['+', 'a', 'b']], l.get_body()) self.assertEqual({}, l.get_parent_env())