-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyze.py
137 lines (110 loc) · 4.96 KB
/
Analyze.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from Scope import Scope
from Lexitize import Lexitize
from Treeitize import Treeitize
import copy
class Analyze(object):
def __init__(self):
self.scope = Scope()
def analyze(self, tree):
def simplifyObject(objectString):
if isinstance(objectString, basestring):
objectString = "'{}'".format(objectString)
objLex = Lexitize().lexitize(str(objectString))[0]
tree = Treeitize().treeitize(objLex)[0]
return tree
def getTopObjectAndParameter(objectVariable):
return (objectVariable.popleft()['value'],
objectVariable.popleft()['value'])
def analyzeObject(objectVariable):
topValue, parVal = getTopObjectAndParameter(objectVariable)
if topValue in ['number', 'boolean', 'string', None]:
return parVal
elif topValue == 'list':
newList = []
while parVal:
newList.append(self.analyze(parVal.popLeftObject()))
return newList
def analyzeVariable(objectVariable):
variable, variableValue = getTopObjectAndParameter(objectVariable)
if variableValue == None:
variableTree = self.scope.get(variable)
return self.analyze(variableTree)
else:
simplifiedVariable = simplifyObject(self.analyze(variableValue))
self.scope.add(variable, simplifiedVariable)
def analyzeOpComp(objectVariable):
operator, values = getTopObjectAndParameter(objectVariable)
val1 = self.analyze(values.popLeftObject())
val2 = self.analyze(values.popLeftObject())
if isinstance(val1, basestring):
val1 = "'{}'".format(val1)
if isinstance(val2, basestring):
val2 = "'{}'".format(val2)
return eval(str(val1) + operator + str(val2))
def analyzeConditional(objectVariable):
#conditional currently unused, but needed if eventually diff cond types
conditional, parVal = getTopObjectAndParameter(objectVariable)
values = parVal.pop()
ifValueTree = values.popleft()
elseValueTree = values.popleft()
comparisonValue= analyzeOpComp(parVal)
if comparisonValue:
executeConditionStructure(ifValueTree)
#return executeConditionStructure(ifValueTree)
elif elseValueTree:
executeConditionStructure(elseValueTree)
#return executeConditionStructure(elseValueTree)
else: return
def analyzeLoop(objectVariable):
loop, parVal = getTopObjectAndParameter(objectVariable)
if loop == 'while':
analyzeWhileLoop(parVal)
elif loop == 'for':
analyzeForLoop(parVal)
#***PRINTING HERE INSTEAD OF THNAKE
def analyzeWhileLoop(parVal):
loopCondition = parVal.popLeftObject()
while self.analyze(copy.deepcopy(loopCondition)):
executeConditionStructure(parVal)
def analyzeForLoop(parVal):
loopVar = parVal.popLeftObject().popleft()['value']
loopList = parVal.popLeftObject()
loopList = self.analyze(loopList)
loopList = simplifyObject(loopList).pop()['value']
variableExists = False
if self.scope.get(loopVar):
variableExists = True
while loopList:
currentVarIteration = loopList.popLeftObject()
self.scope.add(loopVar, currentVarIteration)
executeConditionStructure(parVal)
if not variableExists:
self.scope.pop(loopVar)
def executeConditionStructure(parVal):
parVal = copy.deepcopy(parVal)
while parVal:
result = self.analyze(parVal.popLeftObject())
if result is not None:
print result
return result
def analyzeAttribFunc(objectVariable):
attribFunc, parVal = getTopObjectAndParameter(objectVariable)
functionParameterTree = parVal.popleft()
functionObjectTree = parVal.pop()
functionParameter = self.analyze(functionParameterTree)
functionObject = self.analyze(functionObjectTree)
return eval(str(functionObject) + '['+str(functionParameter)+']')
typeAnalysis = {
'arithmetic': analyzeOpComp,
'comparison': analyzeOpComp,
'object': analyzeObject,
'variable': analyzeVariable,
'conditional': analyzeConditional,
'loop': analyzeLoop,
'attribFunc': analyzeAttribFunc,
}
result = None
currentObject = tree.popLeftObject()
currentType = currentObject[0]['type']
if currentType in typeAnalysis:
return typeAnalysis[currentType](currentObject)