Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum macro #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add enum macro #22

wants to merge 4 commits into from

Conversation

evie-calico
Copy link
Contributor

@evie-calico evie-calico commented Oct 13, 2024

In the simplest case, this is equivalent to a list of constants defined using rb:

; These are the same
enum Foo
  case Bar
  case Baz
end_enum

rsreset
def Foo_Bar rb
def Foo_Baz rb

However, this macro can also be used to define a tagged union in which structs with different layouts can be stored in the same memory.

enum Result
  case Ok
    bytes 16, Message
  case Err
    words 1, Type
end_enum

This is equivalent to the following set of structures:

struct Result_Ok
  bytes 16, Message
end_struct

struct Result_Err
  words 1, Type
end_struct

struct Result
  bytes 1, Discriminant
  ; Clearly `Result_Ok` is much larger in this example, but this could be hard to keep track of in a large enum.
  bytes sizeof_Result_Ok, Content
end_struct

A second argument may be passed to enum to determine what storage to use for the Discriminant field. This is passed to the internal struct invocation, so any alias of new_field will work (bytes, words, longs). When absent, it defaults to bytes.

@evie-calico evie-calico marked this pull request as draft October 13, 2024 17:54
@evie-calico evie-calico marked this pull request as ready for review October 13, 2024 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant