Skip to content

Commit

Permalink
Add compile-time support for dict.values (lcompilers#2661)
Browse files Browse the repository at this point in the history
* Implement `dict.values` for `DictConstant`

* Tests: Add tests

* Tests: Fix typing mistake

* Tests: Update references

* Uncomment check for modifying attribute

* Tests: Update references

* Delete tests/reference/asr-func_04-eef2656.stdout

* Tests: Move `print` before `assert`

* Tests: Update test
  • Loading branch information
kmr-srbh authored and assem2002 committed Apr 28, 2024
1 parent 1c9c095 commit 67f5be1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
22 changes: 22 additions & 0 deletions integration_tests/test_dict_keys_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,26 @@ def test_dict_keys_values():
assert len(k_2) == 3


# dict.values on dict constant
print({1: "a"}.values())
assert len({1: "a"}.values()) == 1

print({"a": 1, "b": 2, "c": 3}.values())
assert len({"a": 1, "b": 2, "c": 3}.values()) == 3

print({1: [1, 2, 3], 2: [4, 5, 6], 3: [7, 8, 9]}.values())
assert len({1: [1, 2, 3], 2: [4, 5, 6], 3: [7, 8, 9]}.values()) == 3

print({(1, 2): "a", (3, 4): "b", (5, 6): "c"}.values())
assert len({(1, 2): "a", (3, 4): "b", (5, 6): "c"}.values()) == 3

v_1: list[list[i32]] = {"list1": [1, 2, 3], "list2": [4, 5, 6], "list3": [7, 8, 9]}.values()
print(v_1)
assert len(v_1) == 3

v_2: list[str] = {(1, 2): "a", (3, 4): "b", (5, 6): "c"}.values()
print(v_2)
assert len(v_2) == 3


test_dict_keys_values()
13 changes: 9 additions & 4 deletions src/libasr/pass/intrinsic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4786,10 +4786,15 @@ static inline void verify_args(const ASR::IntrinsicElementalFunction_t& x, diag:
x.base.base.loc, diagnostics);
}

static inline ASR::expr_t *eval_dict_values(Allocator &/*al*/,
const Location &/*loc*/, ASR::ttype_t *, Vec<ASR::expr_t*>& /*args*/, diag::Diagnostics& /*diag*/) {
// TODO: To be implemented for DictConstant expression
return nullptr;
static inline ASR::expr_t *eval_dict_values(Allocator &al,
const Location &loc, ASR::ttype_t *t, Vec<ASR::expr_t*>& args, diag::Diagnostics& /*diag*/) {
if (args[0] == nullptr) {
return nullptr;
}
ASR::DictConstant_t* cdict = ASR::down_cast<ASR::DictConstant_t>(args[0]);

return ASRUtils::EXPR(ASR::make_ListConstant_t(al, loc,
cdict->m_values, cdict->n_values, t));
}

static inline ASR::asr_t* create_DictValues(Allocator& al, const Location& loc,
Expand Down

0 comments on commit 67f5be1

Please sign in to comment.