@@ -16,7 +16,7 @@ def __init__(self, requirejs=False, insert_runtime=False):
1616 #self._class_props = dict()
1717
1818 def visit_Print (self , node ):
19- r = [ 'fmt.Print (%s);' % self .visit (e ) for e in node .values ]
19+ r = [ 'fmt.Println (%s);' % self .visit (e ) for e in node .values ]
2020 return '' .join (r )
2121
2222 def visit_Expr (self , node ):
@@ -32,6 +32,7 @@ def visit_Module(self, node):
3232
3333 for b in node .body :
3434 line = self .visit (b )
35+
3536 if line :
3637 for sub in line .splitlines ():
3738 if sub == ';' :
@@ -45,6 +46,24 @@ def visit_Module(self, node):
4546 lines = header + lines
4647 return '\n ' .join ( lines )
4748
49+
50+ def visit_Compare (self , node ):
51+ comp = [ '(' ]
52+ comp .append ( self .visit (node .left ) )
53+ comp .append ( ')' )
54+
55+ for i in range ( len (node .ops ) ):
56+ comp .append ( self .visit (node .ops [i ]) )
57+
58+ if isinstance (node .comparators [i ], ast .BinOp ):
59+ comp .append ('(' )
60+ comp .append ( self .visit (node .comparators [i ]) )
61+ comp .append (')' )
62+ else :
63+ comp .append ( self .visit (node .comparators [i ]) )
64+
65+ return ' ' .join ( comp )
66+
4867 def _visit_call_helper_go (self , node ):
4968 name = self .visit (node .func )
5069 if name == '__go__' :
@@ -55,9 +74,57 @@ def _visit_call_helper_go(self, node):
5574 return SyntaxError ('invalid special go call' )
5675
5776 def visit_FunctionDef (self , node ):
58- args = self .visit (node .args )
77+ args_typedefs = {}
78+ return_type = None
79+ for decor in node .decorator_list :
80+ if isinstance (decor , ast .Call ) and isinstance (decor .func , ast .Name ) and decor .func .id == '__typedef__' :
81+ for key in decor .keywords :
82+ args_typedefs [ key .arg ] = key .value .id
83+ elif isinstance (decor , ast .Call ) and isinstance (decor .func , ast .Name ) and decor .func .id == 'returns' :
84+ if decor .keywords :
85+ raise SyntaxError ('invalid go return type' )
86+ else :
87+ return_type = decor .args [0 ].id
88+
89+
90+ #args = self.visit(node.args)
91+ args = []
92+ oargs = []
93+ offset = len (node .args .args ) - len (node .args .defaults )
94+ varargs = False
95+ varargs_name = None
96+ for i , arg in enumerate (node .args .args ):
97+ a = arg .id
98+ if a in args_typedefs :
99+ #a = '%s %s' %(args_typedefs[a], a)
100+ a = '%s %s' % (a , args_typedefs [a ])
101+ else :
102+ err = 'error in function: %s' % node .name
103+ err += '\n missing typedef: %s' % arg .id
104+ raise SyntaxError (err )
105+
106+ dindex = i - offset
107+ if a .startswith ('__variable_args__' ): ## TODO support go `...` varargs
108+ varargs_name = a .split ('__' )[- 1 ]
109+ varargs = ['_vararg_%s' % n for n in range (16 ) ]
110+ args .append ( '[%s]' % ',' .join (varargs ) )
111+
112+ elif dindex >= 0 and node .args .defaults :
113+ default_value = self .visit ( node .args .defaults [dindex ] )
114+ oargs .append ( '%s:%s' % (a , default_value ) )
115+ else :
116+ args .append ( a )
117+
118+ if oargs :
119+ #args.append( '[%s]' % ','.join(oargs) )
120+ args .append ( '{%s}' % ',' .join (oargs ) )
121+
122+ ####
59123 out = []
60- out .append ( self .indent () + 'func %s(%s) {\n ' % (node .name , ', ' .join (args )) )
124+ if return_type :
125+ out .append ( self .indent () + 'func %s(%s) %s {\n ' % (node .name , ', ' .join (args ), return_type ) )
126+ else :
127+ out .append ( self .indent () + 'func %s(%s) {\n ' % (node .name , ', ' .join (args )) )
61128 self .push ()
62129 for b in node .body :
63130 v = self .visit (b )
@@ -119,7 +186,12 @@ def main(script, insert_runtime=True):
119186 script = runtime + '\n ' + script
120187
121188 tree = ast .parse (script )
122- return GoGenerator ().visit (tree )
189+ try :
190+ return GoGenerator ().visit (tree )
191+ except SyntaxError as err :
192+ sys .stderr .write (script )
193+ raise err
194+
123195
124196
125197def command ():
0 commit comments