1
0

Add float support; Write tests

This commit is contained in:
Jeff 2023-09-29 08:21:38 -04:00
parent 5877884877
commit 3182e7c860
2 changed files with 27 additions and 5 deletions

View File

@ -53,11 +53,10 @@ pub fn eval_with_context(source: &str, context: &mut VariableMap) -> Vec<Result<
println!("{sexp}"); println!("{sexp}");
let evaluator = Evaluator::new(tree.clone(), source).unwrap(); let evaluator = Evaluator::new(tree.clone(), source).unwrap();
println!("{evaluator:?}");
let results = evaluator.run(context, &mut cursor, source); let results = evaluator.run(context, &mut cursor, source);
println!("{results:?}"); println!("{results:?}");
println!("{context:?}");
results results
} }
@ -317,7 +316,6 @@ impl ControlFlow {
let second_child = node.child(1).unwrap(); let second_child = node.child(1).unwrap();
let fourth_child = node.child(3).unwrap(); let fourth_child = node.child(3).unwrap();
let sixth_child = node.child(5); let sixth_child = node.child(5);
println!("{second_child:?} {fourth_child:?} {sixth_child:?}");
let else_statement = if let Some(child) = sixth_child { let else_statement = if let Some(child) = sixth_child {
Some(Statement::new(child, source)?) Some(Statement::new(child, source)?)
} else { } else {
@ -370,14 +368,33 @@ mod tests {
#[test] #[test]
fn evaluate_string() { fn evaluate_string() {
assert_eq!(eval("\"one\""), vec![Ok(Value::String("one".to_string()))]);
assert_eq!(eval("'one'"), vec![Ok(Value::String("one".to_string()))]); assert_eq!(eval("'one'"), vec![Ok(Value::String("one".to_string()))]);
assert_eq!(eval("`one`"), vec![Ok(Value::String("one".to_string()))]);
assert_eq!(
eval("`'one'`"),
vec![Ok(Value::String("'one'".to_string()))]
);
assert_eq!(
eval("'`one`'"),
vec![Ok(Value::String("`one`".to_string()))]
);
} }
#[test] #[test]
fn if_else() { fn if_then() {
assert_eq!( assert_eq!(
eval("if true then 'true'"), eval("if true then 'true'"),
vec![Ok(Value::String("true".to_string()))] vec![Ok(Value::String("true".to_string()))]
); );
} }
#[test]
fn if_then_else() {
assert_eq!(eval("if false then 1 else 2"), vec![Ok(Value::Integer(2))]);
assert_eq!(
eval("if true then 1.0 else 42.0"),
vec![Ok(Value::Float(1.0))]
);
}
} }

View File

@ -76,9 +76,14 @@ impl Value {
Ok(Value::Boolean(raw)) Ok(Value::Boolean(raw))
} }
"float" => {
let raw = value_snippet.parse::<f64>().unwrap_or_default();
Ok(Value::Float(raw))
}
"empty" => Ok(Value::Empty), "empty" => Ok(Value::Empty),
_ => Err(Error::UnexpectedSourceNode { _ => Err(Error::UnexpectedSourceNode {
expected: "raw value", expected: "integer, string, boolean or empty",
actual: child.kind(), actual: child.kind(),
}), }),
} }