-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeclarationHandler.py
More file actions
58 lines (48 loc) · 2.02 KB
/
Copy pathDeclarationHandler.py
File metadata and controls
58 lines (48 loc) · 2.02 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
'''
Using this class assumes that the token_stream is a Tokens.ST_DECLARATION
and token_stream has already been parsed and semantically analyzed
'''
import Tokens
import StatusTypes
class DeclarationHandler:
def __init__(self) -> None:
self.status = StatusTypes.STATUS_OK
def handle_declaration(self, token_stream, variables):
variable_type = token_stream[-1][0]
token_stream = token_stream[1:-2]
new_length = len(token_stream)
index = 0
while(index < new_length):
name = token_stream[index][1]
if token_stream[index][0] == Tokens.IDENTIFIER:
if name in variables:
self.status = f'Variable \'{name}\' is already defined.'
if index + 1 < new_length and token_stream[index+1][0] == Tokens.EQUALS:
value = self.set_value_by_type(token_stream[index+2][1], variable_type)
variables[name] = {'value': value, 'type': self.set_variable_by_keyword(variable_type)}
index += 4
else:
variables[name] = {'value': None, 'type': self.set_variable_by_keyword(variable_type)}
index += 2
return variables
def set_value_by_type(self, value, type):
if type == Tokens.KW_STRING or type == Tokens.KW_CHAR:
return value # remove quotes
if type == Tokens.KW_INT:
return int(value)
if type == Tokens.KW_FLOAT:
return float(value)
if type == Tokens.KW_BOOLEAN:
return value == 'TRUE'
def set_variable_by_keyword(self, variable_type):
if variable_type == Tokens.KW_STRING:
return Tokens.STRING
if variable_type == Tokens.KW_CHAR:
return Tokens.CHAR
if variable_type == Tokens.KW_INT:
return Tokens.INT
if variable_type == Tokens.KW_FLOAT:
return Tokens.FLOAT
if variable_type == Tokens.KW_BOOLEAN:
return Tokens.BOOL
return None