Skip to content

Commit

Permalink
Add height and length definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
redelmann committed Apr 10, 2018
1 parent 95308f0 commit fed0036
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions nugget/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def get_focus(self):
def get_children(self):
return []

def height(self):
return None

def length(self):
return None


class Binary(Expr):
"""Binary operation."""
Expand Down Expand Up @@ -274,6 +280,12 @@ def get_focus(self):
def get_children(self):
return [self.lhs, self.rhs]

def height(self):
return 1 + max(self.lhs.height(), self.rhs.height())

def length(self):
return 1 + self.lhs.length() + self.rhs.length()


class Atom(Expr):
"""Variable."""
Expand Down Expand Up @@ -304,6 +316,12 @@ def __hash__(self):
def is_atom(self):
return True

def height(self):
return 0

def length(self):
return 1


class Focus(Expr):
"""Focused expression."""
Expand Down Expand Up @@ -414,3 +432,9 @@ def get_focus(self):
def get_children(self):
return [self.expr]

def height(self):
return 1 + self.expr.height()

def length(self):
return 1 + self.expr.length()

0 comments on commit fed0036

Please sign in to comment.