Skip to content

Commit

Permalink
A bit of clean up and improvements (#32)
Browse files Browse the repository at this point in the history
Implements:
- Conversions (`.into()`) that #31 has (without implementing expr tags).
- Basic `typeof` intrinsic (e.g. `'() typeof -> "list`)
  • Loading branch information
Vandesm14 authored May 2, 2024
1 parent ed6c22c commit f2828f8
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 300 deletions.
7 changes: 3 additions & 4 deletions stack-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ impl Engine {
}
Ok(context)
} else {
context.stack_push(Expr {
kind: ExprKind::Error(Error::new("unknown function".into())),
info: None,
})?;
context.stack_push(
ExprKind::Error(Error::new("unknown function".into())).into(),
)?;

Ok(context)
}
Expand Down
34 changes: 30 additions & 4 deletions stack-core/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ pub struct Expr {
pub info: Option<ExprInfo>,
}

impl From<ExprKind> for Expr {
fn from(value: ExprKind) -> Self {
Self {
info: None,
kind: value,
}
}
}

impl PartialEq for Expr {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -108,6 +117,26 @@ impl ExprKind {
x => x,
}
}

pub fn type_of(&self) -> &str {
match self {
ExprKind::Nil => "nil",
ExprKind::Error(_) => "error",

ExprKind::Boolean(_) => "boolean",
ExprKind::Integer(_) => "integer",
ExprKind::Float(_) => "float",
ExprKind::String(_) => "string",

ExprKind::Symbol(_) => "symbol",

ExprKind::Lazy(_) => "lazy",
ExprKind::List(_) => "list",
ExprKind::Record(_) => "record",

ExprKind::Fn(_) => "function",
}
}
}

impl PartialEq for ExprKind {
Expand Down Expand Up @@ -270,10 +299,7 @@ impl fmt::Display for ExprKind {
.chain(core::iter::repeat(", "))
.zip(x.iter())
.try_for_each(|(sep, (key, value))| {
let key = Expr {
info: None,
kind: ExprKind::Symbol(*key),
};
let key: Expr = ExprKind::Symbol(*key).into();
write!(f, "{sep}{key:#}: {value:#}")
})?;

Expand Down
Loading

0 comments on commit f2828f8

Please sign in to comment.