From 88bbcf036f031f936bceec164d44876b1bd1bf36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20R=C3=B6ger?= Date: Sat, 11 Jan 2025 16:31:41 +0100 Subject: [PATCH] feat(core): improve printing - Expression Conversion - print/println --- lispers-core/src/lisp/expression.rs | 30 ++++++++++++++++++++++++++++- lispers-core/src/lisp/prelude.rs | 10 +++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lispers-core/src/lisp/expression.rs b/lispers-core/src/lisp/expression.rs index f741ad7..e7dbce6 100644 --- a/lispers-core/src/lisp/expression.rs +++ b/lispers-core/src/lisp/expression.rs @@ -198,6 +198,34 @@ impl From<(Expression, Expression)> for Expression { } } +impl From for Expression { + fn from(value: i64) -> Self { + Expression::Integer(value) + } +} + +impl From for Expression { + fn from(value: f64) -> Self { + Expression::Float(value) + } +} + +impl From for Expression { + fn from(value: String) -> Self { + Expression::String(value) + } +} + +impl From for Expression { + fn from(value: bool) -> Self { + if value { + Expression::True + } else { + Expression::Nil + } + } +} + impl TryFrom for i64 { type Error = EvalError; fn try_from(value: Expression) -> Result { @@ -321,7 +349,7 @@ impl Display for Expression { Expression::Symbol(s) => write!(f, "{}", s), Expression::Integer(i) => write!(f, "{}", i), Expression::Float(fl) => write!(f, "{}", fl), - Expression::String(s) => write!(f, "\"{}\"", s), + Expression::String(s) => write!(f, "{}", s), Expression::True => write!(f, "true"), Expression::Nil => write!(f, "nil"), } diff --git a/lispers-core/src/lisp/prelude.rs b/lispers-core/src/lisp/prelude.rs index 8176aa5..4fb30dc 100644 --- a/lispers-core/src/lisp/prelude.rs +++ b/lispers-core/src/lisp/prelude.rs @@ -213,10 +213,17 @@ pub fn prelude_set(env: &Environment, expr: Expression) -> Result Result { + let [e] = expr.try_into()?; + let e = eval(env, e)?; + println!("{}", e); + Ok(e) +} + pub fn prelude_print(env: &Environment, expr: Expression) -> Result { let [e] = expr.try_into()?; let e = eval(env, e)?; - println!("Prelude: {}", e); + print!("{}", 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("let".to_string(), Expression::Function(prelude_let)); 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("cons".to_string(), Expression::Function(prelude_cons)); layer.set("car".to_string(), Expression::Function(prelude_car));