add elaborate function
authorTobias Platen <tplaten@posteo.de>
Sat, 26 Oct 2019 15:29:13 +0000 (17:29 +0200)
committerTobias Platen <tplaten@posteo.de>
Sat, 26 Oct 2019 15:29:13 +0000 (17:29 +0200)
absyn.py
examples/assignment.py
parse_sv.py

index e121357..a1b3427 100644 (file)
--- a/absyn.py
+++ b/absyn.py
@@ -16,6 +16,7 @@ class Absyn:
     def __init__(self):
         self.outputfile = open("output.py","w")
         self.outputfile.write(preamble)
+        self.assign = []
     def printpy(self,p):
         self.outputfile.write(str(p)+"\n")
     def assign(self,p):
@@ -32,10 +33,10 @@ class Absyn:
     def dedent(self,count):
         return Leaf(token.DEDENT, '')
     def nl(self):
-        return Leaf(token.NEWLINE, '#\n')
+        return Leaf(token.NEWLINE, '\n')
 
     def initPorts(self,params,ports):
-        pass_stmt = Node(syms.pass_stmt ,[Leaf(token.NAME, "def __init__(self):")])
+        pass_stmt = Node(syms.pass_stmt ,[Leaf(token.NAME, "def __init__(self):#FIXME")])
         if params:
             params = [Leaf(token.LPAR, '(')] + params + [Leaf(token.RPAR, ')')]
             fn = [Leaf(token.NAME, 'def'),
@@ -54,7 +55,42 @@ class Absyn:
             stmts.children.append(self.nl())
 
         return stmts
-        
+
+    def initFunc(self,ports,params):
+        params = [Leaf(token.LPAR, '('),Leaf(token.NAME,"self")] + [Leaf(token.RPAR, ')')]
+        # TODO handle sv params
+        fn = [Leaf(token.NAME, 'def'),
+              Leaf(token.NAME, '__init__', prefix=' '),
+              Node(syms.parameters, params),
+              Leaf(token.COLON, ':'),
+              self.nl()
+        ]
+        fndef = Node(syms.funcdef, fn)
+        stmts = Node(syms.stmt, [fndef])
+        for port in ports:
+            stmts.children.append(self.indent(2))
+            stmts.children.append(port)
+            stmts.children.append(self.nl())
+        return stmts
+
+    def elaborateFunc(self):
+        params = [Leaf(token.LPAR, '('),Leaf(token.NAME,"self, platform=None"),Leaf(token.RPAR, ')')]
+        fn = [Leaf(token.NAME, 'def'),
+              Leaf(token.NAME, 'elaborate', prefix=' '),
+              Node(syms.parameters, params),
+              Leaf(token.COLON, ':'),
+              self.nl()
+        ]
+        fndef = Node(syms.funcdef, fn)
+        stmts = Node(syms.stmt, [fndef])
+        stmts.children.append(self.indent(2))
+        stmts.children.append(Leaf(token.STRING,"m = Module()"))
+        stmts.children.append(self.nl())
+        ##                      
+        stmts.children.append(self.indent(2))                      
+        stmts.children.append(Leaf(token.STRING,"return m"))                      
+        stmts.children.append(self.nl())
+        return stmts
         
     def module_1(self,p):
         params = p[7]
@@ -65,16 +101,23 @@ class Absyn:
                    Leaf(token.NAME, 'Elaboratable'),
                    Leaf(token.LPAR,')'),
                    Leaf(token.COLON, ':'),
+                   self.nl(),
         ]
 
         suite = Node(syms.suite, [Leaf(token.NEWLINE, '\n'),
                                   self.indent(1),
-                                  self.initPorts(params,ports),
-                                  self.dedent(1)
+                                  self.initFunc(ports,params),
+                                  self.indent(1),
+                                  self.elaborateFunc()
+                                  
         ])
         clsdecl = Node(syms.classdef, clsname + [suite])
         clsdecl = Node(syms.compound_stmt, [clsdecl])
-        #.printpy("#clsdecl"+ repr(clsdecl))
-        #absyn.printpy("#clsstr:")
+        
         self.printpy(str(clsdecl))
         return clsdecl
+
+    # combinatorical assign
+    def cont_assign_1(self,p):
+       self.printpy("#ASSIGN"+str(list(p)))
+       self.assign += [p]
index d48fff9..c338a6b 100644 (file)
@@ -1,8 +1,16 @@
-from nmigen import Memory, Module, Signal, Cat, Elaboratable
-# module_1
-#clsdeclNode(compound_stmt, [Node(classdef, [Leaf(1, 'class'), Leaf(1, 'assignment'), Leaf(11, ':'), Node(suite, [Leaf(4, '\n'), Leaf(5, '    '), Node(stmt, [Node(small_stmt, [Node(pass_stmt, [Leaf(1, 'pass')]), Leaf(4, '\n')]), Leaf(5, '        '), Leaf(3, 'self.i = Signal() # input'), Leaf(5, '        '), Leaf(3, Leaf(4, '\n')), Leaf(5, '        '), Leaf(3, 'self.o = Signal() # output')]), Leaf(6, '')])])])
-#clsstr:
-class assignment(self):
+# this file has been generated by sv2nmigen
+
+from nmigen import Signal, Module, Const, Cat, Elaboratable
+
+
+
+#ASSIGN[None, Leaf(1, 'o'), '=', Leaf(1, 'i')]
+class assignment(Elaboratable):
+
     def __init__(self):
-        self.i = Signal() # input        
+        self.i = Signal() # input
         self.o = Signal() # output
+    def elaborate(self, platform=None):
+        m = Module()
+        return m
+
index 627aa03..3df4571 100644 (file)
@@ -5019,7 +5019,8 @@ def p_lpvalue_4(p):
 ()
 def p_cont_assign_1(p):
     '''cont_assign : lpvalue '=' expression '''
-    if(parse_debug): print('cont_assign_1 TODO', list(p))
+    if(parse_debug): print('cont_assign_1', list(p))
+    absyn.cont_assign_1(p)
     # { list<PExpr*>*tmp = new list<PExpr*>;
     #  tmp->push_back(p[1]);
     #  tmp->push_back(p[3]);