-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloxClass.py
29 lines (26 loc) · 888 Bytes
/
loxClass.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
import loxCallable as c
import loxInstance as i
class LoxClass(c.LoxCallable):
def __init__(self, name, superclass, methods):
self.superclass=superclass
self.name=name
self.methods=methods
def __str__(self):
return self.name
def call(self, interpreter, arguments):
instance=i.LoxInstance(self)
initializer=self.findMethod("init")
if initializer is not None:
initializer.bind(instance).call(interpreter, arguments)
return instance
def arity(self):
initializer=self.findMethod("init")
if initializer is None:
return 0
return initializer.arity()
def findMethod(self, name):
if name in self.methods:
return self.methods[name]
if self.superclass is not None:
return self.superclass.findMethod(name)
return None