-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.py
More file actions
39 lines (35 loc) · 1.27 KB
/
token.py
File metadata and controls
39 lines (35 loc) · 1.27 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
class Token:
def __init__(self, type, value = None, items = None):
self.type = type
self.value = value
self.items = items
def getString(self):
if self.value is None and self.items is None:
return f"{self.type}"
elif self.items is None:
return f"{self.type}:{self.value}"
else:
output_string = f"{self.type} with items: "
for item in self.items:
output_string += item.getString() + ", "
return output_string
def getTypeFromToken(token):
if isinstance(token.value, int) or isinstance(token.value, float):
return "NUMBER"
elif isinstance(token.value, str):
return "STRING"
elif token.value is None and token.items is not None:
return "LIST"
def getTypeFromValue(value):
if isinstance(value, int) or isinstance(value, float):
return "NUMBER"
elif isinstance(value, str):
return "STRING"
elif isinstance(value, list):
return "LIST"
def autoMake(value):
type = Token.getTypeFromValue(value)
if type == "NUMBER" or type == "STRING":
return Token(type, value, None)
elif type == "LIST":
return Token(type, None, value)