add HEX to parser
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 18:16:18 +0000 (19:16 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 18:16:21 +0000 (19:16 +0100)
src/soc/decoder/power_pseudo.py
src/soc/decoder/pseudo/lexer.py
src/soc/decoder/pseudo/parser.py

index a16c77bb1ffeb63c77eaeab828ad0f40db6f6926..f6d675294b31e1c844f7ecc7b1ae3dfd9c0205a2 100644 (file)
@@ -177,7 +177,11 @@ switch (n)
         x <- 9
 """
 
-code = switchtest
+hextest = """
+RT <- 0x0001_a000_0000_0000
+"""
+
+code = hextest
 #code = lswx
 #code = testcond
 #code = testdo
index 2e637365fa739a2455c618c9b276186dfea58699..2518970f18ed4bf4d815f22f3668ad2851814066 100644 (file)
@@ -234,6 +234,7 @@ class PowerLexer:
         'WHILE',
         'BREAK',
         'NAME',
+        'HEX',     # hex numbers
         'NUMBER',  # Python decimals
         'BINARY',  # Python binary
         'STRING',  # single quoted strings only; syntax of raw strings
@@ -279,6 +280,12 @@ class PowerLexer:
     def build(self,**kwargs):
          self.lexer = lex.lex(module=self, **kwargs)
 
+    def t_HEX(self, t):
+        r"""0x[0-9a-f_]+"""
+        val = t.value.replace("_", "")
+        t.value = SelectableInt(int(val, 16), (len(val)-2)*16)
+        return t
+
     def t_BINARY(self, t):
         r"""0b[01]+"""
         t.value = SelectableInt(int(t.value, 2), len(t.value)-2)
index 70b154e7b7c8fa3c4e3de24c4c00254153abc1dd..d8d0969c0a32bcd3acd457bfb6799d756df972f9 100644 (file)
@@ -596,6 +596,7 @@ class PowerParser:
     def p_atom_number(self, p):
         """atom : BINARY
                 | NUMBER
+                | HEX
                 | STRING"""
         p[0] = ast.Constant(p[1])