Example #1
0
 def test_define_complex_fn(self):
     user_env = [] + global_env
     _eval(user_env, [
         _s('define'),
         _s('not'),
         [_s('fn'), [_s('x')], [_s('cond'), [_s('x'), []], [1, 1]]]
     ])
     self.assertEqual(_eval(user_env, [_s('not'), []]), 1)
Example #2
0
 def test_define_fn(self):
     user_env = [] + global_env
     _eval(user_env, [
         _s('define'),
         _s('plus'),
         [_s('fn'), [_s('x'), _s('y')], [_s('+'), _s('x'),
                                         _s('y')]]
     ])
     self.assertEqual(_eval(user_env, [_s('plus'), 1, 3]), 4)
Example #3
0
def load(f, console=False, env=r.global_env):
    buf = ""
    _prompt('lisp>', console)
    for l in f:
        #print("loaded:" + l)
        if len(l) == 0 or l[0] == ';':
            buf = ''
        else:
            buf += l.rstrip('\n')
            if buf.count('(') - buf.count(')') > 0:
                _prompt('    >', console)
                continue

        if buf:
            expr = p.parse(buf)
            if expr is None:
                print('Syntax error in:' + buf)
            else:
                try:
                    result = r._eval(env, expr)
                    _prompt('=>' + str(result) + '\n', console)
                except Exception as e:
                    print(e)

            buf = ''

        _prompt('lisp>', console)
Example #4
0
 def test_fn_quote(self):
     self.assertEqual(
         _eval(global_env, [[
             _s('fn'), [_s('x'), _s('y')],
             [_s('+'), [_s('eval'), _s('x')],
              _s('y')]
         ], [_s('quote'), [_s('+'), 1, 2]], 2]), 5)
Example #5
0
 def test_recursive(self):
     user_env = [] + global_env
     list = [
         _s('define'),
         _s('len'),
         [
             _s('fn'), [_s('x')],
             [
                 _s('cond'), [[_s('eq?'), _s('x'), NIL], 0],
                 [1, [_s('+'), 1, [_s('len'), [_s('cdr'),
                                               _s('x')]]]]
             ]
         ]
     ]
     _eval(user_env, list)
     self.assertEqual(
         _eval(user_env,
               [_s('len'), [_s('quote'), [i for i in range(0, 80)]]]), 80)
Example #6
0
 def test_let_fn(self):
     line = [
         _s('let'),
         [
             _s('plus'),
             [_s('fn'), [_s('x'), _s('y')], [_s('+'),
                                             _s('x'),
                                             _s('y')]]
         ], [_s('plus'), 1, 2]
     ]
     self.assertEqual(_eval(global_env, line), 3)
Example #7
0
 def test_let_car2(self):
     self.assertEqual(
         _eval(global_env, [_s('car'), [_s('quote'), [[1, 2], 3]]]), [1, 2])
Example #8
0
 def test_eq(self):
     self.assertEqual(_eval(global_env, [_s('eq?'), 1, 2]), [])
     self.assertEqual(_eval(global_env, [_s('eq?'), 1, 1]), 1)
Example #9
0
 def test_fn(self):
     self.assertEqual(
         _eval(global_env,
               [[_s('fn'), [_s('x'), _s('y')],
                 [_s('+'), _s('x'), _s('y')]], [_s('+'), 1, 2], 2]), 5)
Example #10
0
 def test_plus(self):
     self.assertEqual(_eval(global_env, [_s('+'), [_s('+'), 1, 2], 3]), 6)
Example #11
0
 def test_set(self):
     user_env = [] + global_env
     _eval(user_env, [_s('define'), _s('a'), 2])
     self.assertEqual(_eval(user_env, _s('a')), 2)
Example #12
0
 def test_quote(self):
     self.assertEqual(_eval(global_env, [_s('quote'), [1, 2]]), [1, 2])
Example #13
0
 def test_eval(self):
     self.assertEqual(_eval(global_env, [_s('eval'), [_s('+'), 1, 2]]), 3)
Example #14
0
 def test_cond(self):
     self.assertEqual(_eval(global_env, [_s('cond'), [[], 1]]), [])
     self.assertEqual(_eval(global_env, [_s('cond'), [[], 2], [1, 1]]), 1)
Example #15
0
 def test_define_setq(self):
     user_env = [] + global_env
     _eval(user_env, [_s('define'), _s('a'), 1])
     _eval(user_env, [_s('setq!'), _s('a'), 2])
     self.assertEqual(_eval(user_env, [_s('+'), 0, _s('a')]), 2)