All environment changes represented explicitly

Start the work of rewriting the evluator to be a true small-step
evaluator - that is, all state changes are represented explicitly as
SideEffect types, and not as methods called on the evaluator, except at
the very top of the evaluation loop
This commit is contained in:
greg 2016-12-29 04:31:25 -08:00
parent e84550f3ec
commit 29d9e50311

View File

@ -3,6 +3,8 @@ extern crate take_mut;
use std::collections::HashMap; use std::collections::HashMap;
use parser::{AST, ASTNode, Expression, Function}; use parser::{AST, ASTNode, Expression, Function};
type Reduction<T> = (T, Option<SideEffect>);
#[derive(Debug)] #[derive(Debug)]
enum SideEffect { enum SideEffect {
Print(String), Print(String),
@ -136,7 +138,7 @@ impl Evaluator {
} }
} }
fn reduce_astnode(&mut self, node: ASTNode) -> (ASTNode, Option<SideEffect>) { fn reduce_astnode(&mut self, node: ASTNode) -> Reduction<ASTNode> {
use parser::ASTNode::*; use parser::ASTNode::*;
match node { match node {
ExprNode(expr) => { ExprNode(expr) => {
@ -155,7 +157,7 @@ impl Evaluator {
} }
} }
fn reduce_expr(&mut self, expression: Expression) -> (Expression, Option<SideEffect>) { fn reduce_expr(&mut self, expression: Expression) -> Reduction<Expression> {
use parser::Expression::*; use parser::Expression::*;
match expression { match expression {
Null => (Null, None), Null => (Null, None),
@ -262,7 +264,7 @@ impl Evaluator {
fn reduce_call(&mut self, fn reduce_call(&mut self,
name: String, name: String,
arguments: Vec<Expression>) arguments: Vec<Expression>)
-> (Expression, Option<SideEffect>) { -> Reduction<Expression> {
use parser::Expression::*; use parser::Expression::*;
// ugly hack for now // ugly hack for now