add start on switch/case/default in parser
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 16:05:43 +0000 (17:05 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 16:05:43 +0000 (17:05 +0100)
src/soc/decoder/power_pseudo.py
src/soc/decoder/pseudo/lexer.py
src/soc/decoder/pseudo/parser.py
src/soc/decoder/pseudo/pywriter.py

index 2de3f5b83167476e5a953dd317e7a2c463caaa08..7005bd0889e9464b397679f4b876d948aea73bf1 100644 (file)
@@ -166,7 +166,18 @@ _lswx = """
 GPR(r)[x] <- 1
 """
 
-code = lswx
+switchtest = """
+switch (n)
+    case(1): x <- 5
+    case(2): fallthrough
+    case(3):
+        x <- 3
+    default:
+        x <- 9
+"""
+
+code = switchtest
+#code = lswx
 #code = testcond
 #code = testdo
 #code = _bpermd
index 1bd5d79eba0686ff606f0385231b184e9e9ddf7a..2e637365fa739a2455c618c9b276186dfea58699 100644 (file)
@@ -419,20 +419,32 @@ class IndentLexer(PowerLexer):
         except StopIteration:
             return None
 
+switchtest = """
+switch (n)
+    case(1): x <- 5
+    case(2): fallthrough
+    case(3):
+        x <- 3
+    default:
+        x <- 9
+print (5)
+"""
+
+cnttzd = """
+n  <- 0
+do while n < 64
+   if (RS)[63-n] = 0b1 then
+        leave
+   n  <- n + 1
+RA <- EXTZ64(n)
+print (RA)
+"""
+
 if __name__ == '__main__':
 
     # quick test/demo
-    cnttzd = """
-    n  <- 0
-    do while n < 64
-       if (RS)[63-n] = 0b1 then
-            leave
-       n  <- n + 1
-    RA <- EXTZ64(n)
-    print (RA)
-    """
-
-    code = cnttzd
+    #code = cnttzd
+    code = switchtest
 
     lexer = IndentLexer(debug=1)
     # Give the lexer some input
@@ -440,3 +452,7 @@ if __name__ == '__main__':
     print (code)
     lexer.input(code)
 
+    tokens = iter(lexer.token, None)
+    for token in tokens:
+        print (token)
+
index a65581d5e68c64550d091e50884ce452a050150e..7906e82518ced4fe5a1aec09f0b998103ed9e2d0 100644 (file)
@@ -382,6 +382,7 @@ class PowerParser:
     def p_compound_stmt(self, p):
         """compound_stmt : if_stmt
                          | while_stmt
+                         | switch_stmt
                          | for_stmt
                          | funcdef
         """
@@ -411,6 +412,61 @@ class PowerParser:
         else:
             p[0] = ast.While(p[3], p[5], p[8])
 
+    def p_switch_smt(self, p):
+        """switch_stmt : SWITCH LPAR atom RPAR COLON NEWLINE INDENT switches DEDENT
+        """
+        switchon = p[3]
+        print("switch stmt")
+        print(astor.dump_tree(p[1]))
+
+        cases = []
+        current_cases = []
+        for (case, suite) in p[8]:
+            print (case, suite)
+        res = []
+        p[0] = ast.If([], [], [])
+
+    def p_switches(self, p):
+        """switches : switch_list switch_default
+                    | switch_default
+        """
+        if len(p) == 3:
+            p[0] = p[1] + [p[2]]
+        else:
+            p[0] = [p[1]]
+
+    def p_switch_list(self, p):
+        """switch_list : switch_case switch_list
+                       | switch_case
+        """
+        if len(p) == 3:
+            p[0] = [p[1]] + p[2]
+        else:
+            p[0] = [p[1]]
+
+    def p_switch_case(self, p):
+        """switch_case : CASE LPAR atomlist RPAR COLON suite
+        """
+        # XXX bad hack
+        if isinstance(p[6][0], ast.Name) and p[6][0].id == 'fallthrough':
+            p[6] = None
+        p[0] = (p[3], p[6])
+
+    def p_switch_default(self, p):
+        """switch_default : DEFAULT COLON suite
+        """
+        p[0] = ('default', p[3])
+
+    def p_atomlist(self, p):
+        """atomlist : atom COMMA atomlist
+                    | atom
+        """
+        assert isinstance(p[1], ast.Constant), "case must be numbers"
+        if len(p) == 4:
+            p[0] = [p[1].value] + p[3]
+        else:
+            p[0] = [p[1].value]
+
     def p_if_stmt(self, p):
         """if_stmt : IF test COLON suite ELSE COLON if_stmt
                    | IF test COLON suite ELSE COLON suite
index 052ee95e481ce5724c10c6d74a886f00c92330eb..5002ce4c231006ceaec0103a72ecfb71f21b18fc 100644 (file)
@@ -72,7 +72,8 @@ class PyISAWriter(ISA):
 
 if __name__ == '__main__':
     isa = PyISAWriter()
-    isa.write_pysource('system')
+    isa.write_pysource('sprset')
+    #isa.write_pysource('system')
     exit(0)
     isa.write_pysource('stringldst')
     isa.write_pysource('fixedshift')