add modulo to parser (and div to SelectableInt)
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 12:05:04 +0000 (13:05 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sat, 4 Apr 2020 12:05:04 +0000 (13:05 +0100)
src/soc/decoder/pseudo/lexer.py
src/soc/decoder/pseudo/parser.py
src/soc/decoder/selectable_int.py

index 95cf6e83d78dc91b7fbfa7b8211ed81c31dfcbc5..ee06c3f154578d84bd20a524f38c1defece8b274 100644 (file)
@@ -251,6 +251,7 @@ class PowerLexer:
         'MINUS',
         'MULT',
         'DIV',
+        'MOD',
         'INVERT',
         'APPEND',
         'BITOR',
@@ -300,6 +301,7 @@ class PowerLexer:
     t_MINUS = r'-'
     t_MULT = r'\*'
     t_DIV = r'/'
+    t_MOD = r'%'
     t_INVERT = r'¬'
     t_COMMA = r','
     t_SEMICOLON = r';'
index 4766ee256365752e655394b4f9fa08e27ea3aae8..6c7ea8f6a7981095b2af6a9d42d2b32721f3d82a 100644 (file)
@@ -112,6 +112,7 @@ binary_ops = {
     "-": ast.Sub(),
     "*": ast.Mult(),
     "/": ast.Div(),
+    "%": ast.Mod(),
     "<=": make_le_compare,
     ">=": make_ge_compare,
     "<": make_lt_compare,
@@ -140,6 +141,32 @@ def check_concat(node):  # checks if the comparison is already a concat
 # also part of Ply
 #import yacc
 
+# https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html
+# python operator precedence
+# Highest precedence at top, lowest at bottom.
+# Operators in the same box evaluate left to right.
+#
+# Operator Description
+# ()                                                     Parentheses (grouping)
+# f(args...)                                             Function call
+# x[index:index]                                         Slicing
+# x[index]                                               Subscription
+# x.attribute                                            Attribute reference
+# **                                                     Exponentiation
+# ~x                                                     Bitwise not
+# +x, -x                                                 Positive, negative
+# *, /, %                                                mul, div, remainder
+# +, -                                                   Addition, subtraction
+# <<, >>                                                 Bitwise shifts
+# &                                                      Bitwise AND
+# ^                                                      Bitwise XOR
+# |                                                      Bitwise OR
+# in, not in, is, is not, <, <=,  >,  >=, <>, !=, ==     comp, membership, ident
+# not x                                                  Boolean NOT
+# and                                                    Boolean AND
+# or                                                     Boolean OR
+# lambda                                                 Lambda expression
+
 class PowerParser:
 
     precedence = (
@@ -147,7 +174,7 @@ class PowerParser:
         ("left", "BITOR"),
         ("left", "BITAND"),
         ("left", "PLUS", "MINUS"),
-        ("left", "MULT", "DIV"),
+        ("left", "MULT", "DIV", "MOD"),
         ("left", "INVERT"),
     )
 
@@ -348,6 +375,7 @@ class PowerParser:
                       | comparison MINUS comparison
                       | comparison MULT comparison
                       | comparison DIV comparison
+                      | comparison MOD comparison
                       | comparison EQ comparison
                       | comparison LE comparison
                       | comparison GE comparison
index 53b84f60957e9ed3c5e2b672b2aafb26d727b815..8b5715bbf21692186a08dab39ca667c5e9114e1e 100644 (file)
@@ -20,6 +20,14 @@ class SelectableInt:
         assert b.bits == self.bits
         return SelectableInt(self.value * b.value, self.bits)
 
+    def __div__(self, b):
+        assert b.bits == self.bits
+        return SelectableInt(self.value / b.value, self.bits)
+
+    def __mod__(self, b):
+        assert b.bits == self.bits
+        return SelectableInt(self.value % b.value, self.bits)
+
     def __or__(self, b):
         assert b.bits == self.bits
         return SelectableInt(self.value | b.value, self.bits)