-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_valuev4.py
84 lines (65 loc) · 1.79 KB
/
type_valuev4.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
# enumerated type for our different language data types
class Type:
INT = "int"
BOOL = "bool"
STRING = "string"
NIL = "nil"
# represents a value, which has a type and its value
class Value:
def __init__(self, type, value=None):
self.t = type
self.v = value
def value(self):
return self.v
def type(self):
return self.t
def print(self):
return f"{self.v}"
class LazyValue:
def __init__(self, ast, env):
self.eval = False
self.v = None # Value
self.a = ast
self.e = env
def evaluated(self):
return self.eval
def set_eval(self):
self.eval = True
def value(self):
return self.v
def set_value(self, value):
self.v = value
def ast(self):
return self.a
def env(self):
return self.e
def print(self):
value = None
type = None
if self.v:
value = self.v.value()
type = self.v.type()
return f"eval: {self.eval} | value: {value} | type: {type} | ast: {self.a} | env: {self.e.print()}"
# creates a value based on the given value
def create_value(val):
if val is True:
return Value(Type.BOOL, True)
if val is False:
return Value(Type.BOOL, False)
elif isinstance(val, str):
return Value(Type.STRING, val)
elif isinstance(val, int):
return Value(Type.INT, val)
else:
raise ValueError("Unknown value type")
# outputs the printable version of the value
def get_printable(val):
if val.type() == Type.INT:
return str(val.value())
if val.type() == Type.STRING:
return val.value()
if val.type() == Type.BOOL:
if val.value() is True:
return "true"
return "false"
return None