@@ -22,6 +22,7 @@ struct AttributeHandler {
2222 {" list@append" , &eval_list_append},
2323 {" list@remove" , &eval_list_remove},
2424 {" list@count" , &eval_list_count},
25+ {" list@index" , &eval_list_index},
2526 {" list@clear" , &eval_list_clear},
2627 {" list@insert" , &eval_list_insert},
2728 {" list@pop" , &eval_list_pop},
@@ -149,6 +150,32 @@ struct AttributeHandler {
149150 return make_ListCount_t (al, loc, s, args[0 ], to_type, nullptr );
150151 }
151152
153+ static ASR::asr_t * eval_list_index (ASR::expr_t *s, Allocator &al, const Location &loc,
154+ Vec<ASR::expr_t *> &args, diag::Diagnostics &diag) {
155+ if (args.size () != 1 ) {
156+ throw SemanticError (" index() takes exactly one argument" ,
157+ loc);
158+ }
159+ ASR::ttype_t *type = ASRUtils::expr_type (s);
160+ ASR::ttype_t *list_type = ASR::down_cast<ASR::List_t>(type)->m_type ;
161+ ASR::ttype_t *ele_type = ASRUtils::expr_type (args[0 ]);
162+ if (!ASRUtils::check_equal_type (ele_type, list_type)) {
163+ std::string fnd = ASRUtils::type_to_str_python (ele_type);
164+ std::string org = ASRUtils::type_to_str_python (list_type);
165+ diag.add (diag::Diagnostic (
166+ " Type mismatch in 'index', the types must be compatible" ,
167+ diag::Level::Error, diag::Stage::Semantic, {
168+ diag::Label (" type mismatch (found: '" + fnd + " ', expected: '" + org + " ')" ,
169+ {args[0 ]->base .loc })
170+ })
171+ );
172+ throw SemanticAbort ();
173+ }
174+ ASR::ttype_t *to_type = ASRUtils::TYPE (ASR::make_Integer_t (al, loc,
175+ 4 , nullptr , 0 ));
176+ return make_ListIndex_t (al, loc, s, args[0 ], to_type, nullptr );
177+ }
178+
152179 static ASR::asr_t * eval_list_insert (ASR::expr_t *s, Allocator &al, const Location &loc,
153180 Vec<ASR::expr_t *> &args, diag::Diagnostics &diag) {
154181 if (args.size () != 2 ) {
0 commit comments