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