add invert operator, fix unary ops
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 3 Apr 2020 18:25:04 +0000 (19:25 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 3 Apr 2020 18:25:04 +0000 (19:25 +0100)
src/soc/decoder/pseudo/lexer.py
src/soc/decoder/pseudo/parser.py

index 86b04efd77868bddc8a0989393a25eefd99dbfc3..95cf6e83d78dc91b7fbfa7b8211ed81c31dfcbc5 100644 (file)
@@ -251,6 +251,7 @@ class PowerLexer:
         'MINUS',
         'MULT',
         'DIV',
+        'INVERT',
         'APPEND',
         'BITOR',
         'BITAND',
@@ -299,6 +300,7 @@ class PowerLexer:
     t_MINUS = r'-'
     t_MULT = r'\*'
     t_DIV = r'/'
+    t_INVERT = r'¬'
     t_COMMA = r','
     t_SEMICOLON = r';'
     t_APPEND = r'\|\|'
index 02bb2cc1a645d3634aace922bda2da548df89eea..ff66e01a844c80c2dff2358bfe19c4a581bd3a70 100644 (file)
@@ -108,8 +108,9 @@ binary_ops = {
     "=": make_eq_compare,
 }
 unary_ops = {
-    "+": ast.Add,
-    "-": ast.Sub,
+    "+": ast.UAdd(),
+    "-": ast.USub(),
+    "¬": ast.Invert(),
     }
 
 def check_concat(node): # checks if the comparison is already a concat
@@ -134,6 +135,7 @@ class PowerParser:
         ("left", "EQ", "GT", "LT", "LE", "GE", "LTU", "GTU"),
         ("left", "PLUS", "MINUS"),
         ("left", "MULT", "DIV"),
+        ("left", "INVERT"),
         )
 
     def __init__(self):
@@ -345,6 +347,7 @@ class PowerParser:
                       | comparison BITAND comparison
                       | PLUS comparison
                       | MINUS comparison
+                      | INVERT comparison
                       | comparison APPEND comparison
                       | power"""
         if len(p) == 4:
@@ -361,7 +364,7 @@ class PowerParser:
             else:
                 p[0] = ast.BinOp(p[1], binary_ops[p[2]], p[3])
         elif len(p) == 3:
-            p[0] = unary_ops[p[1]](p[2])
+            p[0] = ast.UnaryOp(unary_ops[p[1]], p[2])
         else:
             p[0] = p[1]