feat(core): improve printing

- Expression Conversion
- print/println
This commit is contained in:
Jonas Röger 2025-01-11 16:31:41 +01:00
parent ad0792dcd3
commit 88bbcf036f
Signed by: jonas
GPG Key ID: 4000EB35E1AE0F07
2 changed files with 38 additions and 2 deletions

View File

@ -198,6 +198,34 @@ impl From<(Expression, Expression)> for Expression {
} }
} }
impl From<i64> for Expression {
fn from(value: i64) -> Self {
Expression::Integer(value)
}
}
impl From<f64> for Expression {
fn from(value: f64) -> Self {
Expression::Float(value)
}
}
impl From<String> for Expression {
fn from(value: String) -> Self {
Expression::String(value)
}
}
impl From<bool> for Expression {
fn from(value: bool) -> Self {
if value {
Expression::True
} else {
Expression::Nil
}
}
}
impl TryFrom<Expression> for i64 { impl TryFrom<Expression> for i64 {
type Error = EvalError; type Error = EvalError;
fn try_from(value: Expression) -> Result<i64, Self::Error> { fn try_from(value: Expression) -> Result<i64, Self::Error> {
@ -321,7 +349,7 @@ impl Display for Expression {
Expression::Symbol(s) => write!(f, "{}", s), Expression::Symbol(s) => write!(f, "{}", s),
Expression::Integer(i) => write!(f, "{}", i), Expression::Integer(i) => write!(f, "{}", i),
Expression::Float(fl) => write!(f, "{}", fl), Expression::Float(fl) => write!(f, "{}", fl),
Expression::String(s) => write!(f, "\"{}\"", s), Expression::String(s) => write!(f, "{}", s),
Expression::True => write!(f, "true"), Expression::True => write!(f, "true"),
Expression::Nil => write!(f, "nil"), Expression::Nil => write!(f, "nil"),
} }

View File

@ -213,10 +213,17 @@ pub fn prelude_set(env: &Environment, expr: Expression) -> Result<Expression, Ev
} }
} }
pub fn prelude_println(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
let [e] = expr.try_into()?;
let e = eval(env, e)?;
println!("{}", e);
Ok(e)
}
pub fn prelude_print(env: &Environment, expr: Expression) -> Result<Expression, EvalError> { pub fn prelude_print(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
let [e] = expr.try_into()?; let [e] = expr.try_into()?;
let e = eval(env, e)?; let e = eval(env, e)?;
println!("Prelude: {}", e); print!("{}", e);
Ok(e) Ok(e)
} }
@ -270,6 +277,7 @@ pub fn mk_prelude(layer: &mut EnvironmentLayer) {
layer.set("not".to_string(), Expression::Function(prelude_not)); layer.set("not".to_string(), Expression::Function(prelude_not));
layer.set("let".to_string(), Expression::Function(prelude_let)); layer.set("let".to_string(), Expression::Function(prelude_let));
layer.set("set".to_string(), Expression::Function(prelude_set)); layer.set("set".to_string(), Expression::Function(prelude_set));
layer.set("println".to_string(), Expression::Function(prelude_println));
layer.set("print".to_string(), Expression::Function(prelude_print)); layer.set("print".to_string(), Expression::Function(prelude_print));
layer.set("cons".to_string(), Expression::Function(prelude_cons)); layer.set("cons".to_string(), Expression::Function(prelude_cons));
layer.set("car".to_string(), Expression::Function(prelude_car)); layer.set("car".to_string(), Expression::Function(prelude_car));