Skip to content
Snippets Groups Projects
Commit 1e753b0b authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Added PI to parser and Node stringification.

parent 6139bd82
No related branches found
No related tags found
No related merge requests found
......@@ -45,6 +45,9 @@ OP_HINT = 17
OP_REWRITE_ALL = 18
OP_REWRITE = 19
# Special identifierd
PI = 'pi'
TYPE_MAP = {
int: TYPE_INTEGER,
......@@ -352,7 +355,17 @@ class ExpressionLeaf(Leaf, ExpressionBase):
return self.negated == other.negated and self.type == other.type \
and self.value == other.value
def __str__(self):
val = str(self.value)
# Replace PI leaf by the Greek character
if val == PI:
val = 'π'
return '-' * self.negated + val
def __repr__(self):
return str(self)
return '-' * self.negated + str(self.value)
def equals(self, other, ignore_negation=False):
......
......@@ -15,7 +15,7 @@ from pybison import BisonParser, BisonSyntaxError
from graph_drawing.graph import generate_graph
from node import ExpressionNode as Node, ExpressionLeaf as Leaf, OP_MAP, \
TOKEN_MAP, TYPE_OPERATOR, OP_COMMA, OP_NEG, OP_MUL, Scope
TOKEN_MAP, TYPE_OPERATOR, OP_COMMA, OP_NEG, OP_MUL, Scope, PI
from rules import RULES
from possibilities import filter_duplicates, pick_suggestion, apply_suggestion
......@@ -156,7 +156,7 @@ class Parser(BisonParser):
left, right = filter(None, match.groups())
# Filter words (otherwise they will be preprocessed as well)
if (left + right).upper() in self.tokens:
if (left + right).upper() in self.tokens + [PI.upper()]:
return left + right
# If all characters on the right are numbers. e.g. "a4", the
......@@ -405,9 +405,10 @@ class Parser(BisonParser):
% (option, target)) # pragma: nocover
# -----------------------------------------
# operator tokens
# PI and operator tokens
# -----------------------------------------
operators = ''
operators = '"%s"%s{ returntoken(IDENTIFIER); }\n' \
% (PI, ' ' * (8 - len(PI)))
for op_str, op in OP_MAP.iteritems():
operators += '"%s"%s{ returntoken(%s); }\n' \
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment