Skip to content

[WIP] [PoC] lib.enum: forward property accesses from enum view to base enum#1426

Draft
whitequark wants to merge 1 commit intoamaranth-lang:mainfrom
whitequark:enum-predicates-proof-of-concept
Draft

[WIP] [PoC] lib.enum: forward property accesses from enum view to base enum#1426
whitequark wants to merge 1 commit intoamaranth-lang:mainfrom
whitequark:enum-predicates-proof-of-concept

Conversation

@whitequark
Copy link
Copy Markdown
Member

@whitequark whitequark commented Jun 27, 2024

Proof of concept PR for #1419.

Can be tested with:

from amaranth import *
from amaranth.lib import enum


class MyEnum(enum.Enum, shape=3):
    GetX1 = 0
    GetX2 = 1
    GetX4 = 2
    PutX1 = 3
    PutX2 = 4
    PutX4 = 5

    @property
    def is_get(self):
        return self in [MyEnum.GetX1, MyEnum.GetX2, MyEnum.GetX4]

    @property
    def is_put(self):
        return self in [MyEnum.PutX1, MyEnum.PutX2, MyEnum.PutX4]


sig = Signal(MyEnum)
print(sig.is_get)
print(sig.is_put)
print(sig.x)

This returns:

(proxy (array [True, True, True, False, False, False]) (sig sig))
(proxy (array [False, False, False, True, True, True]) (sig sig))
Traceback (most recent call last):
  File "/home/whitequark/Projects/amaranth/xenum.py", line 25, in <module>
    print(sig.x)
          ^^^^^
  File "/home/whitequark/Projects/amaranth/amaranth/lib/enum.py", line 317, in __getattr__
    raise AttributeError(f"{self!r} has no attribute {name!r}")
AttributeError: EnumView(MyEnum, (sig sig)) has no attribute 'x'

@whitequark whitequark marked this pull request as draft June 27, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant