From 4d9b13f7a16ed1c94950360f66b2a61f14aa6b4b Mon Sep 17 00:00:00 2001 From: John Date: Sun, 23 Feb 2025 03:22:48 -0600 Subject: [PATCH] cl-interpret: Enums, pt 1: C was right the whole time!!1 --- compiler/cl-interpret/src/interpret.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/cl-interpret/src/interpret.rs b/compiler/cl-interpret/src/interpret.rs index 7528f82..8486967 100644 --- a/compiler/cl-interpret/src/interpret.rs +++ b/compiler/cl-interpret/src/interpret.rs @@ -151,8 +151,23 @@ impl Interpret for Struct { } } impl Interpret for Enum { - fn interpret(&self, _env: &mut Environment) -> IResult { - println!("TODO: {self}"); + fn interpret(&self, env: &mut Environment) -> IResult { + let Self { name, kind } = self; + if let EnumKind::Variants(variants) = kind { + env.push_frame(Sym::to_ref(name), Default::default()); + 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::Tuple(ty) => eprintln!("TODO: Enum-tuple variants: {ty}"), + VariantKind::Struct(_) => eprintln!("TODO: Enum-struct members: {kind}"), + } + } + let (frame, _) = env + .pop_frame() + .expect("Frame stack should remain balanced."); + env.insert(*name, Some(ConValue::Module(Box::new(frame)))); + } Ok(ConValue::Empty) } }