Take care of all expression tests

This commit is contained in:
Greg Shuflin 2021-11-11 22:24:35 -08:00
parent c3d36ab320
commit 96d12f3659
2 changed files with 8 additions and 16 deletions

View File

@ -135,10 +135,10 @@ peg::parser! {
":" _ identifier:type_identifier() { identifier }
pub rule expression() -> Expression =
_ kind:expression_kind() _ type_anno:type_anno()? { Expression { id: Default::default(), type_anno, kind: kind } }
__ kind:expression_kind() _ type_anno:type_anno()? { Expression { id: Default::default(), type_anno, kind } }
rule expression_no_struct() -> Expression =
_ kind:expression_kind_no_struct() { Expression { id: Default::default(), type_anno: None, kind: kind } }
__ kind:expression_kind_no_struct() { Expression { id: Default::default(), type_anno: None, kind: kind } }
rule expression_kind() -> ExpressionKind =
precedence_expr(true)
@ -333,7 +333,7 @@ peg::parser! {
}
rule cond_block() -> IfExpressionBody =
"{" _ cond_arms:(cond_arm() ++ ",") _ "}" { IfExpressionBody::CondList(cond_arms) }
"{" __ cond_arms:(cond_arm() ++ (delimiter()+)) __ "}" { IfExpressionBody::CondList(cond_arms) }
rule cond_arm() -> ConditionArm =
_ "else" _ body:expr_or_block() { ConditionArm { condition: Condition::Else, guard: None, body } } /
@ -347,7 +347,6 @@ peg::parser! {
rule condition_guard() -> Option<Expression> =
("if" _ expr:expression() { expr } )?
rule expr_or_block() -> Block = block() / ex:expression() {
Statement {
id: Default::default(), location: Default::default(),
@ -388,7 +387,7 @@ peg::parser! {
"true" { Pattern::Literal(PatternLiteral::BoolPattern(true)) } /
"false" { Pattern::Literal(PatternLiteral::BoolPattern(false)) } /
s:bare_string_literal() { Pattern::Literal(PatternLiteral::StringPattern(Rc::new(s.to_string()))) } /
sign:("-"?) num:nat_literal() {
sign:("-"?) num:(float_literal() / nat_literal()) {
let neg = sign.is_some();
Pattern::Literal(PatternLiteral::NumPattern { neg, num })
} /

View File

@ -133,13 +133,6 @@ macro_rules! assert_fail2 {
};
}
macro_rules! assert_expr {
($input:expr, $correct:expr) => {
let mut parser = make_parser($input);
assert_eq!(parser.expression().unwrap(), $correct);
};
}
macro_rules! assert_expr2 {
($input:expr, $correct:expr) => {
let expr = schala_parser::expression($input);
@ -1103,7 +1096,7 @@ fn if_exprs() {
})
);
assert_expr!(
assert_expr2!(
r#"
if true then {
let a = 10
@ -1188,7 +1181,7 @@ fn pattern_matching() {
);
assert_expr2!(
"if x { is 1 then 5, else 20 }",
"if x { is 1 then 5; else 20 }",
expr(IfExpression {
discriminator: Some(bx(expr(Value(qn!(x))))),
body: bx(IfExpressionBody::CondList(vec![
@ -1221,10 +1214,10 @@ fn pattern_matching() {
})
);
assert_expr! {
assert_expr2! {
r#"
if (45, "panda", false, 2.2) {
is (49, "pablo", _, 28.4) then "no"
is (49, "pablo", _, 28.4) then "no"
is (_, "panda", _, -2.2) then "yes"
is _ then "maybe"
}"#,