Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5215a2e678
|
||
|
|
66c3ab128f
|
@@ -81,6 +81,7 @@ 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> {
|
||||||
@@ -100,32 +101,30 @@ 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(overlay), body)
|
eval(&env.overlay(capture).overlay(overlay), body)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dispatch a closure. Evaluates `body` in `env` overlayed with the `layer`
|
/// Dispatch a function inside an environment
|
||||||
fn dispatch_closure(
|
fn dispatch_function(
|
||||||
env: &Environment,
|
env: &Environment,
|
||||||
layer: EnvironmentLayer,
|
f: Expression,
|
||||||
body: Expression,
|
args: Expression,
|
||||||
) -> Result<Expression, EvalError> {
|
) -> Result<Expression, EvalError> {
|
||||||
println!("Dispatching closure: {} -> {}", &layer, &body);
|
match f {
|
||||||
let env = env.overlay(layer);
|
Expression::Function(f) => f(env, args),
|
||||||
eval(&env, body)
|
Expression::AnonymousFunction {
|
||||||
|
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) => match eval(env, *lhs)? {
|
Expression::Cell(lhs, rhs) => dispatch_function(env, eval(env, *lhs)?, *rhs),
|
||||||
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,3 +1,4 @@
|
|||||||
|
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;
|
||||||
@@ -129,13 +130,10 @@ 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.
|
||||||
@@ -164,13 +162,19 @@ 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),
|
||||||
@@ -192,10 +196,12 @@ 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
|
||||||
@@ -226,13 +232,16 @@ 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(_) => Err(EvalError::TypeError(
|
Err(a) => Err(EvalError::TypeError(format!(
|
||||||
"Expression is not a ForeignDataWrapper".to_string(),
|
"Expression (type={}) is not a ForeignDataWrapper of type {}",
|
||||||
)),
|
a.type_name(),
|
||||||
|
type_name::<T>()
|
||||||
|
))),
|
||||||
},
|
},
|
||||||
_ => Err(EvalError::TypeError(
|
e => Err(EvalError::TypeError(format!(
|
||||||
"Expression is not a ForeignDataWrapper".to_string(),
|
"Expression ({}) is not a ForeignDataWrapper",
|
||||||
)),
|
e
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -416,13 +425,17 @@ 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!(f, "(lambda ({}) {})", argument_symbols.join(" "), body),
|
} => write!(
|
||||||
|
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,6 +8,7 @@ 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> {
|
||||||
@@ -82,7 +83,33 @@ pub fn prelude_div(env: &Environment, expr: Expression) -> Result<Expression, Ev
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prelude_lambda(_env: &Environment, expr: Expression) -> Result<Expression, EvalError> {
|
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> {
|
||||||
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
|
||||||
@@ -92,8 +119,10 @@ 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),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -115,6 +144,7 @@ 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());
|
||||||
@@ -148,10 +178,7 @@ pub fn prelude_let(env: &Environment, expr: Expression) -> Result<Expression, Ev
|
|||||||
})
|
})
|
||||||
.collect::<Result<HashMap<String, Expression>, EvalError>>()?;
|
.collect::<Result<HashMap<String, Expression>, EvalError>>()?;
|
||||||
|
|
||||||
Ok(Expression::Closure {
|
eval(&env.overlay(bindings.into()), body)
|
||||||
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> {
|
||||||
|
|||||||
+2
-1
@@ -5,7 +5,8 @@
|
|||||||
(set 'ray-depth 5)
|
(set 'ray-depth 5)
|
||||||
(set 'sub-pixel 2)
|
(set 'sub-pixel 2)
|
||||||
|
|
||||||
(set 'mz (mandel-zoom frames 1800 5000))
|
(set 'mz (mandel-zoom frames 1800 50000))
|
||||||
|
(println (mz 1))
|
||||||
|
|
||||||
(defun mandel-plane (t)
|
(defun mandel-plane (t)
|
||||||
(texture-plane
|
(texture-plane
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ 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