Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
762ed3c478
|
@@ -81,7 +81,6 @@ impl Iterator for CellIterator {
|
||||
fn dispatch_anonymous_function(
|
||||
env: &Environment,
|
||||
argument_symbols: Vec<String>,
|
||||
capture: EnvironmentLayer,
|
||||
body: Expression,
|
||||
args: Expression,
|
||||
) -> Result<Expression, EvalError> {
|
||||
@@ -101,30 +100,32 @@ fn dispatch_anonymous_function(
|
||||
overlay.set(symbol.to_owned(), eval(&env, arg.to_owned())?);
|
||||
}
|
||||
|
||||
eval(&env.overlay(capture).overlay(overlay), body)
|
||||
eval(&env.overlay(overlay), body)
|
||||
}
|
||||
|
||||
/// Dispatch a function inside an environment
|
||||
fn dispatch_function(
|
||||
/// Dispatch a closure. Evaluates `body` in `env` overlayed with the `layer`
|
||||
fn dispatch_closure(
|
||||
env: &Environment,
|
||||
f: Expression,
|
||||
args: Expression,
|
||||
layer: EnvironmentLayer,
|
||||
body: Expression,
|
||||
) -> Result<Expression, EvalError> {
|
||||
match f {
|
||||
Expression::Function(f) => f(env, args),
|
||||
Expression::AnonymousFunction {
|
||||
argument_symbols,
|
||||
capture,
|
||||
body,
|
||||
} => dispatch_anonymous_function(env, argument_symbols, capture, *body, args),
|
||||
a => Err(EvalError::NotAFunction(a)),
|
||||
}
|
||||
println!("Dispatching closure: {} -> {}", &layer, &body);
|
||||
let env = env.overlay(layer);
|
||||
eval(&env, body)
|
||||
}
|
||||
|
||||
/// Evaluate an expression inside an environment
|
||||
pub fn eval(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||
match expr {
|
||||
Expression::Cell(lhs, rhs) => dispatch_function(env, eval(env, *lhs)?, *rhs),
|
||||
Expression::Cell(lhs, rhs) => match eval(env, *lhs)? {
|
||||
Expression::Function(f) => f(env, *rhs),
|
||||
Expression::AnonymousFunction {
|
||||
argument_symbols,
|
||||
body,
|
||||
} => dispatch_anonymous_function(env, argument_symbols, *body, *rhs),
|
||||
a => Err(EvalError::NotAFunction(a)),
|
||||
},
|
||||
Expression::Closure { layer, body } => dispatch_closure(env, layer, *body),
|
||||
Expression::Quote(e) => Ok(*e),
|
||||
Expression::Symbol(s) => env.get(&s).ok_or(EvalError::SymbolNotBound(s)),
|
||||
x => Ok(x),
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use std::any::type_name;
|
||||
use std::any::Any;
|
||||
use std::fmt::Debug;
|
||||
use std::fmt::Display;
|
||||
@@ -130,10 +129,13 @@ pub enum Expression {
|
||||
Cell(Box<Expression>, Box<Expression>),
|
||||
/// A function expression pointing to native code.
|
||||
Function(fn(&Environment, Expression) -> Result<Expression, EvalError>),
|
||||
Closure {
|
||||
layer: EnvironmentLayer,
|
||||
body: Box<Expression>,
|
||||
},
|
||||
/// A anonymous function expression consisting of bound symbols and a body expression.
|
||||
AnonymousFunction {
|
||||
argument_symbols: Vec<String>,
|
||||
capture: EnvironmentLayer,
|
||||
body: Box<Expression>,
|
||||
},
|
||||
/// A foreign data expression.
|
||||
@@ -162,19 +164,13 @@ impl PartialEq for Expression {
|
||||
(
|
||||
AnonymousFunction {
|
||||
argument_symbols: args1,
|
||||
capture: capture1,
|
||||
body: body1,
|
||||
},
|
||||
AnonymousFunction {
|
||||
argument_symbols: args2,
|
||||
capture: capture2,
|
||||
body: body2,
|
||||
},
|
||||
) => {
|
||||
PartialEq::eq(args1, args2)
|
||||
&& PartialEq::eq(body1, body2)
|
||||
&& PartialEq::eq(capture1, capture2)
|
||||
}
|
||||
) => PartialEq::eq(args1, args2) && PartialEq::eq(body1, body2),
|
||||
(ForeignExpression(f1), ForeignExpression(f2)) => PartialEq::eq(f1, f2),
|
||||
(Quote(e1), Quote(e2)) => PartialEq::eq(e1, e2),
|
||||
(Symbol(s1), Symbol(s2)) => PartialEq::eq(s1, s2),
|
||||
@@ -196,12 +192,10 @@ impl PartialOrd for Expression {
|
||||
(
|
||||
AnonymousFunction {
|
||||
argument_symbols: args1,
|
||||
capture: _,
|
||||
body: body1,
|
||||
},
|
||||
AnonymousFunction {
|
||||
argument_symbols: args2,
|
||||
capture: _,
|
||||
body: body2,
|
||||
},
|
||||
) => args1
|
||||
@@ -232,16 +226,13 @@ impl<T: ForeignData> TryFrom<Expression> for ForeignDataWrapper<T> {
|
||||
match value {
|
||||
Expression::ForeignExpression(f) => match f.as_any_box().downcast::<T>() {
|
||||
Ok(data) => Ok(ForeignDataWrapper(data)),
|
||||
Err(a) => Err(EvalError::TypeError(format!(
|
||||
"Expression (type={}) is not a ForeignDataWrapper of type {}",
|
||||
a.type_name(),
|
||||
type_name::<T>()
|
||||
))),
|
||||
Err(_) => Err(EvalError::TypeError(
|
||||
"Expression is not a ForeignDataWrapper".to_string(),
|
||||
)),
|
||||
},
|
||||
e => Err(EvalError::TypeError(format!(
|
||||
"Expression ({}) is not a ForeignDataWrapper",
|
||||
e
|
||||
))),
|
||||
_ => Err(EvalError::TypeError(
|
||||
"Expression is not a ForeignDataWrapper".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -425,17 +416,13 @@ impl Display for Expression {
|
||||
}
|
||||
}
|
||||
Expression::Function(_) => write!(f, "<function>"),
|
||||
Expression::Closure { layer, body } => {
|
||||
write!(f, "(closure captured={}, body={})", layer, body)
|
||||
}
|
||||
Expression::AnonymousFunction {
|
||||
argument_symbols,
|
||||
capture,
|
||||
body,
|
||||
} => write!(
|
||||
f,
|
||||
"(lambda (capture={}) ({}) {})",
|
||||
capture,
|
||||
argument_symbols.join(" "),
|
||||
body
|
||||
),
|
||||
} => write!(f, "(lambda ({}) {})", argument_symbols.join(" "), body),
|
||||
Expression::Quote(e) => write!(f, "'{}", e),
|
||||
Expression::Symbol(s) => write!(f, "{}", s),
|
||||
Expression::Integer(i) => write!(f, "{}", i),
|
||||
|
||||
@@ -8,7 +8,6 @@ use super::eval::CellIterator;
|
||||
use super::eval::EvalError;
|
||||
use super::expression::Expression;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::VecDeque;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub fn prelude_add(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||
@@ -83,33 +82,7 @@ pub fn prelude_div(env: &Environment, expr: Expression) -> Result<Expression, Ev
|
||||
}
|
||||
}
|
||||
|
||||
fn capture_variables(env: &Environment, expr: &Expression) -> Result<EnvironmentLayer, EvalError> {
|
||||
let mut capture: HashMap<String, Expression> = HashMap::new();
|
||||
let mut q = VecDeque::new();
|
||||
q.push_back(expr);
|
||||
|
||||
while let Some(expr) = q.pop_front() {
|
||||
match expr {
|
||||
Expression::Symbol(s) => {
|
||||
if let Some(expr) = env.get(&s) {
|
||||
capture.insert(s.clone(), expr);
|
||||
}
|
||||
}
|
||||
Expression::Cell(x, xs) => {
|
||||
q.push_back(&*x);
|
||||
q.push_back(&*xs);
|
||||
}
|
||||
Expression::Quote(x) => {
|
||||
q.push_back(&*x);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(capture.into())
|
||||
}
|
||||
|
||||
pub fn prelude_lambda(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||
pub fn prelude_lambda(_env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||
let [args, body]: [Expression; 2] = expr.try_into()?;
|
||||
let mut arg_exprs: Vec<Expression> = args.try_into()?;
|
||||
let argument_symbols: Vec<String> = arg_exprs
|
||||
@@ -119,10 +92,8 @@ pub fn prelude_lambda(env: &Environment, expr: Expression) -> Result<Expression,
|
||||
x => Err(EvalError::NotASymbol(x.to_owned())),
|
||||
})
|
||||
.collect::<Result<Vec<String>, EvalError>>()?;
|
||||
|
||||
Ok(Expression::AnonymousFunction {
|
||||
argument_symbols,
|
||||
capture: capture_variables(&env, &body)?,
|
||||
body: Box::new(body),
|
||||
})
|
||||
}
|
||||
@@ -144,7 +115,6 @@ pub fn prelude_defun(env: &Environment, expr: Expression) -> Result<Expression,
|
||||
|
||||
let f = Expression::AnonymousFunction {
|
||||
argument_symbols,
|
||||
capture: EnvironmentLayer::new(),
|
||||
body: Box::new(body),
|
||||
};
|
||||
env.shared_set(name, f.clone());
|
||||
@@ -178,7 +148,10 @@ pub fn prelude_let(env: &Environment, expr: Expression) -> Result<Expression, Ev
|
||||
})
|
||||
.collect::<Result<HashMap<String, Expression>, EvalError>>()?;
|
||||
|
||||
eval(&env.overlay(bindings.into()), body)
|
||||
Ok(Expression::Closure {
|
||||
layer: bindings.into(),
|
||||
body: Box::new(body),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn prelude_if(env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
||||
|
||||
+1
-2
@@ -5,8 +5,7 @@
|
||||
(set 'ray-depth 5)
|
||||
(set 'sub-pixel 2)
|
||||
|
||||
(set 'mz (mandel-zoom frames 1800 50000))
|
||||
(println (mz 1))
|
||||
(set 'mz (mandel-zoom frames 1800 5000))
|
||||
|
||||
(defun mandel-plane (t)
|
||||
(texture-plane
|
||||
|
||||
@@ -22,10 +22,6 @@ fn main() {
|
||||
"(defun bind-test (x) (let '((x . x)) (lambda (y) (+ x y))))",
|
||||
"(set 'add10 (bind-test 10))",
|
||||
"(add10 10)",
|
||||
"(set 'doAdd (let '((a . 20)) (lambda () (add10 a))))",
|
||||
"(doAdd)",
|
||||
"(defun mkDoubleClosure (a b c) (let '((x . (+ a b)) (y . (* b c))) (lambda (z) (let '((d . (+ z x))) (* d y)))))",
|
||||
"((mkDoubleClosure 1 2 3) 2)",
|
||||
];
|
||||
|
||||
let environment = Environment::default();
|
||||
|
||||
Reference in New Issue
Block a user