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

Commit

Permalink
feat: add base stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Nov 23, 2023
1 parent d7e73d5 commit e009c44
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
4 changes: 4 additions & 0 deletions build.soft
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(require '[stdlib.soft])

(println (-> stdlib/args
(map (fun [x] (str "Hello " x)))))
28 changes: 20 additions & 8 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ pub enum Value {
String(String),
Float(u64),
Fun(Fun),
List {
elements: Vec<Value>,
},
List(Vec<Value>),
Apply {
callee: Box<Value>,
arguments: Vec<Value>,
Expand Down Expand Up @@ -208,13 +206,12 @@ 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()?
Expr::List(list) => Ok(Value::List(
list.elements()?
.into_iter()
.map(|expr| expr.expand(environment))
.collect::<Result<Vec<_>, _>>()?,
}),
)),
Expr::Def(def) => Ok(Value::Def {
name: def.name()?.expand(environment)?.try_into()?,
value: def.value()?.expand(environment)?.into(),
Expand Down Expand Up @@ -261,10 +258,12 @@ impl Expr {
Expr::Literal(_) => Err(keyword!("eval.error/invalid-literal")),
}
}
}

impl Value {
/// Evaluate the expression into a value.
pub fn eval(self, environment: &Environment) -> Trampoline<Value> {

Check failure on line 265 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 👁️‍🗨️ Lint check (nightly)

parameter is only used in recursion
match self.expand(environment)? {
match self {
Value::Deref { box value, .. } => {
let Value::Atomic(atomic) = value else {
bail!(keyword!("eval.error/atomic-expected"))
Expand All @@ -273,7 +272,20 @@ impl Expr {

Done(guard.clone())
}
Value::Apply { callee, arguments } => {

Check warning on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 🧪 Test check (nightly)

unused variable: `callee`

Check warning on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 🧪 Test check (nightly)

unused variable: `arguments`

Check warning on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 👁️‍🗨️ Compilation check (nightly)

unused variable: `callee`

Check warning on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 👁️‍🗨️ Compilation check (nightly)

unused variable: `arguments`

Check failure on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 👁️‍🗨️ Lint check (nightly)

unused variable: `callee`

Check failure on line 275 in src/eval.rs

View workflow job for this annotation

GitHub Actions / 👁️‍🗨️ Lint check (nightly)

unused variable: `arguments`
todo!()
}
Value::List(old_elements) => {
let mut new_elements = Vec::new();
for element in old_elements {
new_elements.push(element.eval(environment)?);
}

Done(Value::List(new_elements))
}

// Base cases for evaluation when it will just walk the tree. These
// are the cases where the evaluation is recursive.
value => Done(value),
}
}
Expand Down
15 changes: 15 additions & 0 deletions stdlib.soft
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(defmacro* map* (fun* [f coll]
(if (cons? coll)
(cons (f (car coll)) (map* f (cdr coll)))
coll)))

(defmacro* quasi-quote (fun* [form]
(if (cons? form)
(if (= 'unquote (car form))
(cdr form)
(cons 'list (map* quasi-quote form)))
(list 'quote form))))

(defmacro* defmacro [name args & body]
(let [body (cons 'block body)]
`(defmacro* ,name (fun* ,args ,body))))

0 comments on commit e009c44

Please sign in to comment.