From: Luke Kenneth Casson Leighton Date: Sat, 4 Apr 2020 12:05:04 +0000 (+0100) Subject: add modulo to parser (and div to SelectableInt) X-Git-Tag: div_pipeline~1541 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=31f713496ba2520badcd9a5edac972744d76e12c;p=soc.git add modulo to parser (and div to SelectableInt) --- diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index 95cf6e83..ee06c3f1 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -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';' diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index 4766ee25..6c7ea8f6 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -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 diff --git a/src/soc/decoder/selectable_int.py b/src/soc/decoder/selectable_int.py index 53b84f60..8b5715bb 100644 --- a/src/soc/decoder/selectable_int.py +++ b/src/soc/decoder/selectable_int.py @@ -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)