MCPcopy Create free account
hub / github.com/battlecode/battlecode-2018 / Function

Class Function

bindings/frankenswig/function.py:3–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1from .helpers import *
2
3class Function(object):
4 def __init__(self, type, name, args, body='', docs=''):
5 self.type = type
6 self.name = name
7 self.args = args
8 self.body = body
9 self.docs = docs
10
11 def to_swig(self):
12 result = s(f'''\
13 {self.type.to_swig()} {self.name}({', '.join(a.to_swig() for a in self.args)});
14 ''')
15 return result
16
17 def to_c(self):
18 return f'''{doxygen(self.docs)}{self.type.to_c()} {self.name}({', '.join(a.to_c() for a in self.args)});\n'''
19
20 def to_rust(self):
21 result = s(f'''\
22 #[no_mangle]
23 pub extern "C" fn {self.name}({', '.join(a.to_rust() for a in self.args)}) -> {self.type.rust} {{
24 let _default: {self.type.rust} = {self.type.default};
25 '''
26 )
27 result += s(self.body, indent=4)
28 result += '\n}\n'
29 return result
30
31 @staticmethod
32 def pyentry(type, args, pyname, docs):
33 pyargs = ', '.join(a.to_python() for a in args)
34 start = f'def {pyname}({pyargs}):\n'
35 hargs = args if len(args) > 0 and args[0].name != 'self' else args[1:]
36 mypy_hint = ', '.join(a.type.to_python() for a in hargs)
37 mypy_hint = f'# type: ({mypy_hint}) -> {type.to_python()}'
38 doc_hint = ''
39 for a in args:
40 doc_hint += f':type {a.name}: {a.type.to_python()}\n'
41 doc_hint += f':rtype: {type.to_python()}\n'
42 asserts = ''
43 for arg in args:
44 if arg.name == 'self':
45 continue
46 asserts += f'assert type({arg.name}) is {arg.type.to_python()}, "incorrect type of arg {arg.name}: should be {arg.type.to_python()}, is {{}}".format(type({arg.name}))\n'
47
48 docs = s(f"{mypy_hint}\n'''{docs}\n{doc_hint}'''\n{asserts}\n", indent=4)
49 return start + docs
50
51 def to_swig(self):
52 result = s(f'''\
53 %newobject {self.name};
54 {self.type.to_swig()} {self.name}({', '.join(a.to_swig() for a in self.args)});
55 ''')
56 return result
57
58 def to_python(self):
59 # note: we assume that error + null checking, etc. will occur on the rust side.
60 # (it'll probably be much faster there in any case.)

Callers 2

__init__Method · 0.85
constructorMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected