add support for parsing parameterless lambdas

This commit is contained in:
Dave Holoway
2020-06-16 15:42:48 +01:00
parent 8b71037a58
commit 9ac6086bad
2 changed files with 29 additions and 2 deletions

View File

@@ -522,6 +522,18 @@ class BracketedExpression extends Expression {
this.expression = expression;
}
}
class LambdaExpression extends Expression {
/**
*
* @param {*[]} params
* @param {Expression|Block} body
*/
constructor(params, body) {
super();
this.params = params;
this.body = body;
}
}
class IncDecExpression extends Expression {
/**
* @param {ResolvedIdent} expr
@@ -1674,6 +1686,19 @@ function rootTerm(tokens, mdecls, scope, imports, typemap) {
return newTerm(tokens, mdecls, scope, imports, typemap);
case 'open-bracket':
tokens.inc();
if (tokens.isValue(')')) {
// parameterless lambda
tokens.expectValue('->');
let ident, lambdaBody = null;
if (tokens.current.value === '{') {
// todo - parse lambda body
skipBody(tokens);
} else {
lambdaBody = expression(tokens, mdecls, scope, imports, typemap);
ident = `() -> ${lambdaBody.source}`;
}
return new ResolvedIdent(ident, [new LambdaExpression([], lambdaBody)]);
}
matches = expression(tokens, mdecls, scope, imports, typemap);
tokens.expectValue(')');
if (isCastExpression(tokens.current, matches)) {