From ec5a9d457e464da9f55dbfdd76b5acb888ec0dec Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 31 Oct 2018 01:45:16 -0700 Subject: [PATCH] String patterns --- schala-lang/language/src/eval.rs | 9 +++++++++ schala-lang/language/src/reduced_ast.rs | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/schala-lang/language/src/eval.rs b/schala-lang/language/src/eval.rs index 88ee439..0e03199 100644 --- a/schala-lang/language/src/eval.rs +++ b/schala-lang/language/src/eval.rs @@ -569,6 +569,15 @@ if a { is 15 -> "x", is 10 -> "y" } test_in_fresh_env!(source, "\"y\""); } + #[test] + fn string_pattern() { + let source = r#" +let a = "foo" +if a { is "foo" -> "x", is _ -> "y" } +"#; + test_in_fresh_env!(source, "\"x\""); + } + #[test] fn boolean_pattern() { let source = r#" diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 1bfdef0..c026a31 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -292,7 +292,19 @@ impl PatternLiteral { bound_vars: vec![], } }, - StringPattern(_s) => unimplemented!(), + StringPattern(s) => { + let guard = Some(Expr::Call { + f: Box::new(Expr::Func(Func::BuiltIn(Rc::new("==".to_string())))), + args: vec![Expr::Lit(Lit::StringLit(s.clone())), Expr::ConditionalTargetSigilValue] + }); + + Subpattern { + tag: None, + subpatterns: vec![], + guard, + bound_vars: vec![], + } + }, BoolPattern(b) => { let guard = Some(if *b { Expr::ConditionalTargetSigilValue