convert numbers to python format
[sv2nmigen.git] / absyn.py
index 413e51301580f81b58f54dd12e3e1b0dc6ebbc2b..386e5d3f23bfe1ddd981d38cc1d7079a865e2fd2 100644 (file)
--- a/absyn.py
+++ b/absyn.py
@@ -54,6 +54,15 @@ class CondStatement:
         self.elsepart = elsepart
 
 
+def makeBlock(x):
+    if(type(x) == Assignment):
+        return [x]
+    elif(type(x) == CondStatement):
+        return [x]
+    else:
+        return x.statements
+
+
 class Absyn:
     def __init__(self, outputfn):
         self.outputfn = outputfn
@@ -62,6 +71,7 @@ class Absyn:
         self.ports = []
         self.wires = []
         self.comb = []
+        self.sync = []
 
     def open(self):
         if(self.outputfile is None):
@@ -126,7 +136,9 @@ class Absyn:
 
     def do_assign(self, a, stmts, indent):
         stmts.children.append(self.indent(indent))
-        stmts.children.append(Leaf(token.STRING, "m.d.comb += "))
+        stmts.children.append(Leaf(token.STRING, "m.d."))
+        stmts.children.append(Leaf(token.STRING, self.blocktype))
+        stmts.children.append(Leaf(token.STRING, " += "))
         if(self.isPort(a.left)):
             stmts.children.append(Leaf(token.STRING, "self."))
         stmts.children.append(Leaf(token.STRING, a.left))
@@ -146,7 +158,7 @@ class Absyn:
         stmts.children.append(Leaf(token.STRING, "):"))
         stmts.children.append(self.nl())
 
-        for c1 in c.ifpart.statements:
+        for c1 in makeBlock(c.ifpart):
             if(type(c1) == Assignment):
                 self.do_assign(c1, stmts, indent+1)
             else:
@@ -157,7 +169,7 @@ class Absyn:
             stmts.children.append(Leaf(token.STRING, "with m.Else():"))
             stmts.children.append(self.nl())
 
-            for c1 in c.elsepart.statements:
+            for c1 in makeBlock(c.elsepart):
                 if(type(c1) == Assignment):
                     self.do_assign(c1, stmts, indent+1)
                 else:
@@ -189,12 +201,18 @@ class Absyn:
             stmts.children.append(Leaf(token.STRING, ")"))
             stmts.children.append(self.nl())
 
-        # refactor: assignments non cond
+        self.blocktype = "comb"
         for a in self.assign:
-            self.do_assign(a, stmts)
+            self.do_assign(a, stmts, 2)
 
         for c in self.comb:
-            print("comb", c)
+            if(type(c) == Assignment):
+                self.do_assign(c, stmts, 2)
+            else:
+                self.do_ifblock(c, stmts, 2)
+
+        self.blocktype = "sync"
+        for c in self.sync:
             if(type(c) == Assignment):
                 self.do_assign(c, stmts, 2)
             else:
@@ -251,6 +269,9 @@ class Absyn:
 
     # cond assigmments and other nested blocks
     def always_comb(self, p3, p1):
-        print("always_comb")
         slist = p3[6]
         self.comb += slist.statements
+
+    def always_ff(self, p3, p1):
+        slist = p3[1]
+        self.sync += slist.statements