Skip to content

Commit

Permalink
made parenthesis optional for anonymous functions that take no parame…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
mcfriend99 committed Sep 25, 2024
1 parent eac79fd commit 8204189
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 48 deletions.
39 changes: 11 additions & 28 deletions libs/ast/parser.b
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ class Parser {
if self._match(LPAREN) return self._grouping()
if self._match(LBRACE) return self._dict()
if self._match(LBRACKET) return self._list()
if self._match(BAR) return self._anonymous_compat()
if self._match(AT) return self._anonymous()

return nil
Expand Down Expand Up @@ -920,41 +919,25 @@ class Parser {
return result
}

/**
* anonymous compartibility functions
*/
_anonymous_compat() {
var params = []

while !self._check(BAR) {
params.append(self._consume_any('parameter name expected', IDENTIFIER, TRI_DOT).literal)

if !self._check(BAR)
self._consume(COMMA, "',' expected between function params")
}

self._consume(BAR, "'|' expected after anonymous function args")
self._consume(LBRACE, "'{' expected after function declaration")
var body = self._block()

return FunctionDecl('', params, body)
}

/**
* anonymous functions
*/
_anonymous() {
var params = []
self._consume(LPAREN, "expected '(' at start of anonymous function")

while !self._check(RPAREN) {
params.append(self._consume_any('parameter name expected', IDENTIFIER, TRI_DOT).literal)

if !self._check(RPAREN)
self._consume(COMMA, "',' expected between function params")
if self._check(LPAREN) {
self._consume(LPAREN, "expected '(' at start of anonymous function")

while !self._check(RPAREN) {
params.append(self._consume_any('parameter name expected', IDENTIFIER, TRI_DOT).literal)

if !self._check(RPAREN)
self._consume(COMMA, "',' expected between function params")
}

self._consume(RPAREN, "expected ')' after anonymous function parameters")
}

self._consume(RPAREN, "expected ')' after anonymous function parameters")
self._consume(LBRACE, "'{' expected after function declaration")
var body = self._block()

Expand Down
27 changes: 7 additions & 20 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,6 @@ static void statement(b_parser *p);
static void declaration(b_parser *p);

static void anonymous(b_parser *p, bool can_assign);
static void anonymous_compat(b_parser *p, bool can_assign);

static b_parse_rule *get_rule(b_tkn_type type);

Expand Down Expand Up @@ -1350,7 +1349,7 @@ b_parse_rule parse_rules[] = {
[PERCENT_EQ_TOKEN] = {NULL, NULL, PREC_NONE}, // %=
[AMP_TOKEN] = {NULL, binary, PREC_BIT_AND}, // &
[AMP_EQ_TOKEN] = {NULL, NULL, PREC_NONE}, // &=
[BAR_TOKEN] = {anonymous_compat, binary, PREC_BIT_OR}, // |
[BAR_TOKEN] = {NULL, binary, PREC_BIT_OR}, // |
[BAR_EQ_TOKEN] = {NULL, NULL, PREC_NONE}, // |=
[TILDE_TOKEN] = {unary, NULL, PREC_UNARY}, // ~
[TILDE_EQ_TOKEN] = {NULL, NULL, PREC_NONE}, // ~=
Expand Down Expand Up @@ -1557,25 +1556,13 @@ static void anonymous(b_parser *p, bool can_assign) {
begin_scope(p);

// compile parameter list
consume(p, LPAREN_TOKEN, "expected '(' at start of anonymous function");
if (!check(p, RPAREN_TOKEN)) {
function_args(p);
}
consume(p, RPAREN_TOKEN, "expected ')' after anonymous function parameters");

function_body(p, &compiler, true);
}

static void anonymous_compat(b_parser *p, bool can_assign) {
b_compiler compiler;
init_compiler(p, &compiler, TYPE_FUNCTION);
begin_scope(p);

// compile parameter list
if (!check(p, BAR_TOKEN)) {
function_args(p);
if(check(p, LPAREN_TOKEN)) {
consume(p, LPAREN_TOKEN, "expected '(' at start of anonymous function");
if (!check(p, RPAREN_TOKEN)) {
function_args(p);
}
consume(p, RPAREN_TOKEN, "expected ')' after anonymous function parameters");
}
consume(p, BAR_TOKEN, "expected '|' after anonymous function parameters");

function_body(p, &compiler, true);
}
Expand Down

0 comments on commit 8204189

Please sign in to comment.