-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallable.py
More file actions
110 lines (91 loc) · 3.39 KB
/
Copy pathcallable.py
File metadata and controls
110 lines (91 loc) · 3.39 KB
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
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Final, List, override
from .ast import expr, stmt
from .environment import Environment
from .exceptions import ReturnException
from .token import Token, TokenType
if TYPE_CHECKING:
from .interpreter import Interpreter
from .lox_class import LoxInstance
class Callable(ABC):
@abstractmethod
def arity(self) -> int:
pass
@abstractmethod
def call(self, interpreter: "Interpreter", args: List[object]) -> object:
pass
@abstractmethod
def name(self) -> str:
pass
class LoxFunction(Callable):
def __init__(
self,
declaration: stmt.Function,
closure: Environment,
is_initializer: bool = False,
) -> None:
self.declaration: Final[stmt.Function] = declaration
self.closure: Final[Environment] = closure
self.is_initializer = is_initializer
@override
def name(self) -> str:
return self.declaration.name.string_repr
def __str__(self) -> str:
return f"<function {self.name()}>"
@override
def arity(self) -> int:
return len(self.declaration.params)
@override
def call(self, interpreter: "Interpreter", args: List[object]) -> object:
environment = Environment(parent=self.closure)
for i in range(len(args)):
environment.declare(self.declaration.params[i])
environment.define(self.declaration.params[i], args[i])
try:
interpreter.execute_multiple_statements(self.declaration.body, environment)
except ReturnException as e:
if self.is_initializer:
tok = Token()
tok.token_type = TokenType.THIS
tok.string_repr = "this"
return self.closure.get_at(0, tok)
return e.value
if self.is_initializer:
tok = Token()
tok.token_type = TokenType.THIS
tok.string_repr = "this"
return self.closure.get_at(0, tok)
return None
def bind(self, instance: "LoxInstance") -> "LoxFunction":
environment = Environment(parent=self.closure)
environment.define("this", instance)
if instance.base_class_instance is not None:
environment.define("super", instance.base_class_instance)
return LoxFunction(
declaration=self.declaration,
closure=environment,
is_initializer=self.is_initializer,
)
class ArrowFunction(Callable):
def __init__(self, declaration: expr.Arrow, closure: Environment) -> None:
self.declaration: Final[expr.Arrow] = declaration
self.closure: Final[Environment] = closure
@override
def name(self) -> str:
return "<arrow function>"
def __str__(self) -> str:
return f"<function {self.name()}>"
@override
def arity(self) -> int:
return len(self.declaration.params)
@override
def call(self, interpreter: "Interpreter", args: List[object]) -> object:
environment = Environment(parent=self.closure)
for i in range(len(args)):
environment.declare(self.declaration.params[i])
environment.define(self.declaration.params[i], args[i])
try:
interpreter.execute_multiple_statements(self.declaration.body, environment)
except ReturnException as e:
return e.value
return None