Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merged in feature/improve_tree (pull request boriel-basic#227)
Browse files Browse the repository at this point in the history
Feature/improve tree

Approved-by: Jose Rodriguez <boriel@gmail.com>
  • Loading branch information
boriel committed Oct 19, 2019
2 parents b750dd9 + b62bc8e commit 3c19ba0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ examples/*.tzx
scratch/
.coverage
htmlcov/
build/
64 changes: 23 additions & 41 deletions ast_/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,47 +98,29 @@ def children(self, value):
for x in value:
self.children.append(x)

def inorder(self, funct, stopOn=None):
""" Iterates in order, calling the function with the current node.
If stopOn is set to True or False, it will stop on true or false.
def inorder(self):
""" Traverses the tree in order
"""
if stopOn is None:
for i in self.children:
i.inorder(funct)
else:
for i in self.children:
if i.inorder(funct) == stopOn:
return stopOn

return funct(self)

def preorder(self, funct, stopOn=None):
""" Iterates in preorder, calling the function with the current node.
If stopOn is set to True or False, it will stop on true or false.
for i in self.children:
yield from i.inorder()

yield self

def preorder(self):
""" Traverses the tree in preorder
"""
if funct(self.symbol) == stopOn and stopOn is not None:
return stopOn

if stopOn is None:
for i in self.children:
i.preorder(funct)
else:
for i in self.children:
if i.preorder(funct) == stopOn:
return stopOn

def postorder(self, funct, stopOn=None):
""" Iterates in postorder, calling the function with the current node.
If stopOn is set to True or False, it will stop on true or false.
yield self

for i in self.children:
yield from i.preorder()

def postorder(self):
""" Traverses the tree in postorder
"""
if stopOn is None:
for i in range(len(self.children) - 1, -1, -1):
self.children[i].postorder(funct)
else:
for i in range(len(self.children) - 1, -1, -1):
if self.children[i].postorder(funct) == stopOn:
return stopOn
return funct(self.symbol)
for i in range(len(self.children) - 1, -1, -1):
yield from self.children[i].postorder()

yield self

def appendChild(self, node):
""" Appends the given node to the current children list
Expand All @@ -151,15 +133,15 @@ def prependChild(self, node):
self.children.insert(0, node)

@classmethod
def makenode(clss, symbol, *nexts):
def makenode(cls, symbol, *nexts):
""" Stores the symbol in an AST instance,
and left and right to the given ones
"""
result = clss(symbol)
result = cls(symbol)
for i in nexts:
if i is None:
continue
if not isinstance(i, clss):
if not isinstance(i, cls):
raise NotAnAstError(i)
result.appendChild(i)

Expand Down

0 comments on commit 3c19ba0

Please sign in to comment.