From 8fc8b2e4d43edb09f466b9480f89888f0219ed11 Mon Sep 17 00:00:00 2001 From: Sebastian Fischer Date: Thu, 17 Oct 2024 15:58:42 +0200 Subject: [PATCH] ... --- src/callable/primitive/substitute.rs | 15 +++++++++++-- src/callable/primitive/type_reflection.rs | 16 +------------- src/context/core.rs | 26 +++++++++++++++++------ src/lang.rs | 17 +++++++++++++++ 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/callable/primitive/substitute.rs b/src/callable/primitive/substitute.rs index 89c96a9..32af9a7 100644 --- a/src/callable/primitive/substitute.rs +++ b/src/callable/primitive/substitute.rs @@ -55,9 +55,20 @@ impl Callable for PrimitiveSubstitute { return internal_err!(); }; - let Obj::Promise(_, expr, _) = args.try_get_named("expr")? else { - return internal_err!(); + let x = args.try_get_named("expr")?; + + let Obj::Promise(_, expr, _) = x else { + return Ok(x); }; + // match x { + // Obj::Promise(_, e) + // } + // if let Obj::Promise() + + // let Obj::Promise(_, expr, _) = args.try_get_named("expr")? else { + // return internal_err!(); + // } else { + // } fn recurse(exprs: ExprList, env: &Environment, paren: bool) -> ExprList { exprs diff --git a/src/callable/primitive/type_reflection.rs b/src/callable/primitive/type_reflection.rs index ccbf9e6..7e1cf3d 100644 --- a/src/callable/primitive/type_reflection.rs +++ b/src/callable/primitive/type_reflection.rs @@ -17,21 +17,7 @@ impl Callable for PrimitiveTypeOf { let mut args = Obj::List(args); let x = args.try_get_named("x")?.force(stack)?; - let t = match x { - Obj::Null => "null", - Obj::Vector(v) => match v { - Vector::Character(_) => "character", - Vector::Integer(_) => "integer", - Vector::Double(_) => "double", - Vector::Logical(_) => "logical", - }, - Obj::List(_) => "list", - Obj::Expr(_) => "expression", - Obj::Promise(..) => "promise", - Obj::Function(..) => "function", - Obj::Environment(..) => "environment", - }; - EvalResult::Ok(Obj::Vector(Vector::Character(vec![t.to_string()].into()))) + EvalResult::Ok(Obj::Vector(Vector::Character(vec![x.type_of()].into()))) } } diff --git a/src/context/core.rs b/src/context/core.rs index 954d0dd..15bb352 100644 --- a/src/context/core.rs +++ b/src/context/core.rs @@ -88,7 +88,7 @@ pub trait Context: std::fmt::Debug + std::fmt::Display { internal_err!() } } - // Avoid creating a new closure just to point to another, just reuse it + // Avoid creating a new promise just to point to another, just reuse it (k, Expr::Symbol(s)) => match self.env().get(s.clone()) { Ok(c @ Obj::Promise(..)) => { let k = k.map_or(OptionNA::NA, OptionNA::Some); @@ -110,11 +110,25 @@ pub trait Context: std::fmt::Debug + std::fmt::Display { } (k, v) => { let k = k.map_or(OptionNA::NA, OptionNA::Some); - if let Ok(elem) = self.eval(v) { - Ok(List::from(vec![(k, elem)]).iter_pairs()) - } else { - internal_err!() - } + let x = match v { + Expr::Bool(x) => Obj::Vector(vec![x].into()), + Expr::Integer(x) => Obj::Vector(vec![x].into()), + Expr::Number(x) => Obj::Vector(vec![x].into()), + Expr::String(x) => Obj::Vector(vec![x].into()), + _ => { + if let Ok(elem) = self.eval(v) { + elem + } else { + return internal_err!(); + } + } + }; + Ok(List::from(vec![(k, x)]).iter_pairs()) + + // if let Ok(elem) = self.eval(v) { + // } else { + // internal_err!() + // } } }) .collect::, _>>()? diff --git a/src/lang.rs b/src/lang.rs index c37c92e..0c0fddc 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -79,6 +79,23 @@ impl ViewMut for Obj { } impl Obj { + pub fn type_of(&self) -> String { + match self { + Obj::Null => "null", + Obj::Vector(v) => match v { + Vector::Character(_) => "character", + Vector::Integer(_) => "integer", + Vector::Double(_) => "double", + Vector::Logical(_) => "logical", + }, + Obj::List(_) => "list", + Obj::Expr(_) => "expression", + Obj::Promise(..) => "promise", + Obj::Function(..) => "function", + Obj::Environment(..) => "environment", + } + .to_string() + } pub fn with_visibility(self, visibility: bool) -> EvalResult { Signal::Return(self, visibility).into() }