prettyprint expressions for ayin.rs

This commit is contained in:
me 2025-12-26 19:09:30 +02:00
parent 6c0b63b95c
commit 9680ba75a0
2 changed files with 42 additions and 1 deletions

View file

@ -262,6 +262,47 @@ impl Vector {
} }
} }
impl std::fmt::Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut string = "".to_string();
match self {
Expr::Value(value) => match value {
Value::Int(i) => string.push_str(&format!("{i}")),
Value::Float(f) => string.push_str(&format!("{f:2}")),
Value::String(s) => string.push_str(&format!("{s:?}")),
Value::Boolean(b) => string.push_str(&format!("{b}")),
Value::Closure { .. } => string.push_str("<fn>"),
_ => {}
},
Expr::Func(_) => string.push_str("<fn>"),
Expr::Record(map) => {
string.push_str("{");
for (key, val) in map {
string.push_str(&format!(" .{}: {},", key.0, val));
}
if map.len() > 0 {
string.push_str(" }");
} else {
string.push_str("}");
}
}
Expr::Vector(vec) => {
string.push_str("[");
for val in vec {
string.push_str(&format!(" {},", val));
}
if vec.len() > 0 {
string.push_str(" ]");
} else {
string.push_str("]");
}
}
_ => {}
}
f.write_str(&string)
}
}
pub const UNIT_VALUE: Value = Value::Record(Record(BTreeMap::new())); pub const UNIT_VALUE: Value = Value::Record(Record(BTreeMap::new()));
pub const UNIT: Expr = Expr::Value(UNIT_VALUE); pub const UNIT: Expr = Expr::Value(UNIT_VALUE);

View file

@ -6,7 +6,7 @@ fn main() {
Err(err) => println!("Error: {err:#?}"), Err(err) => println!("Error: {err:#?}"),
Ok(program) => match ayin::interpret::run(program, "main".into(), vec![]) { Ok(program) => match ayin::interpret::run(program, "main".into(), vec![]) {
Err(err) => println!("Error: {err:#?}"), Err(err) => println!("Error: {err:#?}"),
Ok(e) => println!("{e:#?}"), Ok(e) => println!("{e}"),
}, },
} }
} }