Skip to content

Commit

Permalink
encapsulate type token type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
hstoebel committed Feb 20, 2020
1 parent 2523a3f commit 6f5960b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion interpreter/app/ast/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(self):
self.current_operator = self.root

def feed(self, token):
if isinstance(token, OperatorToken):
if token.is_operator():
self._feed_operator(token)
else:
self._feed_number(token)
Expand Down
6 changes: 3 additions & 3 deletions interpreter/app/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def advance_token(self):
self.current_token = next_token

def done(self):
return isinstance(self.current_token, token.EOFToken)
return self.current_token.is_a(token.EOF)

def eat(self, *token_types):
"""
Expand Down Expand Up @@ -121,9 +121,9 @@ def expr(self):
ast = AST()

while not self.done():
if isinstance(self.current_token, token.OperatorToken):
if self.current_token.is_operator():
ast.feed(self.eat_operator())
elif isinstance(self.current_token, token.SpaceToken):
elif self.current_token.is_a(token.SPACE):
self.advance_token()
else:
ast.feed(self.eat_integers())
Expand Down
14 changes: 13 additions & 1 deletion interpreter/app/token/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ def __str__(self):
def __repr__(self):
return self.__str__()

class SpaceToken(object):
def is_a(self, type: str) -> bool:
"""
Returns if token is of the same type as `type`
Example:
token.is_a('INTEGER')
"""

return self.type == type

def is_operator(self) -> bool:
return isinstance(self, OperatorToken)

class SpaceToken(Token):
def __init__(self) -> None:
self.type = SPACE
self.value = None
Expand Down

0 comments on commit 6f5960b

Please sign in to comment.