Skip to content

Commit

Permalink
Added support for new anonymous syntax for ast module.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed May 25, 2023
1 parent 0ec3d1f commit 478885d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 25 additions & 3 deletions libs/ast/parser.b
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ 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()
if self._match(BAR) return self._anonymous_compat()
if self._match(AT) return self._anonymous()

return nil
}
Expand Down Expand Up @@ -896,9 +897,9 @@ class Parser {
}

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

while !self._check(BAR) {
Expand All @@ -915,6 +916,27 @@ class Parser {
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")
}

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

return FunctionDecl('', params, body)
}

/**
* function definitions
*/
Expand Down
8 changes: 7 additions & 1 deletion libs/ast/scanner.b
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,13 @@ class Scanner {
}
when ',' self._add_token(COMMA)
when ';' self._add_token(SEMICOLON)
when '@' self._decorator()
when '@' {
if !self._is_alpha(self._peek()) {
self._add_token(AT)
} else {
self._decorator()
}
}
when '.' {
if self._match('.') {
self._add_token(self._match('.') ? TRI_DOT : RANGE)
Expand Down

0 comments on commit 478885d

Please sign in to comment.