def pycode(self): global _in_pro, _in_function, _classes_used pars = [] keys = [] extra = [] method = False # import ipdb; ipdb.set_trace() plist = self.subroutine_body.parameter_list if self.subroutine_body.method_name.DCOLON is None: name = pycode(self.subroutine_body.method_name) if name[-8:].lower() == '__define': # class definition if plist: print "Class definition with parameters -- probably not allowed!" return ClassDefinition(name[:-8], find_structure_body(self)) else: # Method definition classname, name = map(pycode, self.subroutine_body.method_name.IDENTIFIER) # methodname = pycode(methodname) # classname = pycode(classname) method = True if plist: for p in plist.get_items(): if p.EXTRA: extra = pycode(p.IDENTIFIER) if p.IDENTIFIER else "extra" continue if p.EQUALS: keys.append((pycode(p.IDENTIFIER[0]), pycode(p.IDENTIFIER[1]))) else: pars.append(pycode(p.IDENTIFIER)) fmap = i2py_map.get_subroutine_map(name) if not fmap: inpars = range(1, len(pars)+1) outpars = inpars inkeys = [ k[0] for k in keys ] outkeys = inkeys if self.PRO: _in_pro = True if not fmap: fmap = i2py_map.map_pro(name, inpars=inpars, outpars=outpars, inkeys=inkeys, outkeys=outkeys, method=method) elif self.FUNCTION: _in_function = True if not fmap: fmap = i2py_map.map_func(name, inpars=inpars, inkeys=inkeys, method=method) else: raise RuntimeError("not PRO, not FUNCTION, then what?") try: header, body = fmap.pydef(pars, keys, extra=extra) except i2py_map.Error, e: error.mapping_error(str(e), self.lineno) header, body = '', ''
map_pro('PRINT', inpars=range(1, 101), noptional=100, inkeys=['FORMAT'], callfunc=(lambda i, o: 'print( ' + ', '.join(i) + ')')) ################################################################################ # # Function maps # ################################################################################ ######################################################## # Things with different names in Python map_func('ABS', inpars=[1], pyname='absolute') map_func('ACOS', inpars=[1], pyname='arccos') map_func('ASIN', inpars=[1], pyname='arcsin') map_func('ALOG', inpars=[1], pyname='log') map_func('ALOG10', inpars=[1], pyname='log10') # ATAN with two arguments is a separate function in numpy # ATAN with complex argument and /phase is also special def map_atan(i, o): if len(i) == 1: return 'arctan(%s)' % (i[0]) elif len(i) == 2: if re.match('PHASE', i[1]): return 'arctan2(%s.imag, %s.real)' % (i[0], i[0]) else:
map_pro("CATCH", inpars=[1], inkeys=["CANCEL"], callfunc=lambda i, o: "# CATCH, %s" % i) map_pro( "PRINT", inpars=range(1, 101), noptional=100, inkeys=["FORMAT"], callfunc=(lambda i, o: "print " + ", ".join(i)) ) ################################################################################ # # Function maps # ################################################################################ ######################################################## # Things with different names in Python map_func("ABS", inpars=[1], pyname="absolute") map_func("ACOS", inpars=[1], pyname="arccos") map_func("ASIN", inpars=[1], pyname="arcsin") map_func("ALOG", inpars=[1], pyname="log") map_func("ALOG10", inpars=[1], pyname="log10") # ATAN with two arguments is a separate function in numpy # ATAN with complex argument and /phase is also special def map_atan(i, o): if len(i) == 1: return "arctan(%s)" % (i[0]) elif len(i) == 2: if re.match("PHASE", i[1]): return "arctan2(%s.imag, %s.real)" % (i[0], i[0]) else: return "arctan2(%s, %s)" % (i[0], i[1])