本文整理汇总了Python中State.State.env_createLocal方法的典型用法代码示例。如果您正苦于以下问题:Python State.env_createLocal方法的具体用法?Python State.env_createLocal怎么用?Python State.env_createLocal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类State.State
的用法示例。
在下文中一共展示了State.env_createLocal方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _analyseExpression
# 需要导入模块: from State import State [as 别名]
# 或者: from State.State import env_createLocal [as 别名]
def _analyseExpression(self, astNumber, nodeNumber, state, recursionDepth):
''' Analyses a single expression node.
In contrast to statements, expressions always have a return value.
Expressions are the more interesting and difficult part since only
here instructions are really executed and histories get written.
Input:
astNumber: The ast the statement belongs to
nodeNumber: The node in the AST to analyse
state: The state of the current execution (contains environment, heap, objects)
Return:
hist: The HistoryCollection for this node
ret: List of accessors. These two cases have to be handled separatly.
The parent statement/expression has to handle what to do with it. This is
necessary to realize assignment statements properly.
For variable list has one member. For MemberExpressions two.
'''
### Get Statement and handle each type
ast = self.asts[astNumber]
curNode =ast[nodeNumber]
t = curNode["type"]
### Identifier handled together with expression
if t in ["Identifier", "Property"]:
# Special case for holes
if curNode["value"] == "_HOLE_":
return HistoryCollection(), ["_HOLE_"]
# For realizing the histories for specific nodes: Store to which object this node belongs to
object = state.env_get(curNode["value"])
if object != None:
self.nodeToObject[astNumber][nodeNumber] = object
# Besides that return the identifier
return HistoryCollection(), [curNode["value"]]
### Expressions
elif t == "ThisExpression":
# the this is only a special local variable
return HistoryCollection(), state.context
elif t == "ArrayExpression":
# Handle as object (returning it as reference makes sense. Since also internally. Creat object and then return reference.)
newObj = state.newObject("array", astNumber, nodeNumber)
return HistoryCollection(), newObj
elif t == "ArrayAccess":
hist, leftRet = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
rightHist, rightRet = self._analyseExpression(astNumber, curNode["children"][1], state, recursionDepth)
hist.addHistoryCollection(rightHist)
return hist, [state.getTarget(leftRet), state.getTarget(rightRet)]
elif t == "ObjectExpression":
obj = state.newObject("ObjectExpression", astNumber, nodeNumber)
hist = HistoryCollection()
if "children" in curNode:
for child in curNode["children"]:
prop = ast[child]
tmpHist, propertyValue = self._analyseExpression(astNumber, prop["children"][0], state, recursionDepth)
hist.addHistoryCollection(tmpHist)
state.heap_add(obj, prop["value"], state.getTarget(propertyValue))
return hist, obj
elif t == "FunctionExpression":
# Execute isolated and store in the separate history.
# Since I remember the ObjectName, afterwards the longest history per object can be taken
isolatedExpressionState = State()
for i, param in enumerate(curNode["children"][:-1]): # Only parameters and last body
parameterName = ast[curNode["children"][i]]["value"]
isolatedExpressionState.env_createLocal(parameterName)
fnHist = self._analyseStatement(astNumber, curNode["children"][-1], isolatedExpressionState, recursionDepth)
self.isolatedFunctionHistories.append(fnHist.toOutputFormat(isolatedExpressionState))
# For the run through history, there is no effect
return HistoryCollection(), None
# Unary operations
if t == "UnaryExpression":
# only the history is used ret should already be None since always value
hist, ret = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
return hist, None
elif t == "UpdateExpression":
return HistoryCollection(), None
# Binary operations
elif t == "BinaryExpression":
# Return value can be ignored -> tested, that not possible to return reference in rasonable way
# Always both parts executed. So histories can be concatenated
hist, ret = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
rightHist, ret = self._analyseExpression(astNumber, curNode["children"][1], state, recursionDepth)
hist.addHistoryCollection(rightHist)
return hist, None
elif t == "AssignmentExpression":
# Execute expressions and assign right to left, AssignmentOperation missing in Parser!
# Good news: javascript gives error when callexpression on left site of assignment
hist, leftRet = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
rightHist, rightRet = self._analyseExpression(astNumber, curNode["children"][1], state, recursionDepth)
rightRet = state.getTarget(rightRet) # need only target
#.........这里部分代码省略.........
示例2: _analyseStatement
# 需要导入模块: from State import State [as 别名]
# 或者: from State.State import env_createLocal [as 别名]
#.........这里部分代码省略.........
for precCaseCond in preceedingCaseConditions:
tmpHist, ret = self._analyseExpression(astNumber, precCaseCond, condStates[-1], recursionDepth)
innerHist.addHistoryCollection(tmpHist)
# Handle current case
isDefault = len(case["children"]) == 1
if not isDefault:
preceedingCaseConditions.append(case["children"][0]);
tmpHist, ret = self._analyseExpression(astNumber, case["children"][0], condStates[-1], recursionDepth)
tmpHist.tagAsSpecialNode("cc")
innerHist.addHistoryCollection(tmpHist)
tmpHist = self._analyseStatement(astNumber, case["children"][1], condStates[-1], recursionDepth)
innerHist.addHistoryCollection(tmpHist)
else:
# For default the statement is only child
tmpHist = self._analyseStatement(case["children"][0], condStates[-1], recursionDepth)
innerHist.addHistoriesConditional(tmpHist)
condHistories.append(innerHist)
hist.addHistoriesConditional(condHistories)
state.mergeIn(condStates)
return hist
# Exceptions (ignored at the moment)
elif t == "ThrowStatement":
return HistoryCollection()
elif t == "TryStatement":
blockHist = self._analyseStatement(astNumber, curNode["children"][0], state, recursionDepth)
if len(curNode["children"]) == 3:
finallyHist = self._analyseStatement(astNumber, curNode["children"][0], state, recursionDepth)
blockHist.addHistoryCollection(finallyHist)
return blockHist
# Loops
elif t in ["WhileStatement", "DoWhileStatement"]:
hist, ret = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
hist.tagAsSpecialNode("wc")
tmpHist = self._analyseStatement(astNumber, curNode["children"][1], state, recursionDepth)
tmpHist.markAsLoopBody()
hist.addHistoryCollection(tmpHist)
return hist
elif t == "ForStatement": # YEAH! The scope is acutally the same as everywhere!
hist, ret = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
tmpHist, ret = self._analyseExpression(astNumber, curNode["children"][1], state, recursionDepth)
hist.addHistoryCollection(tmpHist)
tmpHist, ret = self._analyseExpression(astNumber, curNode["children"][2], state, recursionDepth)
hist.addHistoryCollection(tmpHist)
hist.tagAsSpecialNode("fh")
tmpHist = self._analyseStatement(astNumber, curNode["children"][3], state, recursionDepth)
tmpHist.markAsLoopBody()
hist.addHistoryCollection(tmpHist)
return hist
elif t == "ForInStatement":
# The scope is the same for the iterator name!
hist, ret = self._analyseExpression(astNumber, curNode["children"][0], state, recursionDepth)
hist.tagAsSpecialNode("fh")
# Since we do not handle loops, immediatly assign all elements within the returned enumerable (Whether array or object does not matter because both in heap)
# TODO: the for in for an array only gives the indices!!!! NO IDEA HOW TO HANDLE -> maybe a special case
tmpHist, ret =self._analyseExpression(astNumber, curNode["children"][1], state, recursionDepth)
tmpHist.tagAsSpecialNode("fh")
hist.addHistoryCollection(tmpHist)
tmpHist = self._analyseStatement(astNumber, curNode["children"][2], state, recursionDepth)
tmpHist.markAsLoopBody()
hist.addHistoryCollection(tmpHist)
return hist
### Declarations (handled here toghether with Declarators)
elif t == "VariableDeclaration":
# There might be multiple declarators as eg. "var a = 4, b = 5"
for declarator in [ast[node] for node in curNode["children"]]:
# Create the local variable
state.env_createLocal(declarator["value"])
if "children" in declarator:
hist, ret = self._analyseExpression(astNumber, declarator["children"][0], state, recursionDepth)
state.env_set(declarator["value"], state.getTarget(ret))
return hist
else:
return HistoryCollection()
elif t == "FunctionDeclaration":
# Execute isolated and store in the separate history.
# Since I remember the ObjectName, afterwards the longest history per object can be taken
isolatedFunctionState = State()
for i, param in enumerate(curNode["children"][1:-1]):
parameterName = ast[curNode["children"][i+1]]["value"]
isolatedFunctionState.env_createLocal(parameterName)
isolatedFunctionState.env_set(parameterName, isolatedFunctionState.newObject("anonymous", astNumber, nodeNumber))
fnHist = self._analyseStatement(astNumber, curNode["children"][-1], isolatedFunctionState, recursionDepth)
self.isolatedFunctionHistories.append(fnHist.toOutputFormat(isolatedFunctionState))
# For the run through history, there is no effect
return HistoryCollection()
elif t in es6_standard:
return HistoryCollection()
# For all other nodes, where no specific strategy has been assigned
else:
raise NameError('NodeName not handled')