For expr
This commit is contained in:
parent
7e2b95593f
commit
44cebec818
@ -448,6 +448,7 @@ fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
|
||||
"primary-expr-no-struct",
|
||||
alt((
|
||||
while_expr,
|
||||
for_expr,
|
||||
list_expr,
|
||||
paren_expr,
|
||||
string_literal,
|
||||
@ -481,6 +482,41 @@ fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
|
||||
})(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> {
|
||||
delimited(
|
||||
tok(char('(')),
|
||||
|
@ -389,7 +389,7 @@ fn while_expression() {
|
||||
fn for_expression() {
|
||||
use ExpressionKind::*;
|
||||
|
||||
assert_expr!(
|
||||
assert_expr_comb!(
|
||||
"for { a <- garodzny::maybeValue } return 1",
|
||||
expr(ForExpression {
|
||||
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) ; }",
|
||||
expr(ForExpression {
|
||||
enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }],
|
||||
|
Loading…
Reference in New Issue
Block a user