From ddea470ba82e92f75dfb5b05a22b97e6eac2d9bf Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 10 Oct 2019 18:17:59 -0700 Subject: [PATCH] Parsing tests pass, eval ones fail --- schala-lang/language/src/parsing/test.rs | 131 ++++++++++++----------- 1 file changed, 71 insertions(+), 60 deletions(-) diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 6d4affc..3b0af5e 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use super::tokenize; use super::ParseResult; -use crate::ast::{ItemIdStore, AST, Expression, Statement, StatementKind, IfExpressionBody, Discriminator, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName, ImportSpecifier, ImportedNames, GuardArm, Guard}; +use crate::ast::{ItemIdStore, AST, Expression, Statement, StatementKind, IfExpressionBody, Pattern, PatternLiteral, TypeBody, Enumerator, ForBody, InvocationArgument, FormalParam, PrefixOp, BinOp, QualifiedName, ImportSpecifier, ImportedNames, Condition, ConditionArm}; use super::Declaration::*; use super::Signature; use super::TypeIdentifier::*; @@ -345,14 +345,14 @@ fn parsing_block_expressions() { parse_test_wrap_ast! { "if a() then { b(); c() }", exst!( IfExpression { - discriminator: bx! { - Discriminator::Simple(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})) - }, + discriminator: Some(bx! { + ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]}) + }), body: bx! { - IfExpressionBody::SimpleConditional( - vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })], - None - ) + IfExpressionBody::SimpleConditional { + then_case: vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })], + else_case: None, + } } } ) @@ -361,16 +361,14 @@ fn parsing_block_expressions() { parse_test_wrap_ast! { "if a() then { b(); c() } else { q }", exst!( IfExpression { - discriminator: bx! { - Discriminator::Simple(ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]})) - }, + discriminator: Some(bx! { + ex!(Call { f: bx!(ex!(val!("a"))), arguments: vec![]}) + }), body: bx! { - IfExpressionBody::SimpleConditional( - vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })], - Some( - vec![exst!(val!("q"))], - ) - ) + IfExpressionBody::SimpleConditional { + then_case: vec![exst!(Call { f: bx!(ex!(val!("b"))), arguments: vec![]}), exst!(Call { f: bx!(ex!(val!("c"))), arguments: vec![] })], + else_case: Some(vec![exst!(val!("q"))]), + } } } ) @@ -620,31 +618,41 @@ fn patterns() { parse_test_wrap_ast! { "if x is Some(a) then { 4 } else { 9 }", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(qname!(Some), - vec![Pattern::VarOrName(qname!(a))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::TupleStruct(qname!(Some), vec![Pattern::VarOrName(qname!(a))]), + then_case: vec![exst!(s "4")], + else_case: Some(vec![exst!(s "9")]) }) + } ) } parse_test_wrap_ast! { "if x is Some(a) then 4 else 9", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch(Pattern::TupleStruct(qname!(Some), - vec![Pattern::VarOrName(qname!(a))]), vec![exst!(s "4")], Some(vec![exst!(s "9")]))) } + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::TupleStruct(qname!(Some), vec![Pattern::VarOrName(qname!(a))]), + then_case: vec![exst!(s "4")], + else_case: Some(vec![exst!(s "9")]) } + ) + } ) } parse_test_wrap_ast! { "if x is Something { a, b: x } then { 4 } else { 9 }", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Record(qname!(Something), vec![ - (rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))), - (rc!(b),Pattern::VarOrName(qname!(x))) + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::Record(qname!(Something), vec![ + (rc!(a),Pattern::Literal(PatternLiteral::StringPattern(rc!(a)))), + (rc!(b),Pattern::VarOrName(qname!(x))) ]), - vec![exst!(s "4")], Some(vec![exst!(s "9")]))) + then_case: vec![exst!(s "4")], + else_case: Some(vec![exst!(s "9")]) + } + ) } ) } @@ -656,12 +664,12 @@ fn pattern_literals() { "if x is -1 then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }), - vec![exst!(NatLiteral(1))], - Some(vec![exst!(NatLiteral(2))]), - )) + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::Literal(PatternLiteral::NumPattern { neg: true, num: NatLiteral(1) }), + then_case: vec![exst!(NatLiteral(1))], + else_case: Some(vec![exst!(NatLiteral(2))]), + }) } ) } @@ -670,12 +678,12 @@ fn pattern_literals() { "if x is 1 then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }), - vec![exst!(s "1")], - Some(vec![exst!(s "2")]), - )) + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1) }), + then_case: vec![exst!(s "1")], + else_case: Some(vec![exst!(s "2")]), + }) } ) } @@ -684,12 +692,13 @@ fn pattern_literals() { "if x is true then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::BoolPattern(true)), - vec![exst!(NatLiteral(1))], - Some(vec![exst!(NatLiteral(2))]), - )) + discriminator: Some(bx!(ex!(s "x"))), + body: bx!( + IfExpressionBody::SimplePatternMatch { + pattern: Pattern::Literal(PatternLiteral::BoolPattern(true)), + then_case: vec![exst!(NatLiteral(1))], + else_case: Some(vec![exst!(NatLiteral(2))]), + }) } ) } @@ -698,12 +707,12 @@ fn pattern_literals() { "if x is \"gnosticism\" then 1 else 2", exst!( IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::SimplePatternMatch( - Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))), - vec![exst!(s "1")], - Some(vec![exst!(s "2")]), - )) + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::SimplePatternMatch { + pattern: Pattern::Literal(PatternLiteral::StringPattern(rc!(gnosticism))), + then_case: vec![exst!(s "1")], + else_case: Some(vec![exst!(s "2")]), + }) } ) } @@ -753,7 +762,7 @@ fn imports_3() { fn imports_4() { parse_test_wrap_ast! { "import bespouri::*", - import!(ImportSpecifier { + import!(ImportSpecifier { id: ItemIdStore::new_id(), path_components: vec![rc!(bespouri)], imported_names: ImportedNames::All @@ -768,15 +777,17 @@ fn if_expr() { "if x { 1 then 5, else 20 }", exst! { IfExpression { - discriminator: bx!(Discriminator::Simple(ex!(s "x"))), - body: bx!(IfExpressionBody::GuardList( + discriminator: Some(bx!(ex!(s "x"))), + body: bx!(IfExpressionBody::CondList( vec![ - GuardArm { - guard: Guard::Pat(Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1)})), + ConditionArm { + condition: Condition::Pattern(Pattern::Literal(PatternLiteral::NumPattern { neg: false, num: NatLiteral(1)})), + guard: None, body: vec![exst!(s "5")], }, - GuardArm { - guard: Guard::None, + ConditionArm { + condition: Condition::Else, + guard: None, body: vec![exst!(s "20")], }, ]