diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index a673e0f515..88aaf52961 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -413,6 +413,11 @@ RUN(NAME variable_decl_03 LABELS cpython llvm c) RUN(NAME array_expr_01 LABELS cpython llvm c) RUN(NAME array_expr_02 LABELS cpython llvm c NOFAST) RUN(NAME array_expr_03 LABELS cpython llvm c) +RUN(NAME array_expr_04 LABELS cpython llvm c) +RUN(NAME array_expr_05 LABELS cpython llvm c) +RUN(NAME array_expr_06 LABELS cpython llvm c) +RUN(NAME array_expr_07 LABELS cpython llvm c) +RUN(NAME array_expr_08 LABELS cpython llvm c) RUN(NAME array_size_01 LABELS cpython llvm c) RUN(NAME array_size_02 LABELS cpython llvm c) RUN(NAME array_01 LABELS cpython llvm wasm c) diff --git a/integration_tests/array_expr_04.py b/integration_tests/array_expr_04.py new file mode 100644 index 0000000000..c14e4cc988 --- /dev/null +++ b/integration_tests/array_expr_04.py @@ -0,0 +1,35 @@ +from lpython import i8, i16, i32, i64 +from numpy import int8, int16, int32, int64, array + +def g(): + a8: i8[4] = array([127, -127, 3, 111], dtype=int8) + a16: i16[4] = array([127, -127, 3, 111], dtype=int16) + a32: i32[4] = array([127, -127, 3, 111], dtype=int32) + a64: i64[4] = array([127, -127, 3, 111], dtype=int64) + + print(a8) + print(a16) + print(a32) + print(a64) + + assert (a8[0] == i8(127)) + assert (a8[1] == i8(-127)) + assert (a8[2] == i8(3)) + assert (a8[3] == i8(111)) + + assert (a16[0] == i16(127)) + assert (a16[1] == i16(-127)) + assert (a16[2] == i16(3)) + assert (a16[3] == i16(111)) + + assert (a32[0] == i32(127)) + assert (a32[1] == i32(-127)) + assert (a32[2] == i32(3)) + assert (a32[3] == i32(111)) + + assert (a64[0] == i64(127)) + assert (a64[1] == i64(-127)) + assert (a64[2] == i64(3)) + assert (a64[3] == i64(111)) + +g() diff --git a/integration_tests/array_expr_05.py b/integration_tests/array_expr_05.py new file mode 100644 index 0000000000..8736470c71 --- /dev/null +++ b/integration_tests/array_expr_05.py @@ -0,0 +1,31 @@ +from lpython import u8, u16, u32, u64 +from numpy import uint8, uint16, uint32, uint64, array + +def g(): + a8: u8[3] = array([127, 3, 111], dtype=uint8) + a16: u16[3] = array([127, 3, 111], dtype=uint16) + a32: u32[3] = array([127, 3, 111], dtype=uint32) + a64: u64[3] = array([127, 3, 111], dtype=uint64) + + print(a8) + print(a16) + print(a32) + print(a64) + + assert (a8[0] == u8(127)) + assert (a8[1] == u8(3)) + assert (a8[2] == u8(111)) + + assert (a16[0] == u16(127)) + assert (a16[1] == u16(3)) + assert (a16[2] == u16(111)) + + assert (a32[0] == u32(127)) + assert (a32[1] == u32(3)) + assert (a32[2] == u32(111)) + + assert (a64[0] == u64(127)) + assert (a64[1] == u64(3)) + assert (a64[2] == u64(111)) + +g() diff --git a/integration_tests/array_expr_06.py b/integration_tests/array_expr_06.py new file mode 100644 index 0000000000..b6dc397d87 --- /dev/null +++ b/integration_tests/array_expr_06.py @@ -0,0 +1,21 @@ +from lpython import f32, f64 +from numpy import float32, float64, array + +def g(): + a32: f32[4] = array([127, -127, 3, 111], dtype=float32) + a64: f64[4] = array([127, -127, 3, 111], dtype=float64) + + print(a32) + print(a64) + + assert (abs(a32[0] - f32(127)) <= f32(1e-5)) + assert (abs(a32[1] - f32(-127)) <= f32(1e-5)) + assert (abs(a32[2] - f32(3)) <= f32(1e-5)) + assert (abs(a32[3] - f32(111)) <= f32(1e-5)) + + assert (abs(a64[0] - f64(127)) <= 1e-5) + assert (abs(a64[1] - f64(-127)) <= 1e-5) + assert (abs(a64[2] - f64(3)) <= 1e-5) + assert (abs(a64[3] - f64(111)) <= 1e-5) + +g() diff --git a/integration_tests/array_expr_07.py b/integration_tests/array_expr_07.py new file mode 100644 index 0000000000..598a7fcb64 --- /dev/null +++ b/integration_tests/array_expr_07.py @@ -0,0 +1,21 @@ +from lpython import c32, c64, f32 +from numpy import complex64, complex128, array + +def g(): + a32: c32[4] = array([127, -127, 3, 111], dtype=complex64) + a64: c64[4] = array([127, -127, 3, 111], dtype=complex128) + + print(a32) + print(a64) + + assert (abs(a32[0] - c32(127)) <= f32(1e-5)) + assert (abs(a32[1] - c32(-127)) <= f32(1e-5)) + assert (abs(a32[2] - c32(3)) <= f32(1e-5)) + assert (abs(a32[3] - c32(111)) <= f32(1e-5)) + + assert (abs(a64[0] - c64(127)) <= 1e-5) + assert (abs(a64[1] - c64(-127)) <= 1e-5) + assert (abs(a64[2] - c64(3)) <= 1e-5) + assert (abs(a64[3] - c64(111)) <= 1e-5) + +g() diff --git a/integration_tests/array_expr_08.py b/integration_tests/array_expr_08.py new file mode 100644 index 0000000000..ba0cf0dcb7 --- /dev/null +++ b/integration_tests/array_expr_08.py @@ -0,0 +1,14 @@ +from lpython import i1 +from numpy import bool_, array + +def g(): + a1: i1[4] = array([0, -127, 0, 111], dtype=bool_) + + print(a1) + + assert not a1[0] + assert a1[1] + assert not a1[2] + assert a1[3] + +g() diff --git a/src/libasr/asr_utils.h b/src/libasr/asr_utils.h index 3d0549c2c9..86a2ee5d26 100644 --- a/src/libasr/asr_utils.h +++ b/src/libasr/asr_utils.h @@ -408,6 +408,9 @@ static inline std::string type_to_str(const ASR::ttype_t *t) case ASR::ttypeType::Integer: { return "integer"; } + case ASR::ttypeType::UnsignedInteger: { + return "unsigned integer"; + } case ASR::ttypeType::Real: { return "real"; } diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 08b8d71bfa..4d10ff965c 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -510,7 +510,22 @@ class CommonVisitor : public AST::BaseVisitor { std::map imported_functions; std::map numpy2lpythontypes = { + {"bool", "bool"}, + {"bool_", "bool"}, {"int8", "i8"}, + {"int16", "i16"}, + {"int32", "i32"}, + {"int64", "i64"}, + {"uint8", "u8"}, + {"uint16", "u16"}, + {"uint32", "u32"}, + {"uint64", "u64"}, + {"float32", "f32"}, + {"float64", "f64"}, + {"float_", "f64"}, + {"complex64", "c32"}, + {"complex128", "c64"}, + {"complex_", "c64"}, }; CommonVisitor(Allocator &al, LocationManager &lm, SymbolTable *symbol_table, @@ -920,20 +935,20 @@ class CommonVisitor : public AST::BaseVisitor { if (var_sym->m_type->type == ASR::ttypeType::TypeParameter) { ASR::TypeParameter_t *type_param = ASR::down_cast(var_sym->m_type); type = ASRUtils::TYPE(ASR::make_TypeParameter_t(al, loc, type_param->m_param)); - return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); + type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); } } else { ASR::symbol_t *der_sym = ASRUtils::symbol_get_past_external(s); if( der_sym ) { if ( ASR::is_a(*der_sym) ) { type = ASRUtils::TYPE(ASR::make_Struct_t(al, loc, s)); - return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); + type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); } else if( ASR::is_a(*der_sym) ) { type = ASRUtils::TYPE(ASR::make_Enum_t(al, loc, s)); - return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); + type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); } else if( ASR::is_a(*der_sym) ) { type = ASRUtils::TYPE(ASR::make_Union_t(al, loc, s)); - return ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); + type = ASRUtils::make_Array_t_util(al, loc, type, dims.p, dims.size(), abi, is_argument); } } } @@ -7589,10 +7604,9 @@ class BodyVisitor : public CommonVisitor { tmp = ASR::make_UnsignedIntegerBitNot_t(al, x.base.base.loc, operand, operand_type, value); return; } else if( call_name == "array" ) { + parse_args(x, args); ASR::ttype_t* type = nullptr; - if( x.n_keywords == 0 ) { - parse_args(x, args); - } else { + if( x.n_keywords > 0) { args.reserve(al, 1); visit_expr_list(x.m_args, x.n_args, args); if( x.n_keywords > 1 ) { diff --git a/src/lpython/semantics/python_comptime_eval.h b/src/lpython/semantics/python_comptime_eval.h index 1780e50059..ac609af193 100644 --- a/src/lpython/semantics/python_comptime_eval.h +++ b/src/lpython/semantics/python_comptime_eval.h @@ -28,7 +28,7 @@ struct ProceduresDatabase { "complex64", "complex128", "int8", "exp", "exp2", "uint8", "uint16", "uint32", "uint64", - "size"}}, + "size", "bool_"}}, {"math", {"sin", "cos", "tan", "asin", "acos", "atan", "exp", "exp2", "expm1"}},