-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtype_valuev1.py
45 lines (36 loc) · 1.04 KB
/
type_valuev1.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
from intbase import InterpreterBase
# Enumerated type for our different language data types
class Type:
INT = "int"
BOOL = "bool"
STRING = "string"
# 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 create_value(val):
if val == InterpreterBase.TRUE_DEF:
return Value(Type.BOOL, True)
elif val == InterpreterBase.FALSE_DEF:
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")
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