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

support enum variants having specified values #29

Open
Gankra opened this issue Jun 30, 2024 · 0 comments
Open

support enum variants having specified values #29

Gankra opened this issue Jun 30, 2024 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Gankra
Copy link
Owner

Gankra commented Jun 30, 2024

The kdl-script parser supports enum variants having an assigned value (IntExpr)

/// A variant of an [`EnumDecl`].
#[derive(Debug, Clone)]
pub struct EnumVariant {
pub name: Ident,
/// Optional value this case is required to have
/// in its underlying integer representation.
pub val: Option<IntExpr>,
}

But the type system throws it out.

/// An enum variant
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EnumVariantTy {
pub name: Ident,
// pub val: LiteralExpr,
}


Once the type system supports it, the backends need to be changed to use it:

writeln!(f, "#[derive(Debug, Copy, Clone, PartialEq)]")?;
writeln!(f, "enum {} {{", enum_ty.name)?;
f.add_indent(1);
for variant in &enum_ty.variants {
let variant_name = &variant.name;
writeln!(f, "{variant_name},")?;
}
f.sub_indent(1);

writeln!(f, "typedef enum {} {{", enum_ty.name)?;
f.add_indent(1);
for variant in &enum_ty.variants {
let variant_name = &variant.name;
writeln!(f, "{}_{variant_name},", enum_ty.name)?;
}
f.sub_indent(1);
writeln!(f, "}} {};\n", enum_ty.name)?;


Arguably IntExpr shouldn't be used here, since it artificially clamps to i64. In general we don't have a good story for talking about arbitrary primitive values that rust (and kdl) can't express. But i64 is good enough to start with.

@Gankra Gankra added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jun 30, 2024
@Gankra Gankra changed the title Use enum variant values support enum variants having specified values Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant