Implement while-reduction
This commit is contained in:
parent
eb5ce2ef9e
commit
9a4760d44f
@ -1,4 +1,5 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::clone::Clone;
|
||||||
|
|
||||||
use parser::AST;
|
use parser::AST;
|
||||||
use parser::AST::*;
|
use parser::AST::*;
|
||||||
@ -67,6 +68,17 @@ fn reduce(evr: EvalResult) -> EvalResult {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
WhileStatement(condition, body) => {
|
||||||
|
let (continue_loop, env) = reduce((*condition.clone(), env));
|
||||||
|
match continue_loop {
|
||||||
|
Null => (DoNothing, env),
|
||||||
|
_ => {
|
||||||
|
let (_, new_env) = reduce((*body.clone(), env));
|
||||||
|
reduce((WhileStatement(condition, body), new_env))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
BinOp(op, lhs, rhs) => {
|
BinOp(op, lhs, rhs) => {
|
||||||
let (reduced_lhs, new_env) = reduce((*lhs, env));
|
let (reduced_lhs, new_env) = reduce((*lhs, env));
|
||||||
let (reduced_rhs, new_env2) = reduce((*rhs, new_env));
|
let (reduced_rhs, new_env2) = reduce((*rhs, new_env));
|
||||||
|
@ -4,7 +4,7 @@ use std::iter::Peekable;
|
|||||||
use tokenizer::{Token, Kw};
|
use tokenizer::{Token, Kw};
|
||||||
use tokenizer::Token::*;
|
use tokenizer::Token::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum AST {
|
pub enum AST {
|
||||||
Null,
|
Null,
|
||||||
Name(String),
|
Name(String),
|
||||||
|
Loading…
Reference in New Issue
Block a user