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:
parent
ed9d1312d1
commit
9cc9c5977d
16
main.schala
16
main.schala
@ -1,7 +1,17 @@
|
|||||||
|
|
||||||
|
fn add(a, b)
|
||||||
|
a + b
|
||||||
|
end
|
||||||
|
|
||||||
|
fn subtract(a, b)
|
||||||
|
a - b
|
||||||
|
end
|
||||||
|
|
||||||
fn main()
|
fn main()
|
||||||
a = 4
|
first_value = add(20, 20)
|
||||||
b = 20
|
second_value = subtract(700, 650)
|
||||||
a + b
|
first_value + second_value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
11
src/eval.rs
11
src/eval.rs
@ -8,7 +8,6 @@ type Reduction<T> = (T, Option<SideEffect>);
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum SideEffect {
|
enum SideEffect {
|
||||||
Print(String),
|
Print(String),
|
||||||
Bundle(Vec<SideEffect>),
|
|
||||||
AddBinding(String, Expression),
|
AddBinding(String, Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,11 +127,6 @@ impl Evaluator {
|
|||||||
use self::SideEffect::*;
|
use self::SideEffect::*;
|
||||||
match side_effect {
|
match side_effect {
|
||||||
Print(s) => println!("{}", s),
|
Print(s) => println!("{}", s),
|
||||||
Bundle(l) => {
|
|
||||||
for side_effect in l {
|
|
||||||
self.perform_side_effect(side_effect);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AddBinding(var, value) => {
|
AddBinding(var, value) => {
|
||||||
self.add_binding(var, value);
|
self.add_binding(var, value);
|
||||||
}
|
}
|
||||||
@ -285,19 +279,18 @@ impl Evaluator {
|
|||||||
|
|
||||||
self.frames.push(frame);
|
self.frames.push(frame);
|
||||||
let mut retval = Null;
|
let mut retval = Null;
|
||||||
let mut side_effects = Vec::new();
|
|
||||||
for expr in function.body.iter() {
|
for expr in function.body.iter() {
|
||||||
retval = expr.clone();
|
retval = expr.clone();
|
||||||
while retval.is_reducible() {
|
while retval.is_reducible() {
|
||||||
let r = self.reduce_expr(retval);
|
let r = self.reduce_expr(retval);
|
||||||
retval = r.0;
|
retval = r.0;
|
||||||
if let Some(s) = r.1 {
|
if let Some(s) = r.1 {
|
||||||
side_effects.push(s);
|
self.perform_side_effect(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.frames.pop();
|
self.frames.pop();
|
||||||
(retval, Some(SideEffect::Bundle(side_effects)))
|
(retval, None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user