Repository navigation
Expand file tree
/
Copy pathobject.py
More file actions
106 lines (73 loc) · 2.15 KB
/
Copy pathobject.py
File metadata and controls
106 lines (73 loc) · 2.15 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
import abc
from typing import List, TYPE_CHECKING
from ast1 import Identifier, BlockStatement
# to avoid circular import
if TYPE_CHECKING:
from environment import Environment
ObjectType = str
INTEGER_OBJ = "NUMBER"
BOOLEAN_OBJ = "TRUTH"
NULL_OBJ = "VOID"
RETURN_VALUE_OBJ = "YIELDED"
ERROR_OBJ = "MISHAP"
FUNCTION_OBJ = "RITUAL"
STRING_OBJ = "SCROLL"
class Object(abc.ABC):
@abc.abstractmethod
def type(self) -> ObjectType:
pass
@abc.abstractmethod
def inspect(self) -> str:
pass
class String(Object):
def __init__(self, value: str):
self.value = value
def type(self) -> ObjectType:
return STRING_OBJ
def inspect(self) -> str:
return self.value
class Integer(Object):
def __init__(self, value: int):
self.value = value
def type(self) -> ObjectType:
return INTEGER_OBJ
def inspect(self) -> str:
return str(self.value)
class Boolean(Object):
def __init__(self, value: bool):
self.value = value
def type(self) -> ObjectType:
return BOOLEAN_OBJ
def inspect(self) -> str:
return "verity" if self.value else "fallacy"
class Null(Object):
def type(self) -> ObjectType:
return NULL_OBJ
def inspect(self) -> str:
return "void"
class ReturnValue(Object):
def __init__(self, value: Object):
self.value = value
def type(self) -> ObjectType:
return RETURN_VALUE_OBJ
def inspect(self) -> str:
return f"yield {self.value.inspect()}"
class Error(Object):
def __init__(self, message: str):
self.message = message
def type(self) -> ObjectType:
return ERROR_OBJ
def inspect(self) -> str:
return f"MISHAP: {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) -> ObjectType:
return FUNCTION_OBJ
def inspect(self) -> str:
params = " knot ".join([p.string() for p in self.parameters])
return f"rune({params}) unfold ... fold"