Skip to content

Resolve "x: i32[3] = [1, 2, 3] fails" #870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
21 changes: 21 additions & 0 deletions integration_tests/test_array_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from ltypes import i32, f64

def test_array_01():
x: f64[3] = [1.0, 2.0, 3.0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is not consistent with CPython:
In CPython, the variable x will be of type list. But here as can be seen it is of type array!

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()
25 changes: 25 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,31 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
throw SemanticError("Casting " + rtype + " to complex is not Implemented",
right->base.loc);
}
} else if (is_assign && ASR::is_a<ASR::List_t>(*right_type) && ASRUtils::is_array(left_type)) {
ASR::ttype_t* list_type = ASR::down_cast<ASR::List_t>(right_type)->m_type;
if (ASRUtils::is_real(*left_type) && ASRUtils::is_real(*list_type)) {
if (ASR::is_a<ASR::ListConstant_t>(*right)) {
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(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<ASR::ListConstant_t>(*right)) {
ASR::ListConstant_t* list = ASR::down_cast<ASR::ListConstant_t>(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)) {
Expand Down