Skip to content

Commit 254b640

Browse files
gh-199: Fix LAMBDA.
Closes #199.
1 parent 0d7e115 commit 254b640

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/parser.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ static bool require_space_only_gap(Parser *parser, const Token *left, const Toke
135135
int gap_start_col;
136136
int gap_end_col;
137137

138-
if (!left || !right || right->type != TOKEN_IDENT) {
138+
/* Allow the right token to be an identifier or the '(' that begins a
139+
parameter list. This lets constructs like `LAMBDA INT (INT x)` use
140+
the same space-only gap rules as typed function declarations. */
141+
if (!left || !right || (right->type != TOKEN_IDENT && right->type != TOKEN_LPAREN)) {
139142
report_error(parser, message);
140143
return false;
141144
}
@@ -632,14 +635,22 @@ static Expr *parse_primary(Parser *parser) {
632635
}
633636
if (match(parser, TOKEN_LAMBDA)) {
634637
Token lambda_tok = token;
635-
/* LAMBDA R: ( params ) { body } */
638+
/* LAMBDA R ( params ) { body } -- also accept legacy 'R: (' */
636639
if (!is_type_token(parser->current_token.type)) {
637640
report_error(parser, "Expected return type after LAMBDA");
638641
return NULL;
639642
}
640643
DeclType ret = parse_type_name(parser->current_token.literal);
641-
advance(parser);
642-
consume(parser, TOKEN_COLON, "Expected ':' after return type");
644+
645+
/* Enforce the spec-compliant space-separated form `INT (` and
646+
explicitly reject the legacy colon form `INT: (`. Use the same
647+
space-only gap rules as for named function declarations so
648+
that line-continuations and comments are handled consistently. */
649+
if (!require_space_only_gap(parser, &parser->current_token, &parser->next_token, k_type_name_gap_error)) {
650+
return NULL;
651+
}
652+
advance(parser); /* consume return type -> current_token becomes '(' */
653+
643654
consume(parser, TOKEN_LPAREN, "Expected '(' after LAMBDA parameter list");
644655

645656
ParamList params = {0};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ASSIGN(f, LAMBDA INT: (INT x) { RETURN(x) })
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ASSIGN(FUNC f, LAMBDA INT (INT x) { RETURN(x) })
2+
ASSERT(EQ(f(0d2), 0d2))

0 commit comments

Comments
 (0)