Skip to content

Commit

Permalink
Allow subscripts to have methods called (#2735)
Browse files Browse the repository at this point in the history
* Add subscripts to callable types

* Add tests

* Remove C backend
  • Loading branch information
advikkabra authored Jul 12, 2024
1 parent 3d74ccb commit 44068ce
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
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

0 comments on commit 44068ce

Please sign in to comment.