Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ RUN(NAME test_builtin_float LABELS cpython llvm c)
RUN(NAME test_builtin_str_02 LABELS cpython llvm c)
RUN(NAME test_builtin_round LABELS cpython llvm c)
RUN(NAME test_builtin_divmod LABELS cpython llvm c)
RUN(NAME test_builtin_sum LABELS cpython llvm c)
RUN(NAME test_math1 LABELS cpython llvm c)
RUN(NAME test_math_02 LABELS cpython llvm)
RUN(NAME test_pass_compare LABELS cpython llvm c)
Expand Down
28 changes: 28 additions & 0 deletions integration_tests/test_builtin_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from ltypes import f32, f64, i32, i64

def test_sum():
arr_i32 :list[i32]
arr_i32 = [6, 12]
res_i32 :i32 = sum(arr_i32)
assert res_i32 == 18

arr_i64 :list[i64] = [i64(600000000000), i64(1200000000000)]
res_i64 :i64 = sum(arr_i64)
assert res_i64 == i64(1800000000000)

eps1 :f32 = f32(1e-12)
x :f32 = f32(12.5)
y :f32 = f32(6.5)
arr_f32 :list[f32]
arr_f32 = [x, y]
res_f32 :f32 = sum(arr_f32)
assert abs(res_f32 - f32(19.0)) < eps1

eps2: f64 = 1e-12
arr_f64: list[f64]
arr_f64 = [12.6, 6.4]
res_f64 :f64 = sum(arr_f64)
assert abs(res_f64 - 19.0) < eps2


