Skip to content

Instantly share code, notes, and snippets.

@4toblerone
Created February 22, 2013 20:54
Show Gist options
  • Save 4toblerone/5016471 to your computer and use it in GitHub Desktop.
Save 4toblerone/5016471 to your computer and use it in GitHub Desktop.
"""def composeAST(self):
tokenPosition = -1
for token in self.listOfTokens:
#tracks position of the current token we are inspecting
tokenPosition += 1
# if we are dealing with numbers and have two ASTNodes on the childrenStack
# we should form a mini/subtree because by the time we have two "children" nodes
# we must have at least one "parent" node , which is enough for mini/sub tree
if token.type == "NUMBER" and len(self.childrenStack) < 3 :
numberToken = ASTNode(token)
if self.listOfTokens[tokenPosition+2].type in self.mathOpTimesDivide:
self.higherChildrenStack.append(numberToken)
else:
self.childrenStack.append(numberToken)
# check if have conditions to make new subtree
if len(self.childrenStack) == 2:
mathOpNode = self.parentStack.pop()
mathOpNode.addChild(self.childrenStack.pop())
mathOpNode.addChild(self.childrenStack.pop())
self.childrenStack.append(mathOpNode)#here parent node is becoming child node
elif token.type in self.mathOpTokensTypes:
mathOpNode = ASTNode(token)
self.parentStack.append(mathOpNode)
else:
print "Doslo je do greske"
return self.childrenStack[0];
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment