Fix __str__ in case self.token is not a string.

This commit is contained in:
Ethan Dalool 2020-09-09 12:55:09 -07:00
parent bab1c2bffc
commit 7052b2c432

View file

@ -59,8 +59,10 @@ class ExpressionTree:
if self.token is None: if self.token is None:
return '""' return '""'
if self.token not in OPERATORS: self_token = str(self.token)
t = self.token
if self_token not in OPERATORS:
t = self_token
t = t.replace('"', '\\"') t = t.replace('"', '\\"')
t = t.replace('(', '\\(') t = t.replace('(', '\\(')
t = t.replace(')', '\\)') t = t.replace(')', '\\)')
@ -72,8 +74,8 @@ class ExpressionTree:
child = self.children[0] child = self.children[0]
childstring = str(child) childstring = str(child)
if child.token in OPERATORS: if child.token in OPERATORS:
return f'{self.token}({childstring})' return f'{self_token}({childstring})'
return f'{self.token} {childstring}' return f'{self_token} {childstring}'
children = [] children = []
for child in self.children: for child in self.children:
@ -83,9 +85,9 @@ class ExpressionTree:
children.append(childstring) children.append(childstring)
if len(children) == 1: if len(children) == 1:
return f'{self.token} {children[0]}' return f'{self_token} {children[0]}'
s = f' {self.token} ' s = f' {self_token} '
s = s.join(children) s = s.join(children)
return s return s