Disable eval for now

This commit is contained in:
Richard Feldman 2019-07-06 13:33:58 -04:00
parent 6abec2b0f2
commit 38a4ac5c5c
4 changed files with 100 additions and 99 deletions

View file

@ -1,6 +1,7 @@
use expr::{Expr, Operator, Pattern, Ident}; use expr::{Expr, Pattern, Ident};
use expr::Pattern::*; use expr::Pattern::*;
use expr::Operator::*; use operator::Operator::*;
use operator::Operator;
use std::rc::Rc; use std::rc::Rc;
use std::fmt; use std::fmt;
use im_rc::hashmap::HashMap; use im_rc::hashmap::HashMap;
@ -52,7 +53,7 @@ pub fn scoped_eval(expr: Expr, vars: &Scope) -> Evaluated {
Expr::Str(string) => Str(string), Expr::Str(string) => Str(string),
Expr::Frac(numerator, denominator) => Frac(fraction_from_i64s(numerator, denominator)), Expr::Frac(numerator, denominator) => Frac(fraction_from_i64s(numerator, denominator)),
Expr::Char(ch) => Char(ch), Expr::Char(ch) => Char(ch),
Expr::Closure(args, body) => Closure(args, body, vars.clone()), Expr::Closure(args, body) => Closure(args.into_iter().map(|e| e.value).collect(), Box::new(body.value), vars.clone()),
Expr::EmptyRecord => EmptyRecord, Expr::EmptyRecord => EmptyRecord,
// Resolve variable names // Resolve variable names
@ -65,7 +66,7 @@ pub fn scoped_eval(expr: Expr, vars: &Scope) -> Evaluated {
let mut output = String::new(); let mut output = String::new();
for (string, var_name) in pairs.into_iter() { for (string, var_name) in pairs.into_iter() {
match vars.get(&var_name) { match vars.get(&var_name.value) {
Some(resolved) => { Some(resolved) => {
match **resolved { match **resolved {
Str(ref var_string) => { Str(ref var_string) => {
@ -73,11 +74,11 @@ pub fn scoped_eval(expr: Expr, vars: &Scope) -> Evaluated {
output.push_str(var_string.as_str()); output.push_str(var_string.as_str());
}, },
_ => { _ => {
return EvalError(TypeMismatch(var_name)); return EvalError(TypeMismatch(var_name.value));
} }
} }
}, },
None => { return EvalError(UnrecognizedVarName(var_name)); } None => { return EvalError(UnrecognizedVarName(var_name.value)); }
} }
} }

View file

@ -1,7 +1,7 @@
pub mod expr; pub mod expr;
pub mod parse; pub mod parse;
pub mod parse_state; pub mod parse_state;
pub mod eval; // pub mod eval;
pub mod fast_fraction; pub mod fast_fraction;
// mod ena; // mod ena;

View file

@ -1,4 +1,4 @@
use expr::Operator; use operator::Operator;
use expr::{Expr, Pattern, Ident}; use expr::{Expr, Pattern, Ident};
use std::char; use std::char;

View file

@ -1,4 +1,4 @@
#[macro_use] extern crate pretty_assertions; // #[macro_use] extern crate pretty_assertions;
extern crate combine; extern crate combine;
extern crate fraction; extern crate fraction;
@ -6,102 +6,102 @@ extern crate roc;
#[cfg(test)] #[cfg(test)]
mod test_eval { mod test_eval {
use roc::expr::Operator::*; // use roc::operator::Operator::*;
use roc::expr::Pattern::*; // use roc::expr::Pattern::*;
use roc::expr::Expr::*; // use roc::expr::Expr::*;
use roc::eval::eval; // use roc::eval::eval;
use roc::eval::Evaluated; // use roc::eval::Evaluated;
use fraction::Fraction; // use fraction::Fraction;
#[test] // #[test]
fn one_plus_one() { // fn one_plus_one() {
assert_eq!( // assert_eq!(
eval(Operator(Box::new(Int(1)), Plus, Box::new(Int(1)))), // eval(Operator(Box::new(Int(1)), Plus, Box::new(Int(1)))),
Evaluated::Int(2) // Evaluated::Int(2)
); // );
} // }
#[test] // #[test]
fn point_one_plus_point_two() { // fn point_one_plus_point_two() {
// 0.1 + 0.2 == 0.3 THAT'S WHAT'S UP // // 0.1 + 0.2 == 0.3 THAT'S WHAT'S UP
assert_eq!( // assert_eq!(
eval(Operator(Box::new(Frac(1, 10)), Plus, Box::new(Frac(2, 10)))), // eval(Operator(Box::new(Frac(1, 10)), Plus, Box::new(Frac(2, 10)))),
Evaluated::Frac(Fraction::new(3u64, 10u64)) // Evaluated::Frac(Fraction::new(3u64, 10u64))
); // );
} // }
#[test] // #[test]
fn addition_reduces() { // fn addition_reduces() {
assert_eq!( // assert_eq!(
eval(Operator(Box::new(Frac(1, 3)), Plus, Box::new(Frac(7, 14)))), // eval(Operator(Box::new(Frac(1, 3)), Plus, Box::new(Frac(7, 14)))),
Evaluated::Frac(Fraction::new(5u64, 6u64)) // Evaluated::Frac(Fraction::new(5u64, 6u64))
); // );
} // }
#[test] // #[test]
fn division_reduces() { // fn division_reduces() {
assert_eq!( // assert_eq!(
eval(Operator(Box::new(Frac(1, 3)), Slash, Box::new(Frac(7, 14)))), // eval(Operator(Box::new(Frac(1, 3)), Slash, Box::new(Frac(7, 14)))),
Evaluated::ApplyVariant( // Evaluated::ApplyVariant(
"Ok".to_string(), // "Ok".to_string(),
Some(vec![Evaluated::Frac(Fraction::new(2u64, 3u64))]) // Some(vec![Evaluated::Frac(Fraction::new(2u64, 3u64))])
) // )
); // );
} // }
#[test] // #[test]
fn division_by_zero() { // fn division_by_zero() {
assert_eq!( // assert_eq!(
eval(Operator(Box::new(Frac(1, 10)), Slash, Box::new(Frac(0, 10)))), // eval(Operator(Box::new(Frac(1, 10)), Slash, Box::new(Frac(0, 10)))),
Evaluated::ApplyVariant( // Evaluated::ApplyVariant(
"Err".to_string(), // "Err".to_string(),
Some(vec![Evaluated::ApplyVariant("DivisionByZero".to_string(), None)]) // Some(vec![Evaluated::ApplyVariant("DivisionByZero".to_string(), None)])
) // )
); // );
} // }
#[test] // #[test]
fn string_interpolation() { // fn string_interpolation() {
assert_eq!( // assert_eq!(
eval( // eval(
Assign(Identifier("foo".to_string()), Box::new(Str("one".to_string())), // Assign(Identifier("foo".to_string()), Box::new(Str("one".to_string())),
Box::new(Assign(Identifier("bar".to_string()), Box::new(Str("two".to_string())), // Box::new(Assign(Identifier("bar".to_string()), Box::new(Str("two".to_string())),
Box::new(Assign(Identifier("baz".to_string()), Box::new(Str("three".to_string())), // Box::new(Assign(Identifier("baz".to_string()), Box::new(Str("three".to_string())),
Box::new(InterpolatedStr( // Box::new(InterpolatedStr(
// "hi_\(foo)_\(bar)_\(baz)_string!" // // "hi_\(foo)_\(bar)_\(baz)_string!"
vec![ // vec![
("hi_".to_string(), "foo".to_string()), // ("hi_".to_string(), "foo".to_string()),
("_".to_string(), "bar".to_string()), // ("_".to_string(), "bar".to_string()),
("_".to_string(), "baz".to_string()), // ("_".to_string(), "baz".to_string()),
], // ],
"_string!".to_string() // "_string!".to_string()
)) // ))
))))) // )))))
), // ),
Evaluated::Str("hi_one_two_three_string!".to_string()) // Evaluated::Str("hi_one_two_three_string!".to_string())
); // );
} // }
#[test] // #[test]
fn if_else() { // fn if_else() {
assert_eq!( // assert_eq!(
eval( // eval(
If(Box::new(ApplyVariant("True".to_string(), None)), // If(Box::new(ApplyVariant("True".to_string(), None)),
Box::new(Operator(Box::new(Int(1)), Plus, Box::new(Int(2)))), // Box::new(Operator(Box::new(Int(1)), Plus, Box::new(Int(2)))),
Box::new(Operator(Box::new(Int(4)), Plus, Box::new(Int(5)))) // Box::new(Operator(Box::new(Int(4)), Plus, Box::new(Int(5))))
) // )
), // ),
Evaluated::Int(3) // Evaluated::Int(3)
); // );
assert_eq!( // assert_eq!(
eval( // eval(
If(Box::new(ApplyVariant("False".to_string(), None)), // If(Box::new(ApplyVariant("False".to_string(), None)),
Box::new(Operator(Box::new(Int(1)), Plus, Box::new(Int(2)))), // Box::new(Operator(Box::new(Int(1)), Plus, Box::new(Int(2)))),
Box::new(Operator(Box::new(Int(4)), Plus, Box::new(Int(5)))) // Box::new(Operator(Box::new(Int(4)), Plus, Box::new(Int(5))))
) // )
), // ),
Evaluated::Int(9) // Evaluated::Int(9)
); // );
} // }
} }