Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat: add name to function
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Nov 23, 2023
1 parent 1e102ee commit b10506a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Frame {
/// Closure function.
#[derive(Clone)]
pub struct Fun {
pub name: Expr,
pub parameters: Vec<Keyword>,
pub body: Expr,
}
Expand Down Expand Up @@ -207,6 +208,7 @@ fn apply_expand(apply: crate::Apply, environment: &Environment) -> Result<Value,
/// Expand fun expressions.
fn fun_expand(fun: crate::Fun, environment: &Environment) -> Result<Value, Expr> {
Ok(Value::Fun(Fun {
name: fun.name()?,
parameters: fun
.parameters()?
.elements()?
Expand All @@ -230,7 +232,8 @@ impl Expr {
// Base cases for expansion when it will just walk the tree. These
// are the cases where the expansion is recursive.
Expr::List(list) => Ok(Value::List(
/* elements: */ list.elements()?
/* elements: */
list.elements()?
.into_iter()
.map(|expr| expr.expand(environment))
.collect::<Result<Vec<_>, _>>()?,
Expand All @@ -240,7 +243,8 @@ impl Expr {
/* value: */ def.value()?.expand(environment)?.into(),
)),
Expr::Recur(recur) => Ok(Value::Recur(
/* arguments: */ recur
/* arguments: */
recur
.spine()?
.into_iter()
.map(|expr| expr.expand(environment))
Expand Down
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ define_ast!(Expr, {
define_builtin!(DefMacro, "defmacro*", 2);
define_builtin!(Def, "def*", 2);
define_builtin!(Recur, "recur");
define_builtin!(Fun, "fun*", 2);
define_builtin!(Fun, "fun*", 3);
define_builtin!(Quote, "'", 2);
define_builtin!(Apply, "apply");

Expand Down Expand Up @@ -219,17 +219,25 @@ pub mod fun {
use super::*;

impl Fun {
/// Returns the name of the function.
pub fn name(&self) -> Result<Expr> {
self.0
.at(1)
.ok_or(SemanticError::InvalidExpression)?
.try_into()
}

/// Returns a list of parameters that are in the spine of the function.
pub fn parameters(&self) -> Result<List> {
self.0
.at(1)
.at(2)
.map(List)
.ok_or(SemanticError::MissingParameters)
}

/// Returns the body of the function.
pub fn body(&self) -> Result<Expr> {
self.0.at(2).ok_or(SemanticError::MissingBody)?.try_into()
self.0.at(3).ok_or(SemanticError::MissingBody)?.try_into()
}
}
}
Expand Down

0 comments on commit b10506a

Please sign in to comment.