work on adding when clause
This commit is contained in:
parent
118a5ac54e
commit
722dc4b477
|
@ -227,7 +227,7 @@ pub enum UntypedExpr {
|
||||||
annotation: Option<Annotation>,
|
annotation: Option<Annotation>,
|
||||||
},
|
},
|
||||||
|
|
||||||
Case {
|
When {
|
||||||
location: Span,
|
location: Span,
|
||||||
subjects: Vec<Self>,
|
subjects: Vec<Self>,
|
||||||
clauses: Vec<Clause<Self, (), (), ()>>,
|
clauses: Vec<Clause<Self, (), (), ()>>,
|
||||||
|
@ -327,7 +327,7 @@ impl UntypedExpr {
|
||||||
| Self::Var { location, .. }
|
| Self::Var { location, .. }
|
||||||
| Self::Int { location, .. }
|
| Self::Int { location, .. }
|
||||||
| Self::Todo { location, .. }
|
| Self::Todo { location, .. }
|
||||||
| Self::Case { location, .. }
|
| Self::When { location, .. }
|
||||||
| Self::Call { location, .. }
|
| Self::Call { location, .. }
|
||||||
| Self::List { location, .. }
|
| Self::List { location, .. }
|
||||||
| Self::Float { location, .. }
|
| Self::Float { location, .. }
|
||||||
|
|
|
@ -323,17 +323,43 @@ pub fn expr_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
let expr_block_parser = just(Token::LeftBrace)
|
let when_clause_guard_parser = just(Token::If);
|
||||||
.ignore_then(r.clone().then(r.repeated()))
|
|
||||||
.then_ignore(just(Token::RightBrace))
|
|
||||||
.map_with_span(|(a, b), span| {
|
|
||||||
let mut expressions = vec![a];
|
|
||||||
expressions.extend(b);
|
|
||||||
|
|
||||||
expr::UntypedExpr::Sequence {
|
let when_clause_parser = pattern_parser()
|
||||||
|
.separated_by(just(Token::Comma))
|
||||||
|
.then(
|
||||||
|
pattern_parser()
|
||||||
|
.separated_by(just(Token::Comma))
|
||||||
|
.separated_by(just(Token::Vbar))
|
||||||
|
.allow_leading()
|
||||||
|
.or_not(),
|
||||||
|
)
|
||||||
|
.then(when_clause_guard_parser)
|
||||||
|
// TODO: add hint "Did you mean to wrap a multi line clause in curly braces?"
|
||||||
|
.then_ignore(just(Token::RArrow))
|
||||||
|
.then(r.clone())
|
||||||
|
.map_with_span(
|
||||||
|
|(((patterns, alternative_patterns_opt), guard), then), span| ast::UntypedClause {
|
||||||
location: span,
|
location: span,
|
||||||
expressions,
|
pattern: patterns,
|
||||||
}
|
alternative_patterns: alternative_patterns_opt.unwrap_or_default(),
|
||||||
|
guard,
|
||||||
|
then,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let when_parser = just(Token::When)
|
||||||
|
// TODO: If subject is empty we should return ParseErrorType::ExpectedExpr,
|
||||||
|
.ignore_then(r.clone().separated_by(just(Token::Comma)))
|
||||||
|
.then_ignore(just(Token::Is))
|
||||||
|
.then_ignore(just(Token::LeftBrace))
|
||||||
|
// TODO: If clauses are empty we should return ParseErrorType::NoCaseClause
|
||||||
|
.then(when_clause_parser.repeated())
|
||||||
|
.then_ignore(just(Token::RightBrace))
|
||||||
|
.map_with_span(|(subjects, clauses), span| expr::UntypedExpr::When {
|
||||||
|
location: span,
|
||||||
|
subjects,
|
||||||
|
clauses,
|
||||||
});
|
});
|
||||||
|
|
||||||
let expr_unit_parser = choice((
|
let expr_unit_parser = choice((
|
||||||
|
@ -343,7 +369,7 @@ pub fn expr_parser() -> impl Parser<Token, expr::UntypedExpr, Error = ParseError
|
||||||
todo_parser,
|
todo_parser,
|
||||||
list_parser,
|
list_parser,
|
||||||
assignment_parser,
|
assignment_parser,
|
||||||
expr_block_parser,
|
when_parser,
|
||||||
))
|
))
|
||||||
.boxed();
|
.boxed();
|
||||||
|
|
||||||
|
|
|
@ -52,10 +52,12 @@ fn module() {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wow2(a: Int){
|
pub fn wow2(a: Int){
|
||||||
let x = 1 + {
|
when a is {
|
||||||
2 + 7 * 8
|
0 -> 3
|
||||||
} / 2
|
1 | 2 -> 5
|
||||||
x
|
3 if a == 3 -> 9
|
||||||
|
_ -> 4
|
||||||
|
}
|
||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
let len = code.chars().count();
|
let len = code.chars().count();
|
||||||
|
|
Loading…
Reference in New Issue