-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.py
51 lines (42 loc) · 1.79 KB
/
interpreter.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
from purescript.corefn.expression import Expression
from purescript.prim import prim
class Interpreter(object):
def __init__(self, _load_foreign, _load_module):
self.__load_foreign = _load_foreign
self.__load_module = _load_module
self.loaded_modules = {}
self.loaded_foreign_modules = {}
def get_or_load_module(self, module):
if module not in self.loaded_modules:
self.loaded_modules[module] = self.__load_module(module)
return self.loaded_modules[module]
def get_or_load_foreign_module(self, module):
if module not in self.loaded_foreign_modules:
self.loaded_foreign_modules[module] = self.__load_foreign(module)
return self.loaded_foreign_modules[module]
def run_main(self, module):
"""
:type module: Module
"""
decl = module.decl("main") # type: Expression
main = decl.expression.eval(self, {})
main.run_effect(self)
def run_module_by_name(self, module_name):
self.run_main(self.get_or_load_module(module_name))
def expression(self, expression, frame):
return expression.eval(self, frame)
def load_decl(self, module_name, identifier):
if module_name in prim:
if identifier in prim[module_name]:
return prim[module_name][identifier]
else:
raise NotImplementedError
else:
module = self.get_or_load_module(module_name)
if module.has_decl(identifier):
decl = module.decl(identifier)
return decl.expression.eval(self, {})
else:
module = self.get_or_load_foreign_module(module_name)
decl = module.decl(identifier)
return decl.expression.eval(self, {})