Fixed evaluation of function calls

This bit still isn't quite small-step but maybe that's okay for
functions
This commit is contained in:
greg 2016-12-29 15:53:48 -08:00
parent ed9d1312d1
commit 9cc9c5977d
2 changed files with 15 additions and 12 deletions

View File

@ -1,7 +1,17 @@
fn add(a, b)
a + b
end
fn subtract(a, b)
a - b
end
fn main()
a = 4
b = 20
a + b
first_value = add(20, 20)
second_value = subtract(700, 650)
first_value + second_value
end
main()

View File

@ -8,7 +8,6 @@ type Reduction<T> = (T, Option<SideEffect>);
#[derive(Debug)]
enum SideEffect {
Print(String),
Bundle(Vec<SideEffect>),
AddBinding(String, Expression),
}
@ -128,11 +127,6 @@ impl Evaluator {
use self::SideEffect::*;
match side_effect {
Print(s) => println!("{}", s),
Bundle(l) => {
for side_effect in l {
self.perform_side_effect(side_effect);
}
}
AddBinding(var, value) => {
self.add_binding(var, value);
}
@ -285,19 +279,18 @@ impl Evaluator {
self.frames.push(frame);
let mut retval = Null;
let mut side_effects = Vec::new();
for expr in function.body.iter() {
retval = expr.clone();
while retval.is_reducible() {
let r = self.reduce_expr(retval);
retval = r.0;
if let Some(s) = r.1 {
side_effects.push(s);
self.perform_side_effect(s);
}
}
}
self.frames.pop();
(retval, Some(SideEffect::Bundle(side_effects)))
(retval, None)
}
}