+from lib2to3.pytree import Node, Leaf
+from lib2to3.pgen2 import token
+from lib2to3.pygram import python_symbols as syms
+
+preamble = """# this file has been generated by sv2nmigen
+
+from nmigen import Signal, Module, Const, Cat, Elaboratable
+
+
+
+"""
+
+indent_debug = 0
+
class Absyn:
def __init__(self):
self.outputfile = open("output.py","w")
+ self.outputfile.write(preamble)
def printpy(self,p):
self.outputfile.write(str(p)+"\n")
+ def assign(self,p):
+ p = list(p)
+ if(p[1]=="assign"):
+ self.printpy(p[4])
+ # m.d.comb += [l.eq(r)]
+ def indent(self,count):
+ if(indent_debug):
+ return Leaf(token.INDENT, '>>> '*count)
+ else:
+ return Leaf(token.INDENT, ' '*4*count)
+
+ def dedent(self,count):
+ return Leaf(token.DEDENT, '')
+ def nl(self):
+ return Leaf(token.NEWLINE, '#\n')
+
+ def initPorts(self,params,ports):
+ pass_stmt = Node(syms.pass_stmt ,[Leaf(token.NAME, "def __init__(self):")])
+ if params:
+ params = [Leaf(token.LPAR, '(')] + params + [Leaf(token.RPAR, ')')]
+ fn = [Leaf(token.NAME, 'def'),
+ Leaf(token.NAME, '__initXXX__', prefix=' '),
+ Node(syms.parameters, params),
+ Leaf(token.COLON, ':')]
+ fndef = Node(syms.funcdef, fn)
+ stmts = Node(syms.stmt, [fndef])
+ else:
+ stmts = Node(syms.small_stmt, [pass_stmt, Leaf(token.NEWLINE, '\n')])
+ stmts = Node(syms.stmt, [stmts])
+
+ for port in ports:
+ stmts.children.append(self.indent(2))
+ stmts.children.append(port)
+ stmts.children.append(self.nl())
+
+ return stmts
+
+
+ def module_1(self,p):
+ params = p[7]
+ ports = p[8]
+ clsname = [Leaf(token.NAME, 'class'),
+ Leaf(token.NAME, p[4], prefix=' '),
+ Leaf(token.LPAR,'('),
+ Leaf(token.NAME, 'Elaboratable'),
+ Leaf(token.LPAR,')'),
+ Leaf(token.COLON, ':'),
+ ]
+
+ suite = Node(syms.suite, [Leaf(token.NEWLINE, '\n'),
+ self.indent(1),
+ self.initPorts(params,ports),
+ self.dedent(1)
+ ])
+ 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