File tree 3 files changed +95
-1
lines changed
3 files changed +95
-1
lines changed Original file line number Diff line number Diff line change 18
18
"test_math1.py"
19
19
]
20
20
21
+ # At present we run these tests on cpython, later we should also move to lpython
22
+ test_cpython = [
23
+ "test_generics_01.py"
24
+ ]
25
+
26
+
21
27
def main ():
22
28
print ("Compiling..." )
23
29
for pyfile in tests :
@@ -30,7 +36,9 @@ def main():
30
36
if r != 0 :
31
37
print ("Command '%s' failed." % cmd )
32
38
sys .exit (1 )
39
+
33
40
print ("Running..." )
41
+ python_path = "src/runtime/ltypes"
34
42
for pyfile in tests :
35
43
basename = os .path .splitext (pyfile )[0 ]
36
44
cmd = "integration_tests/%s" % (basename )
@@ -39,7 +47,16 @@ def main():
39
47
if r != 0 :
40
48
print ("Command '%s' failed." % cmd )
41
49
sys .exit (1 )
42
- python_path = "src/runtime/ltypes"
50
+ cmd = "PYTHONPATH=%s python integration_tests/%s" % (python_path ,
51
+ pyfile )
52
+ print ("+ " + cmd )
53
+ r = os .system (cmd )
54
+ if r != 0 :
55
+ print ("Command '%s' failed." % cmd )
56
+ sys .exit (1 )
57
+
58
+ print ("Running cpython tests..." )
59
+ for pyfile in test_cpython :
43
60
cmd = "PYTHONPATH=%s python integration_tests/%s" % (python_path ,
44
61
pyfile )
45
62
print ("+ " + cmd )
Original file line number Diff line number Diff line change
1
+ from ltypes import overload
2
+
3
+ @overload
4
+ def foo (a : int , b : int ) -> int :
5
+ return a * b
6
+
7
+ @overload
8
+ def foo (a : int ) -> int :
9
+ return a ** 2
10
+
11
+ @overload
12
+ def foo (a : str ) -> str :
13
+ return "lpython-" + a
14
+
15
+ @overload
16
+ def test (a : int ) -> int :
17
+ return a + 10
18
+
19
+ @overload
20
+ def test (a : bool ) -> int :
21
+ if a :
22
+ return 10
23
+ return - 10
24
+
25
+
26
+ assert foo (2 ) == 4
27
+ assert foo (2 , 10 ) == 20
28
+ assert foo ("hello" ) == "lpython-hello"
29
+ assert test (10 ) == 20
30
+ assert test (False ) == - test (True ) and test (True ) == 10
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