@@ -12,11 +12,41 @@ class GoGenerator( pythonjs.JSGenerator ):
1212
1313 def __init__ (self , requirejs = False , insert_runtime = False ):
1414 pythonjs .JSGenerator .__init__ (self , requirejs = False , insert_runtime = False )
15- #self._classes = dict()
16- #self._class_props = dict()
15+
16+ self ._class_stack = list ()
17+ self ._classes = dict ()
18+ self ._class_props = dict ()
19+
1720 self ._vars = set ()
1821 self ._kwargs_type_ = dict ()
1922
23+ def visit_ClassDef (self , node ):
24+ self ._class_stack .append ( node )
25+ node ._parents = set ()
26+ out = []
27+ props = set ()
28+ bases = set ()
29+ base_classes = set ()
30+
31+ self ._classes [ node .name ] = node
32+ self ._class_props [ node .name ] = props
33+ for decor in node .decorator_list : ## class decorators
34+ if isinstance (decor , ast .Call ):
35+ props .update ( [self .visit (a ) for a in decor .args ] )
36+
37+ out .append ( 'type %s struct {' % node .name )
38+ for name in props :
39+ out .append ('%s int' % name )
40+
41+ out .append ('}' )
42+
43+ for b in node .body :
44+ out .append ( self .visit (b ) )
45+
46+ self ._class_stack .pop ()
47+ return '\n ' .join (out )
48+
49+
2050 def visit_Print (self , node ):
2151 r = [ 'fmt.Println(%s);' % self .visit (e ) for e in node .values ]
2252 return '' .join (r )
@@ -48,7 +78,7 @@ def visit_Module(self, node):
4878 else :
4979 lines .append ( sub )
5080 else :
51- raise SyntaxError (line )
81+ raise SyntaxError (b )
5282
5383 lines .append ('type _kwargs_type_ struct {' )
5484 for name in self ._kwargs_type_ :
@@ -167,12 +197,17 @@ def _visit_function(self, node):
167197 offset = len (node .args .args ) - len (node .args .defaults )
168198 varargs = False
169199 varargs_name = None
200+ is_method = False
170201 for i , arg in enumerate (node .args .args ):
171202 arg_name = arg .id
172203 if arg_name not in args_typedefs :
173- err = 'error in function: %s' % node .name
174- err += '\n missing typedef: %s' % arg .id
175- raise SyntaxError (err )
204+ if arg_name == 'self' :
205+ is_method = True
206+ continue
207+ else :
208+ err = 'error in function: %s' % node .name
209+ err += '\n missing typedef: %s' % arg .id
210+ raise SyntaxError (err )
176211
177212 arg_type = args_typedefs [arg_name ]
178213 a = '%s %s' % (arg_name , arg_type )
@@ -202,11 +237,16 @@ def _visit_function(self, node):
202237 args .append ( '%s ...%s' % (starargs , args_typedefs [starargs ]))
203238
204239 ####
240+ if is_method :
241+ assert self ._class_stack
242+ method = '(self *%s) ' % self ._class_stack [- 1 ].name
243+ else :
244+ method = ''
205245 out = []
206246 if return_type :
207- out .append ( self .indent () + 'func %s(%s) %s {\n ' % (node .name , ', ' .join (args ), return_type ) )
247+ out .append ( self .indent () + 'func %s%s (%s) %s {\n ' % (method , node .name , ', ' .join (args ), return_type ) )
208248 else :
209- out .append ( self .indent () + 'func %s(%s) {\n ' % (node .name , ', ' .join (args )) )
249+ out .append ( self .indent () + 'func %s%s (%s) {\n ' % (method , node .name , ', ' .join (args )) )
210250 self .push ()
211251
212252 if oargs :
@@ -283,6 +323,10 @@ def visit_While(self, node):
283323 def _inline_code_helper (self , s ):
284324 return s
285325
326+
327+
328+
329+
286330def main (script , insert_runtime = True ):
287331 if insert_runtime :
288332 dirname = os .path .dirname (os .path .abspath (__file__ ))
@@ -291,6 +335,7 @@ def main(script, insert_runtime=True):
291335 script = runtime + '\n ' + script
292336
293337 tree = ast .parse (script )
338+ #return GoGenerator().visit(tree)
294339 try :
295340 return GoGenerator ().visit (tree )
296341 except SyntaxError as err :
0 commit comments