This commit is contained in:
Greg Shuflin 2021-11-18 22:12:13 -08:00
parent 7e2b95593f
commit 44cebec818
2 changed files with 38 additions and 2 deletions

View File

@ -448,6 +448,7 @@ fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
"primary-expr-no-struct", "primary-expr-no-struct",
alt(( alt((
while_expr, while_expr,
for_expr,
list_expr, list_expr,
paren_expr, paren_expr,
string_literal, string_literal,
@ -481,6 +482,41 @@ fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
})(input) })(input)
} }
fn for_expr(input: Span) -> ParseResult<ExpressionKind> {
println!("IN FOR EXPR: {}", input.fragment());
fn for_enumerators(input: Span) -> ParseResult<Vec<Enumerator>> {
println!("NEUMS: {}", input.fragment());
alt((
delimited(tok(char('{')), separated_list0(tok(char(',')), enumerator), tok(char('}'))),
map(enumerator, |enumerator| vec![enumerator]),
))(input)
}
//TODO add guards, etc.
fn enumerator(input: Span) -> ParseResult<Enumerator> {
alt((
map(separated_pair(tok(identifier), kw("<-"), expression_no_struct), |(ident, generator)| {
Enumerator { id: rc_string(ident.fragment()), generator }
}),
//TODO distinguish these two cases in AST
map(separated_pair(tok(identifier), kw("="), expression_no_struct), |(ident, generator)| {
Enumerator { id: rc_string(ident.fragment()), generator }
}),
))(input)
}
fn for_body(input: Span) -> ParseResult<Box<ForBody>> {
alt((
preceded(kw("return"), map(expression_no_struct, |expr| Box::new(ForBody::MonadicReturn(expr)))),
map(block, |body| Box::new(ForBody::StatementBlock(body))),
))(input)
}
map(preceded(kw("for"), pair(for_enumerators, for_body)), |(enumerators, body)| {
ExpressionKind::ForExpression { enumerators, body }
})(input)
}
fn paren_expr(input: Span) -> ParseResult<ExpressionKind> { fn paren_expr(input: Span) -> ParseResult<ExpressionKind> {
delimited( delimited(
tok(char('(')), tok(char('(')),

View File

@ -389,7 +389,7 @@ fn while_expression() {
fn for_expression() { fn for_expression() {
use ExpressionKind::*; use ExpressionKind::*;
assert_expr!( assert_expr_comb!(
"for { a <- garodzny::maybeValue } return 1", "for { a <- garodzny::maybeValue } return 1",
expr(ForExpression { expr(ForExpression {
enumerators: vec![Enumerator { id: rc("a"), generator: expr(Value(qn!(garodzny, maybeValue))) }], enumerators: vec![Enumerator { id: rc("a"), generator: expr(Value(qn!(garodzny, maybeValue))) }],
@ -397,7 +397,7 @@ fn for_expression() {
}) })
); );
assert_expr!( assert_expr_comb!(
"for n <- someRange { f(n) ; }", "for n <- someRange { f(n) ; }",
expr(ForExpression { expr(ForExpression {
enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }], enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }],