cl-interpret: Tuple structs + fix tuple member access

This commit is contained in:
2025-02-22 03:31:27 -06:00
parent 697d139cfd
commit 7a8da33de9
4 changed files with 147 additions and 40 deletions

View File

@@ -6,7 +6,8 @@ use cl_ast::{format::FmtAdapter, ExprKind, Sym};
use super::{
builtin::Builtin,
error::{Error, IResult},
function::Function, Callable, Environment,
function::Function,
Callable, Environment,
};
use std::{collections::HashMap, ops::*, rc::Rc};
@@ -40,6 +41,8 @@ pub enum ConValue {
RangeInc(Integer, Integer),
/// A value of a product type
Struct(Box<(Sym, HashMap<Sym, ConValue>)>),
/// A value of a product type with anonymous members
TupleStruct(Box<(Sym, Box<[ConValue]>)>),
/// An entire namespace
Module(Box<HashMap<Sym, Option<ConValue>>>),
/// A quoted expression
@@ -298,6 +301,20 @@ impl std::fmt::Display for ConValue {
}
')'.fmt(f)
}
ConValue::TupleStruct(parts) => {
let (name, tuple) = parts.as_ref();
if !name.is_empty() {
write!(f, "{name}")?;
}
'('.fmt(f)?;
for (idx, element) in tuple.iter().enumerate() {
if idx > 0 {
", ".fmt(f)?
}
element.fmt(f)?
}
')'.fmt(f)
}
ConValue::Struct(parts) => {
let (name, map) = parts.as_ref();
use std::fmt::Write;