-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringExpression.py
More file actions
64 lines (52 loc) · 2.07 KB
/
Copy pathStringExpression.py
File metadata and controls
64 lines (52 loc) · 2.07 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
import Tokens
import StatusTypes
class StringExpression:
def __init__(self) -> None:
self.status = StatusTypes.STATUS_OK
self.error_string = None
def evaluate(self, token_stream, variables):
result = str()
for token_type, token in token_stream:
if token_type == Tokens.CONCATENATOR:
continue
if token_type == Tokens.IDENTIFIER:
temp = variables[token]['value']
if (type := variables[token]['type']) in (Tokens.STRING, Tokens.CHAR):
# [1:-1] --> removes single/double quotes
string_value = temp[1:-1]
elif type in (Tokens.INT, Tokens.FLOAT):
string_value = str(temp)
else: # if BOOL_TRUE or BOOL FALSE
string_value = 'TRUE' if temp else 'FALSE'
elif token_type == Tokens.STRING:
string_value = self.process_string(token[1:-1])
elif token_type in (Tokens.BOOL, Tokens.BOOL_TRUE, Tokens.BOOL_FALSE):
string_value = token
else: # if Tokens.CHAR
string_value = token[1] if token[1] != '#' else '\n'
result += string_value
return f'"{result}"'
def process_string(self, string):
processed = str()
length = len(string)
i = 0
while i < length:
if string[i] == '[':
if i + 2 < length and string[i+2] == ']':
processed += string[i+1]
i += 3
else:
self.status = StatusTypes.STATUS_MISSING_CLOSE_BRAC
self.error_string = f'"{string}"'
break
elif string[i] == ']':
self.status = StatusTypes.STATUS_MISSING_OPEN_BRAC
self.error_string = f'"{string}"'
break
elif string[i] == '#':
processed += '\n'
i += 1
else:
processed += string[i]
i += 1
return processed