Skip to content
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

Allow subscripts to have methods called #2735

Merged
merged 3 commits into from
Jul 12, 2024
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 @@ -796,6 +796,7 @@ RUN(NAME generics_array_03 LABELS cpython llvm llvm_jit c)
RUN(NAME generics_list_01 LABELS cpython llvm llvm_jit c)
RUN(NAME test_statistics_01 LABELS cpython llvm llvm_jit NOFAST)
RUN(NAME test_statistics_02 LABELS cpython llvm llvm_jit NOFAST REQ_PY_VER 3.10)
RUN(NAME test_attributes LABELS cpython llvm llvm_jit)
RUN(NAME test_str_attributes LABELS cpython llvm llvm_jit c)
RUN(NAME kwargs_01 LABELS cpython llvm llvm_jit c NOFAST)
RUN(NAME def_func_01 LABELS cpython llvm llvm_jit c)
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/test_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def test_attributes() -> None:
a: i32 = 10
assert a.bit_length() == 4

b: str = 'abc'
assert b.upper() == 'ABC'

c: list[i32] = [10, 20, 30]
assert c[0].bit_length() == 4
assert c.index(10) == 0

test_attributes()
11 changes: 11 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8040,6 +8040,17 @@ we will have to use something else.
}
handle_builtin_attribute(dict_expr, at->m_attr, loc, eles);
return;
} else if (AST::is_a<AST::Subscript_t>(*at->m_value)) {
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(at->m_value);
visit_Subscript(*s);
ASR::expr_t *subscript_expr = ASR::down_cast<ASR::expr_t>(tmp);
Vec<ASR::expr_t*> eles;
eles.reserve(al, args.size());
for (size_t i = 0; i < args.size(); i++) {
eles.push_back(al, args[i].m_value);
}
handle_builtin_attribute(subscript_expr, at->m_attr, loc, eles);
return;
} else {
throw SemanticError("Only Name type and constant integers supported in Call", loc);
}
Expand Down
Loading