Commit 1e753b0b authored by Taddeus Kroes's avatar Taddeus Kroes

Added PI to parser and Node stringification.

parent 6139bd82
...@@ -45,6 +45,9 @@ OP_HINT = 17 ...@@ -45,6 +45,9 @@ OP_HINT = 17
OP_REWRITE_ALL = 18 OP_REWRITE_ALL = 18
OP_REWRITE = 19 OP_REWRITE = 19
# Special identifierd
PI = 'pi'
TYPE_MAP = { TYPE_MAP = {
int: TYPE_INTEGER, int: TYPE_INTEGER,
...@@ -352,7 +355,17 @@ class ExpressionLeaf(Leaf, ExpressionBase): ...@@ -352,7 +355,17 @@ class ExpressionLeaf(Leaf, ExpressionBase):
return self.negated == other.negated and self.type == other.type \ return self.negated == other.negated and self.type == other.type \
and self.value == other.value 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): def __repr__(self):
return str(self)
return '-' * self.negated + str(self.value) return '-' * self.negated + str(self.value)
def equals(self, other, ignore_negation=False): def equals(self, other, ignore_negation=False):
......
...@@ -15,7 +15,7 @@ from pybison import BisonParser, BisonSyntaxError ...@@ -15,7 +15,7 @@ from pybison import BisonParser, BisonSyntaxError
from graph_drawing.graph import generate_graph from graph_drawing.graph import generate_graph
from node import ExpressionNode as Node, ExpressionLeaf as Leaf, OP_MAP, \ 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 rules import RULES
from possibilities import filter_duplicates, pick_suggestion, apply_suggestion from possibilities import filter_duplicates, pick_suggestion, apply_suggestion
...@@ -156,7 +156,7 @@ class Parser(BisonParser): ...@@ -156,7 +156,7 @@ class Parser(BisonParser):
left, right = filter(None, match.groups()) left, right = filter(None, match.groups())
# Filter words (otherwise they will be preprocessed as well) # 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 return left + right
# If all characters on the right are numbers. e.g. "a4", the # If all characters on the right are numbers. e.g. "a4", the
...@@ -405,9 +405,10 @@ class Parser(BisonParser): ...@@ -405,9 +405,10 @@ class Parser(BisonParser):
% (option, target)) # pragma: nocover % (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(): for op_str, op in OP_MAP.iteritems():
operators += '"%s"%s{ returntoken(%s); }\n' \ operators += '"%s"%s{ returntoken(%s); }\n' \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment