-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaiman.py
91 lines (77 loc) · 3.24 KB
/
chaiman.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
85
86
87
88
89
# chaiman.py
import logging
from scope.chaivars import *
from scope.chaiflow import *
class ChaiMan:
def __init__(self, parse_tree, global_variables, memory_indexes, next_free_memory_index):
self.parse_tree = parse_tree
self.global_variables = global_variables
self.memory_indexes = memory_indexes
self.next_free_memory_index = next_free_memory_index
def get_memory_location(self, pidentifier):
"""
Returns the location in memory of given pidentifier (variable)
:param pidentifier: variable identifier
:return: memory index
"""
return self.memory_indexes[pidentifier]
def get_pidentifier_assigned_to_mem_index(self, index):
"""
Returns the object assigned to given memory index
:param index: memory index
:return:
"""
for var, mem_index in self.memory_indexes.items():
if mem_index == index:
return var
def get_object_memory_location(self, object):
"""
Returns the location in memory of given object
:param object: object
:return: memory index
"""
# if self.get_variable_assigned_to_value(variable=object):
return self.memory_indexes[object.pidentifier]
# else:
# raise Exception("get_object_memory_location: object '{}' referenced before assignment".format(object))
def get_object_from_memory(self, pidentifier):
"""
Returns the object from memory
:param pidentifier: object identifier
:return: object
"""
return self.global_variables[pidentifier]
def declare_global_variable(self, variable):
"""
Declares global variable in global variables dictionary
:param variable:
:return:
"""
if variable.pidentifier not in self.global_variables.keys():
self.global_variables[variable.pidentifier] = variable
self.memory_indexes[variable.pidentifier] = self.next_free_memory_index
if isinstance(variable, Int):
self.next_free_memory_index += 1
elif isinstance(variable, IntArray):
self.next_free_memory_index += variable.length
else:
raise Exception("Variable with pidentifier {} was already declared".format(variable.pidentifier))
def set_variable_assigned_to_value(self, variable):
"""
Sets status of variable assignment to value to TRUE.
Used for later checks of variable reference before assignment
:param variable: var which has been assigned value
:return:
"""
if variable.pidentifier in self.global_variables.keys():
self.global_variables[variable.pidentifier].set_value_has_been_set()
else:
raise Exception("chaiman set_variable_assigned_to_value: trying to set value to undeclared variable"
" '{}', line {}".format(variable.pidentifier, variable.lineno))
def get_variable_assigned_to_value(self, variable):
"""
Returns the status of variable assignment to value.
:param variable:
:return:
"""
return self.global_variables[variable.pidentifier].get_value_has_been_set_status()