Skip to content

Commit 44068ce

Browse files
authored
Allow subscripts to have methods called (#2735)
* Add subscripts to callable types * Add tests * Remove C backend
1 parent 3d74ccb commit 44068ce

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ RUN(NAME generics_array_03 LABELS cpython llvm llvm_jit c)
796796
RUN(NAME generics_list_01 LABELS cpython llvm llvm_jit c)
797797
RUN(NAME test_statistics_01 LABELS cpython llvm llvm_jit NOFAST)
798798
RUN(NAME test_statistics_02 LABELS cpython llvm llvm_jit NOFAST REQ_PY_VER 3.10)
799+
RUN(NAME test_attributes LABELS cpython llvm llvm_jit)
799800
RUN(NAME test_str_attributes LABELS cpython llvm llvm_jit c)
800801
RUN(NAME kwargs_01 LABELS cpython llvm llvm_jit c NOFAST)
801802
RUN(NAME def_func_01 LABELS cpython llvm llvm_jit c)

integration_tests/test_attributes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def test_attributes() -> None:
2+
a: i32 = 10
3+
assert a.bit_length() == 4
4+
5+
b: str = 'abc'
6+
assert b.upper() == 'ABC'
7+
8+
c: list[i32] = [10, 20, 30]
9+
assert c[0].bit_length() == 4
10+
assert c.index(10) == 0
11+
12+
test_attributes()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8040,6 +8040,17 @@ we will have to use something else.
80408040
}
80418041
handle_builtin_attribute(dict_expr, at->m_attr, loc, eles);
80428042
return;
8043+
} else if (AST::is_a<AST::Subscript_t>(*at->m_value)) {
8044+
AST::Subscript_t *s = AST::down_cast<AST::Subscript_t>(at->m_value);
8045+
visit_Subscript(*s);
8046+
ASR::expr_t *subscript_expr = ASR::down_cast<ASR::expr_t>(tmp);
8047+
Vec<ASR::expr_t*> eles;
8048+
eles.reserve(al, args.size());
8049+
for (size_t i = 0; i < args.size(); i++) {
8050+
eles.push_back(al, args[i].m_value);
8051+
}
8052+
handle_builtin_attribute(subscript_expr, at->m_attr, loc, eles);
8053+
return;
80438054
} else {
80448055
throw SemanticError("Only Name type and constant integers supported in Call", loc);
80458056
}

0 commit comments

Comments
 (0)