diff --git a/integration_tests/test_array_01.py b/integration_tests/test_array_01.py new file mode 100644 index 0000000000..11edd649b2 --- /dev/null +++ b/integration_tests/test_array_01.py @@ -0,0 +1,21 @@ +from ltypes import i32, f64 + +def test_array_01(): + x: f64[3] = [1.0, 2.0, 3.0] + eps: f64 = 1e-16 + assert abs(x[1] - 1.0) < eps + assert abs(x[2] - 2.0) < eps + assert abs(x[3] - 3.0) < eps + +def test_array_02(): + x: i32[3] = [1, 2, 3] + eps: f64 = 1e-16 + assert abs(x[1] - 1) < eps + assert abs(x[2] - 2) < eps + assert abs(x[3] - 3) < eps + +def check(): + test_array_01() + test_array_02() + +check() diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 52426351b0..7d06e53133 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -935,6 +935,31 @@ class CommonVisitor : public AST::BaseVisitor { throw SemanticError("Casting " + rtype + " to complex is not Implemented", right->base.loc); } + } else if (is_assign && ASR::is_a(*right_type) && ASRUtils::is_array(left_type)) { + ASR::ttype_t* list_type = ASR::down_cast(right_type)->m_type; + if (ASRUtils::is_real(*left_type) && ASRUtils::is_real(*list_type)) { + if (ASR::is_a(*right)) { + ASR::ListConstant_t* list = ASR::down_cast(right); + ASR::expr_t **m_args = list->m_args; + size_t n_args = list->n_args; + return (ASR::expr_t *)ASR::make_ArrayConstant_t( + al, right->base.loc, m_args, n_args, list_type); + } else { + throw SemanticError("Only assigning real ConstantList is supported for now", + right->base.loc); + } + } else if (ASRUtils::is_integer(*left_type) && ASRUtils::is_integer(*list_type)) { + if (ASR::is_a(*right)) { + ASR::ListConstant_t* list = ASR::down_cast(right); + ASR::expr_t **m_args = list->m_args; + size_t n_args = list->n_args; + return (ASR::expr_t *)ASR::make_ArrayConstant_t( + al, right->base.loc, m_args, n_args, list_type); + } else { + throw SemanticError("Only assigning Integer ConstantList is supported for now", + right->base.loc); + } + } } if (!is_assign) { // This will only be used for BinOp if (ASRUtils::is_logical(*left_type) && ASRUtils::is_logical(*right_type)) {