cl-ast: Allow c-like enums to take an expr
This commit is contained in:
parent
fc80be5fcc
commit
d6c0a6cf1b
@ -183,7 +183,7 @@ pub struct Variant {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum VariantKind {
|
||||
Plain,
|
||||
CLike(u128),
|
||||
CLike(Box<Expr>),
|
||||
Tuple(Ty),
|
||||
Struct(Vec<StructMember>),
|
||||
}
|
||||
|
@ -40,7 +40,6 @@ impl_from! {
|
||||
// TODO: Struct members in struct
|
||||
}
|
||||
impl From for VariantKind {
|
||||
u128 => VariantKind::CLike,
|
||||
Ty => VariantKind::Tuple,
|
||||
// TODO: enum struct variants
|
||||
}
|
||||
|
@ -159,7 +159,10 @@ impl Interpret for Enum {
|
||||
for (idx, Variant { name, kind }) in variants.iter().enumerate() {
|
||||
match kind {
|
||||
VariantKind::Plain => env.insert(*name, Some(ConValue::Int(idx as _))),
|
||||
VariantKind::CLike(idx) => env.insert(*name, Some(ConValue::Int(*idx as _))),
|
||||
VariantKind::CLike(idx) => {
|
||||
let idx = idx.interpret(env)?;
|
||||
env.insert(*name, Some(idx))
|
||||
}
|
||||
VariantKind::Tuple(ty) => eprintln!("TODO: Enum-tuple variants: {ty}"),
|
||||
VariantKind::Struct(_) => eprintln!("TODO: Enum-struct members: {kind}"),
|
||||
}
|
||||
|
@ -603,13 +603,8 @@ impl Parse<'_> for VariantKind {
|
||||
const P: Parsing = Parsing::VariantKind;
|
||||
Ok(match p.peek_kind(P)? {
|
||||
TokenKind::Eq => {
|
||||
p.match_type(TokenKind::Eq, P)?;
|
||||
let tok = p.match_type(TokenKind::Literal, P)?;
|
||||
|
||||
VariantKind::CLike(match tok.data() {
|
||||
TokenData::Integer(i) => *i,
|
||||
_ => panic!("Expected token data for {tok:?} while parsing {P}"),
|
||||
})
|
||||
p.consume_peeked();
|
||||
VariantKind::CLike(Box::new(p.parse()?))
|
||||
}
|
||||
TokenKind::LCurly => VariantKind::Struct(delim(
|
||||
sep(StructMember::parse, TokenKind::Comma, TokenKind::RCurly, P),
|
||||
|
Loading…
x
Reference in New Issue
Block a user