Răsfoiți Sursa

Added PI to parser and Node stringification.

Taddeus Kroes 14 ani în urmă
părinte
comite
1e753b0b1f
2 a modificat fișierele cu 18 adăugiri și 4 ștergeri
  1. 13 0
      src/node.py
  2. 5 4
      src/parser.py

+ 13 - 0
src/node.py

@@ -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):

+ 5 - 4
src/parser.py

@@ -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' \