Record types

This commit is contained in:
Alex Bethel 2022-08-07 13:03:09 -05:00
parent 02134f542c
commit 00a9ac7cf7

View file

@ -485,18 +485,22 @@ fn parse_literal(_m: &ParserMeta) -> impl Parser<char, Expr, Error = Simple<char
fn parse_type(m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> { fn parse_type(m: &ParserMeta) -> impl Parser<char, Type, Error = Simple<char>> {
recursive(|rec| { recursive(|rec| {
choice((parse_named_type(m), parse_tuple_type(m, rec))) choice((
.repeated() parse_named_type(m),
.at_least(1) parse_tuple_type(m, rec.clone()),
.map(|types| { parse_record_type(m, rec.clone()),
types ))
.into_iter() .repeated()
.reduce(|l, r| Type::Application { .at_least(1)
function: Box::new(l), .map(|types| {
expression: Box::new(r), types
}) .into_iter()
.unwrap() .reduce(|l, r| Type::Application {
}) function: Box::new(l),
expression: Box::new(r),
})
.unwrap()
})
}) })
} }
@ -513,6 +517,7 @@ fn parse_tuple_type(
.delimited_by(just('(').then(whitespace()), just(')').then(whitespace())) .delimited_by(just('(').then(whitespace()), just(')').then(whitespace()))
.map(|types| { .map(|types| {
if types.len() == 1 { if types.len() == 1 {
// `(Int)` is the same as `Int`
types.into_iter().next().unwrap() types.into_iter().next().unwrap()
} else { } else {
Type::Tuple(types) Type::Tuple(types)
@ -520,6 +525,21 @@ fn parse_tuple_type(
}) })
} }
fn parse_record_type(
_m: &ParserMeta,
rec: impl Parser<char, Type, Error = Simple<char>>,
) -> impl Parser<char, Type, Error = Simple<char>> {
ident()
.then_ignore(whitespace())
.then_ignore(just(':'))
.then_ignore(whitespace())
.then(rec)
.separated_by(just(',').then(whitespace()))
.allow_trailing()
.delimited_by(just('{').then(whitespace()), just('}').then(whitespace()))
.map(Type::Record)
}
fn parse_pattern(m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> { fn parse_pattern(m: &ParserMeta) -> impl Parser<char, Pattern, Error = Simple<char>> {
// TODO: add all the patterns // TODO: add all the patterns
choice((parse_capture_pattern(m),)) choice((parse_capture_pattern(m),))