1 from lib2to3
.pytree
import Node
, Leaf
2 from lib2to3
.pgen2
import token
3 from lib2to3
.pygram
import python_symbols
as syms
5 preamble
= """# this file has been generated by sv2nmigen
7 from nmigen import Signal, Module, Const, Cat, Elaboratable
17 self
.outputfile
= open("output.py","w")
18 self
.outputfile
.write(preamble
)
21 self
.outputfile
.write(str(p
)+"\n")
26 # m.d.comb += [l.eq(r)]
27 def indent(self
,count
):
29 return Leaf(token
.INDENT
, '>>> '*count
)
31 return Leaf(token
.INDENT
, ' '*4*count
)
33 def dedent(self
,count
):
34 return Leaf(token
.DEDENT
, '')
36 return Leaf(token
.NEWLINE
, '\n')
38 def initPorts(self
,params
,ports
):
39 pass_stmt
= Node(syms
.pass_stmt
,[Leaf(token
.NAME
, "def __init__(self):#FIXME")])
41 params
= [Leaf(token
.LPAR
, '(')] + params
+ [Leaf(token
.RPAR
, ')')]
42 fn
= [Leaf(token
.NAME
, 'def'),
43 Leaf(token
.NAME
, '__initXXX__', prefix
=' '),
44 Node(syms
.parameters
, params
),
45 Leaf(token
.COLON
, ':')]
46 fndef
= Node(syms
.funcdef
, fn
)
47 stmts
= Node(syms
.stmt
, [fndef
])
49 stmts
= Node(syms
.small_stmt
, [pass_stmt
, Leaf(token
.NEWLINE
, '\n')])
50 stmts
= Node(syms
.stmt
, [stmts
])
53 stmts
.children
.append(self
.indent(2))
54 stmts
.children
.append(port
)
55 stmts
.children
.append(self
.nl())
59 def initFunc(self
,ports
,params
):
60 params
= [Leaf(token
.LPAR
, '('),Leaf(token
.NAME
,"self")] + [Leaf(token
.RPAR
, ')')]
61 # TODO handle sv params
62 fn
= [Leaf(token
.NAME
, 'def'),
63 Leaf(token
.NAME
, '__init__', prefix
=' '),
64 Node(syms
.parameters
, params
),
65 Leaf(token
.COLON
, ':'),
68 fndef
= Node(syms
.funcdef
, fn
)
69 stmts
= Node(syms
.stmt
, [fndef
])
71 stmts
.children
.append(self
.indent(2))
72 stmts
.children
.append(port
)
73 stmts
.children
.append(self
.nl())
76 def elaborateFunc(self
):
77 params
= [Leaf(token
.LPAR
, '('),Leaf(token
.NAME
,"self, platform=None"),Leaf(token
.RPAR
, ')')]
78 fn
= [Leaf(token
.NAME
, 'def'),
79 Leaf(token
.NAME
, 'elaborate', prefix
=' '),
80 Node(syms
.parameters
, params
),
81 Leaf(token
.COLON
, ':'),
84 fndef
= Node(syms
.funcdef
, fn
)
85 stmts
= Node(syms
.stmt
, [fndef
])
86 stmts
.children
.append(self
.indent(2))
87 stmts
.children
.append(Leaf(token
.STRING
,"m = Module()"))
88 stmts
.children
.append(self
.nl())
90 stmts
.children
.append(self
.indent(2))
91 stmts
.children
.append(Leaf(token
.STRING
,"return m"))
92 stmts
.children
.append(self
.nl())
98 clsname
= [Leaf(token
.NAME
, 'class'),
99 Leaf(token
.NAME
, p
[4], prefix
=' '),
100 Leaf(token
.LPAR
,'('),
101 Leaf(token
.NAME
, 'Elaboratable'),
102 Leaf(token
.LPAR
,')'),
103 Leaf(token
.COLON
, ':'),
107 suite
= Node(syms
.suite
, [Leaf(token
.NEWLINE
, '\n'),
109 self
.initFunc(ports
,params
),
114 clsdecl
= Node(syms
.classdef
, clsname
+ [suite
])
115 clsdecl
= Node(syms
.compound_stmt
, [clsdecl
])
117 self
.printpy(str(clsdecl
))
120 # combinatorical assign
121 def cont_assign_1(self
,p
):
122 self
.printpy("#ASSIGN"+str(list(p
)))