-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj_and_env.py
201 lines (154 loc) · 5.02 KB
/
obj_and_env.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from parser import *
from tokens import *
from abc import ABC, abstractmethod
import hashlib
from typing import Callable
##############################################################
class Environment:
def __init__(self, outer=None):
self.store = {}
self.outer = outer
def get(self, name):
obj = self.store.get(name)
if obj is None and self.outer is not None:
obj = self.outer.get(name)
return obj
def set(self, name, val):
self.store[name] = val
return val
def NewEnclosedEnvironment(outer):
env = Environment()
env.outer = outer
return env
def NewEnvironment():
return Environment()
##############################################################
class ObjectType:
NULL_OBJ = "NULL"
ERROR_OBJ = "ERROR"
INTEGER_OBJ = "INTEGER"
BOOLEAN_OBJ = "BOOLEAN"
STRING_OBJ = "STRING"
RETURN_VALUE_OBJ = "RETURN_VALUE"
FUNCTION_OBJ = "FUNCTION"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
HASH_OBJ = "HASH"
class Object(ABC):
@abstractmethod
def type(self) -> ObjectType:
pass
@abstractmethod
def inspect(self) -> str:
pass
class HashKey():
def __init__(self, type: ObjectType, value: int):
self.Type_ = type
self.Value = value
class Hashable(ABC):
@abstractmethod
def hash_key(self) -> HashKey:
pass
BuiltinFunction = Callable[[List[Object]], Object]
class Integer(Object , Hashable):
def __init__(self, value: int):
self.value = value
def type(self) -> str:
return ObjectType.INTEGER_OBJ
def inspect(self) -> str:
return str(self.value)
def hash_key(self) -> HashKey:
return HashKey(type = self.type(), value = int(self.value))
class Boolean(Object , Hashable):
def __init__(self, value: bool):
self.value = value
def type(self) -> str:
return ObjectType.BOOLEAN_OBJ
def inspect(self) -> str:
return str(self.value).lower()
def hash_key(self) -> HashKey:
valuee = 0
if self.value:
valuee = 1
return HashKey(type = self.type(), value = valuee)
class Null(Object):
def type(self) -> str:
return ObjectType.NULL_OBJ
def inspect(self) -> str:
return None
class ReturnValue(Object):
def __init__(self, value: Object):
self.value = value
def type(self) -> str:
return ObjectType.RETURN_VALUE_OBJ
def inspect(self) -> str:
return self.value.inspect()
class Error(Object):
def __init__(self, message: str):
self.message = message
def type(self) -> str:
return ObjectType.ERROR_OBJ
def inspect(self) -> str:
return "ERROR: " + self.message
class Function(Object):
def __init__(self, parameters : list[Identifier], body : BlockStatement, env : Environment):
self.parameters = parameters
self.body = body
self.env = env
def type(self) -> str:
return ObjectType.FUNCTION_OBJ
def inspect(self) -> str:
params = "[NO PARAMETERS]"
if len(self.parameters) > 0:
params = ""
for i in self.parameters:
params += i.String() + ", "
params = params[:-2]
return f"fn({params}) {{\n{self.body}\n}}"
class String(Object , Hashable):
def __init__(self, value: str):
self.value = value
def type(self) -> str:
return ObjectType.STRING_OBJ
def inspect(self) -> str:
return self.value
def hash_key(self) -> HashKey:
h = hashlib.blake2b(digest_size=8) # 64 bits = 8 bytes.
h.update(self.value.encode('utf-8'))
x = HashKey(type=self.type(), value=int.from_bytes(h.digest(), byteorder='big'))
return x
class Builtin(Object):
def __init__(self, fn : BuiltinFunction):
self.func = fn
def type(self) -> str:
return ObjectType.BUILTIN_OBJ
def inspect(self) -> str:
return "builtin function"
class Array(Object):
def __init__(self, elements : list[Object]):
self.elements = elements
def type(self) -> str:
return ObjectType.ARRAY_OBJ
def inspect(self) -> str:
elements = ""
for i in self.elements:
elements += i.inspect() + ", "
elements = elements[:-2]
return f"[{elements}]"
class HashPair:
def __init__(self, key : Object, value : Object):
self.key = key
self.value = value
class Hash(Object):
def __init__(self, pairs : dict[HashKey, HashPair] , godPairs : dict[HashKey, HashPair] = {}):
self.pairs = pairs
self.godPairs = godPairs
def type(self) -> str:
return ObjectType.HASH_OBJ
def inspect(self) -> str:
pairs = ""
for key in self.pairs:
pairs += f"{key.Value}: {self.pairs[key].value.inspect()}, "
pairs = pairs[:-2]
return f"{{{pairs}}}"
##############################################################