From 77d39c7058935e4f0dea7bf23afa2a3314a995f7 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Fri, 3 Apr 2020 19:25:04 +0100 Subject: [PATCH] add invert operator, fix unary ops --- src/soc/decoder/pseudo/lexer.py | 2 ++ src/soc/decoder/pseudo/parser.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/soc/decoder/pseudo/lexer.py b/src/soc/decoder/pseudo/lexer.py index 86b04efd..95cf6e83 100644 --- a/src/soc/decoder/pseudo/lexer.py +++ b/src/soc/decoder/pseudo/lexer.py @@ -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'\|\|' diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index 02bb2cc1..ff66e01a 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -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] -- 2.30.2