feat(prelude): add progn

This commit is contained in:
Jonas Röger 2024-11-10 23:46:50 +01:00
parent fd98aa4a96
commit 369293598f
Signed by: jonas
GPG Key ID: 4000EB35E1AE0F07
3 changed files with 13 additions and 1 deletions

View File

@ -15,6 +15,7 @@ fn main() {
"(let '((a . (vec3 1 2 3)) (b . (vec3 4 5 6))) (vec3-dot (vec3-norm (vec3-add a b)) a))", "(let '((a . (vec3 1 2 3)) (b . (vec3 4 5 6))) (vec3-dot (vec3-norm (vec3-add a b)) a))",
"(defun do-n-times (f n) (if (= n 0) '() (cons (f) (do-n-times f (- n 1)))))", "(defun do-n-times (f n) (if (= n 0) '() (cons (f) (do-n-times f (- n 1)))))",
"(do-n-times (lambda () (print 'hello)) 5)", "(do-n-times (lambda () (print 'hello)) 5)",
"(progn (print 'hello) (print 'world))",
]; ];
let environment = Environment::default(); let environment = Environment::default();

View File

@ -103,7 +103,7 @@ pub fn eval(env: &Environment, expr: Expression) -> Result<Expression, EvalError
a => Err(EvalError::NotAFunction(a)), a => Err(EvalError::NotAFunction(a)),
}, },
Expression::Quote(e) => Ok(*e), Expression::Quote(e) => Ok(*e),
Expression::Symbol(s) => eval(env, env.get(&s).ok_or(EvalError::SymbolNotBound(s))?), Expression::Symbol(s) => env.get(&s).ok_or(EvalError::SymbolNotBound(s)),
x => Ok(x), x => Ok(x),
} }
} }

View File

@ -245,6 +245,16 @@ pub fn prelude_eval(env: &Environment, expr: Expression) -> Result<Expression, E
eval(env, eval(env, e)?) eval(env, eval(env, e)?)
} }
pub fn prelude_progn(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
let mut result = Expression::Nil;
for e in CellIterator::new(expr) {
result = eval(env, e?)?;
}
Ok(result)
}
pub fn mk_prelude(layer: &mut EnvironmentLayer) { pub fn mk_prelude(layer: &mut EnvironmentLayer) {
layer.set("+".to_string(), Expression::Function(prelude_add)); layer.set("+".to_string(), Expression::Function(prelude_add));
layer.set("-".to_string(), Expression::Function(prelude_sub)); layer.set("-".to_string(), Expression::Function(prelude_sub));
@ -265,4 +275,5 @@ pub fn mk_prelude(layer: &mut EnvironmentLayer) {
layer.set("car".to_string(), Expression::Function(prelude_car)); layer.set("car".to_string(), Expression::Function(prelude_car));
layer.set("cdr".to_string(), Expression::Function(prelude_cdr)); layer.set("cdr".to_string(), Expression::Function(prelude_cdr));
layer.set("eval".to_string(), Expression::Function(prelude_eval)); layer.set("eval".to_string(), Expression::Function(prelude_eval));
layer.set("progn".to_string(), Expression::Function(prelude_progn));
} }