Пример #1
0
 def test_program(self):
     self.assertEqual(parse("""
     (def (f x)
      (* x x))
     (f 12)
     (print '"hello world")
     """), List(
         List(Symbol('def'), List(Symbol('f'), Symbol('x')),
              List(Symbol('*'), Symbol('x'), Symbol('x'))),
         List(Symbol('f'), Number(12)),
         List(Symbol('print'),
              List(Symbol('quote'), String("hello world")))))
Пример #2
0
 def test_string(self):
     self.assertEqual(parse('""'), List(
         String("")))
     self.assertEqual(parse(r'"\""'), List(
         String("\"")))
     self.assertEqual(parse('"\\"" one'), List(
         String('"'),
         Symbol("one")))
     self.assertEqual(parse(r'"hello world"'), List(
         String("hello world")))
     self.assertEqual(parse(r'"hello \"world\""'), List(
         String("hello \"world\"")))
     self.assertEqual(parse(r'"hello" "world"'), List(
         String("hello"), String("world")))
Пример #3
0
 def evaluate(self, code):
     i = Interpreter()
     for e in parse(code):
         i.evaluate(e)
     return i
Пример #4
0
 def test_comment(self):
     self.assertEqual(parse("one ; 1 2\ntwo"), List(
         Symbol("one"),
         Symbol("two")))
Пример #5
0
 def test_identifier(self):
     self.assertEqual(parse("hello world*"), List(
         Symbol("hello"),
         Symbol("world*")))
Пример #6
0
 def test_quote(self):
     self.assertEqual(parse("'(12\n3)"), List(
         List(Symbol('quote'), List(Number(12), Number(3)))))
Пример #7
0
 def test_sexpr(self):
     self.assertEqual(parse("(12 3) ()"), List(
         List(Number(12), Number(3)),
         List()))
Пример #8
0
 def test_exprs(self):
     self.assertEqual(parse("12 3"), List(
         Number(12),
         Number(3)))
Пример #9
0
 def test_number(self):
     self.assertEqual(parse("1"), List(Number(1)))
     self.assertEqual(parse("1234"), List(Number(1234)))
     self.assertEqual(parse("-1"), List(Number(-1)))
     self.assertEqual(parse("1.0"), List(Number(1.0)))
     self.assertEqual(parse("-1.0"), List(Number(-1.0)))