test_sum()
1 change: 1 addition & 0 deletions src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct PythonIntrinsicProcedures {
{"_mod", {m_builtin, &eval__mod}},
{"max" , {m_builtin , &eval_max}},
{"min" , {m_builtin , &eval_min}},
{"sum" , {m_builtin , &not_implemented}},
// The following functions for string methods are not used
// for evaluation.
{"_lpython_str_capitalize", {m_builtin, &not_implemented}},
Expand Down
54 changes: 54 additions & 0 deletions src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,60 @@ def pow(x: bool, y: bool) -> i32:
def pow(c: c32, y: i32) -> c32:
return c**c32(y)

# sum
# supported data types: i32, i64, f32, f64

@overload
def sum(arr: list[i32]) -> i32:
"""
Sum of the elements of `arr`.
"""
sum: i32
sum = 0

i: i32
for i in range(len(arr)):
sum += arr[i]
return sum

@overload
def sum(arr: list[i64]) -> i64:
"""
Sum of the elements of `arr`.
"""
sum: i64
sum = i64(0)

i: i32
for i in range(len(arr)):
sum += arr[i]
return sum

@overload
def sum(arr: list[f32]) -> f32:
"""
Sum of the elements of `arr`.
"""
sum: f32
sum = f32(0.0)

i: i32
for i in range(len(arr)):
sum += arr[i]
return sum

@overload
def sum(arr: list[f64]) -> f64:
"""
Sum of the elements of `arr`.
"""
sum: f64
sum = 0.0

i: i32
for i in range(len(arr)):
sum += arr[i]
return sum

def bin(n: i32) -> str:
"""
Expand Down
8 changes: 4 additions & 4 deletions src/runtime/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def ceil(x: f32) -> i32:
@overload
def fsum(arr: list[i32]) -> f64:
"""
Sum of the elements of `arr`.
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
Expand All @@ -115,7 +115,7 @@ def fsum(arr: list[i32]) -> f64:
@overload
def fsum(arr: list[i64]) -> f64:
"""
Sum of the elements of `arr`.
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
Expand All @@ -128,7 +128,7 @@ def fsum(arr: list[i64]) -> f64:
@overload
def fsum(arr: list[f32]) -> f64:
"""
Sum of the elements of `arr`.
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
Expand All @@ -141,7 +141,7 @@ def fsum(arr: list[f32]) -> f64:
@overload
def fsum(arr: list[f64]) -> f64:
"""
Sum of the elements of `arr`.
Floating-point sum of the elements of `arr`.
"""
sum: f64
sum = 0.0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_01_decl-39cf894.stdout",
"stdout_hash": "c2477224b2c01309d0c62db9e4d2168abc779fd2fb0c4874a4232f14",
"stdout_hash": "4409e567b0abf42b56cf4c70a66bf2e24bbeb61bc52338b26cb4840c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_01_decl-39cf894.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-array_02_decl-e8f6874.stdout",
"stdout_hash": "768f04d435a32db40ba224093967cf0acaca792a73c732728e66eee8",
"stdout_hash": "b2be9582bdf29f9c7dd1d480998ef8c7b5dbefba08d4f430e2316a7d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-array_02_decl-e8f6874.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-bindc_02-bc1a7ea.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-bindc_02-bc1a7ea.stdout",
"stdout_hash": "143c305a40ef34791bab7054403d254c11bd7f357a55e774177f5e1d",
"stdout_hash": "ba2fbb9012c13e01cc2b596c7f706c8078bd1b285a11c861350cc358",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-bindc_02-bc1a7ea.stdout

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/reference/asr-cast-435c233.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-cast-435c233.stdout",
"stdout_hash": "66617db02c3d585c8ac8f560de6438f64c2361dd52d3271a1df8376b",
"stdout_hash": "e8d7a21da9b18a3c2cec593f5dfdd82d28018d3f2a29f9299421c196",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-cast-435c233.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 97 {}) _lpython_main_program (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [f] [] [(SubroutineCall 1 f () [] ())] () Public .false. .false.), f: (Function (SymbolTable 2 {list: (ExternalSymbol 2 list 4 list lpython_builtin [] list Private), s: (Variable 2 s [] Local () () Default (Character 1 -2 () []) Source Public Required .false.), x: (Variable 2 x [] Local () () Default (List (Character 1 -2 () [])) Source Public Required .false.), y: (Variable 2 y [] Local () () Default (List (Character 1 -2 () [])) Source Public Required .false.)}) f (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [list list list] [] [(= (Var 2 s) (StringConstant "lpython" (Character 1 7 () [])) ()) (= (Var 2 x) (FunctionCall 2 list () [((Var 2 s))] (List (Character 1 -2 () [])) () ()) ()) (= (Var 2 y) (ListConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () [])) (StringConstant "c" (Character 1 1 () []))] (List (Character 1 1 () []))) ()) (= (Var 2 x) (FunctionCall 2 list () [((Var 2 y))] (List (Character 1 -2 () [])) () ()) ()) (= (Var 2 x) (FunctionCall 2 list () [((StringConstant "lpython" (Character 1 7 () [])))] (List (Character 1 -2 () [])) (ListConstant [(StringConstant "l" (Character 1 1 () [])) (StringConstant "p" (Character 1 1 () [])) (StringConstant "y" (Character 1 1 () [])) (StringConstant "t" (Character 1 1 () [])) (StringConstant "h" (Character 1 1 () [])) (StringConstant "o" (Character 1 1 () [])) (StringConstant "n" (Character 1 1 () []))] (List (Character 1 1 () []))) ()) ())] () Public .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 96 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
(TranslationUnit (SymbolTable 1 {_lpython_main_program: (Function (SymbolTable 105 {}) _lpython_main_program (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [f] [] [(SubroutineCall 1 f () [] ())] () Public .false. .false.), f: (Function (SymbolTable 2 {list: (ExternalSymbol 2 list 4 list lpython_builtin [] list Private), s: (Variable 2 s [] Local () () Default (Character 1 -2 () []) Source Public Required .false.), x: (Variable 2 x [] Local () () Default (List (Character 1 -2 () [])) Source Public Required .false.), y: (Variable 2 y [] Local () () Default (List (Character 1 -2 () [])) Source Public Required .false.)}) f (FunctionType [] () Source Implementation () .false. .false. .false. .false. .false. [] [] .false.) [list list list] [] [(= (Var 2 s) (StringConstant "lpython" (Character 1 7 () [])) ()) (= (Var 2 x) (FunctionCall 2 list () [((Var 2 s))] (List (Character 1 -2 () [])) () ()) ()) (= (Var 2 y) (ListConstant [(StringConstant "a" (Character 1 1 () [])) (StringConstant "b" (Character 1 1 () [])) (StringConstant "c" (Character 1 1 () []))] (List (Character 1 1 () []))) ()) (= (Var 2 x) (FunctionCall 2 list () [((Var 2 y))] (List (Character 1 -2 () [])) () ()) ()) (= (Var 2 x) (FunctionCall 2 list () [((StringConstant "lpython" (Character 1 7 () [])))] (List (Character 1 -2 () [])) (ListConstant [(StringConstant "l" (Character 1 1 () [])) (StringConstant "p" (Character 1 1 () [])) (StringConstant "y" (Character 1 1 () [])) (StringConstant "t" (Character 1 1 () [])) (StringConstant "h" (Character 1 1 () [])) (StringConstant "o" (Character 1 1 () [])) (StringConstant "n" (Character 1 1 () []))] (List (Character 1 1 () []))) ()) ())] () Public .false. .false.), lpython_builtin: (IntrinsicModule lpython_builtin), main_program: (Program (SymbolTable 104 {}) main_program [] [(SubroutineCall 1 _lpython_main_program () [] ())])}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-complex1-f26c460.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-complex1-f26c460.stdout",
"stdout_hash": "0a2d4585b50b51933fdfb874d94f21ff361ea2f0b04a37dc8dc32fc6",
"stdout_hash": "550c9766480cf97a621465ee015419264ed3d1350db6ae794d979069",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading