File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ from inspect import getfullargspec , getcallargs
2
+
3
+ # data-types
4
+
1
5
i32 = []
2
6
i64 = []
3
7
f32 = []
4
8
f64 = []
5
9
c32 = []
6
10
c64 = []
11
+
12
+ # overloading support
13
+
14
+ class OverloadedFunction :
15
+ """
16
+ A wrapper class for allowing overloading.
17
+ """
18
+ global_map = {}
19
+
20
+ def __init__ (self , func ):
21
+ self .func_name = func .__name__
22
+ f_list = self .global_map .get (func .__name__ , [])
23
+ f_list .append ((func , getfullargspec (func )))
24
+ self .global_map [func .__name__ ] = f_list
25
+
26
+ def __call__ (self , * args , ** kwargs ):
27
+ func_map_list = self .global_map .get (self .func_name , False )
28
+ if not func_map_list :
29
+ raise Exception ("Function not defined" )
30
+ for item in func_map_list :
31
+ func , key = item
32
+ try :
33
+ # This might fail for the cases when arguments don't match
34
+ ann_dict = getcallargs (func , * args , ** kwargs )
35
+ except TypeError :
36
+ continue
37
+ flag = True
38
+ for k , v in ann_dict .items ():
39
+ if not key .annotations .get (k , False ):
40
+ flag = False
41
+ break
42
+ else :
43
+ if type (v ) != key .annotations .get (k ):
44
+ flag = False
45
+ break
46
+ if flag :
47
+ return func (* args , ** kwargs )
48
+ raise Exception ("Function not found with matching signature" )
49
+
50
+
51
+ def overload (f ):
52
+ overloaded_f = OverloadedFunction (f )
53
+ return overloaded_f
You can’t perform that action at this time.
0 commit comments