From 50d5176b45c7b6b3c47ad830c5b3fabc260cdd26 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 16 Oct 2018 04:10:28 -0700 Subject: [PATCH] Fix bug add test --- schala-lang/src/eval.rs | 20 ++++++++++++++++++-- schala-lang/src/reduced_ast.rs | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index 0d1164f..a6ce53c 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -216,7 +216,7 @@ impl<'a> State<'a> { CaseMatch { box cond, alternatives } => match self.expression(Node::Expr(cond))? { Node::PrimObject { name, tag, items } => { for alt in alternatives { - if alt.tag.map(|t| t == tag).unwrap_or(true) { //TODO add guard check - the semantics + if alt.tag.map(|t| t == tag).unwrap_or(true) { let mut inner_state = State { values: self.values.new_scope(None), symbol_table_handle: self.symbol_table_handle.clone(), @@ -243,7 +243,11 @@ impl<'a> State<'a> { for alt in alternatives { match alt.guard { Some(ref guard_expr) if alt.tag.is_none() => { - return self.block(alt.item) + match self.expression(guard_expr.clone().to_node())? { + Node::Expr(Expr::Lit(::reduced_ast::Lit::Bool(true))) => + return self.block(alt.item), + _ => continue, + } }, _ => () } @@ -512,5 +516,17 @@ let a = Some(99) if a { is None -> 4, is Some(x) -> x } "#; fresh_env!(source, "99"); + + let source = r#" +let a = 10 +if a { is 10 -> "x", is 4 -> "y" } +"#; + fresh_env!(source, "\"x\""); + + let source = r#" +let a = 10 +if a { is 15 -> "x", is 10 -> "y" } +"#; + fresh_env!(source, "\"y\""); } } diff --git a/schala-lang/src/reduced_ast.rs b/schala-lang/src/reduced_ast.rs index 60ecf9c..abbc997 100644 --- a/schala-lang/src/reduced_ast.rs +++ b/schala-lang/src/reduced_ast.rs @@ -228,7 +228,7 @@ impl Pattern { _ => panic!("This should never happen") }); let guard = Some(Expr::Call { - f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("=".to_string())))), + f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))), args: vec![comparison, *cond.clone()] }); Alternative {