Skip to content

Commit 64c84e6

Browse files
committed
Add support for dict pop in python_attribute_eval.h
1 parent 49eb3eb commit 64c84e6

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/libasr/asr_utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ static inline ASR::ttype_t* expr_type(const ASR::expr_t *f)
136136
case ASR::exprType::ListItem: { return ((ASR::ListItem_t*)f)->m_type; }
137137
case ASR::exprType::ListSection: { return ((ASR::ListSection_t*)f)->m_type; }
138138
case ASR::exprType::ListPop: { return ((ASR::ListPop_t*)f)->m_type; }
139+
case ASR::exprType::DictPop: { return ((ASR::DictPop_t*)f)->m_type; }
139140
default : throw LFortranException("Not implemented");
140141
}
141142
}
@@ -309,6 +310,7 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
309310
case ASR::exprType::ListItem: { return ASR::down_cast<ASR::ListItem_t>(f)->m_value; }
310311
case ASR::exprType::ListSection: { return ASR::down_cast<ASR::ListSection_t>(f)->m_value; }
311312
case ASR::exprType::ListPop: { return ASR::down_cast<ASR::ListPop_t>(f)->m_value; }
313+
case ASR::exprType::DictPop: { return ASR::down_cast<ASR::DictPop_t>(f)->m_value; }
312314
case ASR::exprType::DictItem: // Drop through
313315
case ASR::exprType::ArrayConstant: // Drop through
314316
case ASR::exprType::IntegerConstant: // Drop through

src/lpython/semantics/python_attribute_eval.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ struct AttributeHandler {
2424
{"list@pop", &eval_list_pop},
2525
{"set@add", &eval_set_add},
2626
{"set@remove", &eval_set_remove},
27-
{"dict@get", &eval_dict_get}
27+
{"dict@get", &eval_dict_get},
28+
{"dict@pop", &eval_dict_pop}
2829
};
2930
}
3031

@@ -269,6 +270,29 @@ struct AttributeHandler {
269270
return make_DictItem_t(al, loc, s, args[0], def, value_type);
270271
}
271272

273+
static ASR::asr_t* eval_dict_pop(ASR::symbol_t *s, Allocator &al, const Location &loc,
274+
Vec<ASR::expr_t*> &args, diag::Diagnostics &diag) {
275+
if (args.size() != 1) {
276+
throw SemanticError("'pop' takes only one argument for now", loc);
277+
}
278+
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(s);
279+
ASR::ttype_t *type = v->m_type;
280+
ASR::ttype_t *key_type = ASR::down_cast<ASR::Dict_t>(type)->m_key_type;
281+
ASR::ttype_t *value_type = ASR::down_cast<ASR::Dict_t>(type)->m_value_type;
282+
if (!ASRUtils::check_equal_type(ASRUtils::expr_type(args[0]), key_type)) {
283+
std::string ktype = ASRUtils::type_to_str_python(ASRUtils::expr_type(args[0]));
284+
std::string totype = ASRUtils::type_to_str_python(key_type);
285+
diag.add(diag::Diagnostic(
286+
"Type mismatch in pop's key value, the types must be compatible",
287+
diag::Level::Error, diag::Stage::Semantic, {
288+
diag::Label("type mismatch (found: '" + ktype + "', expected: '" + totype + "')",
289+
{args[0]->base.loc})
290+
})
291+
);
292+
throw SemanticAbort();
293+
}
294+
return make_DictPop_t(al, loc, s, args[0], value_type, nullptr);
295+
}
272296

273297
}; // AttributeHandler
274298

0 commit comments

Comments
 (0)