-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisptypes.py
76 lines (51 loc) · 1.54 KB
/
lisptypes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
from decimal import Decimal as decimal
class Expression:
pass
# Self-evaluating data types:
class String(str, Expression):
def __repr__(self):
return f"<String: '{str(self)}'>"
class Symbol(str, Expression):
def __repr__(self):
return f"<Symbol: '{str(self)}'>"
def __eq__(self, other):
return type(other) == Symbol and str(self) == str(other)
class Number(Expression):
def __new__(cls, val):
if type(val) == float:
return Float(val)
elif type(val) == int:
return Integer(val)
def __repr__(self):
return f"<Number: {str(self)}>"
class Float(float, Number):
pass
class Integer(int, Number):
pass
# compound data types
class List(list, Expression):
def __str__(self):
if len(self) > 0:
if type(self[0]) == Symbol and self[0] == 'quote':
return f'\'{ str(self[1]) }'
return f'({ " ".join(str(i) for i in self) })'
else:
return '()'
def __repr__(self):
return f'<List: {str(self)}>'
class Pair(Expression):
def __init__(self, car, cdr):
self.car = car
self.cdr = cdr
def __repr__(self):
return f'<Pair: {repr(self.car)} . {repr(self.cdr)}>'
class Procedure(Expression):
def __init__(self, arguments, body):
self.arguments = arguments
self.body = body
class BuiltinProcedure(Expression):
def __init__(self, f):
self.f = f
def call(self, *args):
return self.f(*args)