Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Request: Expanding LinqyCalculator example #188

Open
dannybucks opened this issue Sep 9, 2022 · 1 comment
Open

Support Request: Expanding LinqyCalculator example #188

dannybucks opened this issue Sep 9, 2022 · 1 comment

Comments

@dannybucks
Copy link

This is an awesome library to help in this very complex topic!
I tried to expand the Sprache/samles/LinqyCalculator/ExpressionParser.cs to provide more functions than contained in System.Math. I was somehow unable to achieve that:

  1. Functions could have no parameters.
  2. Functions would accept strings, so that e.g. customFunc("Hello", "World") could be parsed (I was able to add "customFunc", but only with double values as parameters)

Can you maybe explain and show how I could achieve this?

@IanWold
Copy link
Contributor

IanWold commented Oct 18, 2022

Hello! Sorry for being a bit late to reply here, I think I can help a bit.

In order to get functions without parameters, you'll need to make the expr parser within the Function parser optional. This has a consequence when you pass the expressions into CallFunction since you'll need to handle the case where there are no parameters. This is how I rewrote the Function parser with that:

static readonly Parser<Expression> Function =
    from name in Parse.Letter.AtLeastOnce().Text()
    from lparen in Parse.Char('(')
    from expr in Parse.Ref(() => Expr).DelimitedBy(Parse.Char(',').Token()).Optional()
    from rparen in Parse.Char(')')
    select CallFunction(name,
        expr.IsDefined
        ? expr.Get().ToArray()
        : Array.Empty<Expression>()
    );

In order to support strings, you'll need to add a new parser for string constants, and the interesting work would be how to integrate that constant. I'm assuming that you don't want the strings to be able to be added as part of an arithmetic expression, but rather just that they can be used as an argument in a function. In order to do that, you should be able to update the Function parser to derive its argument-expressions either from Expr or your new string constant parser. Here's my implementation of that:

static readonly Parser<Expression> StringConstant =
    from quote in Parse.Char('"')
    from remainder in Parse.Until(Parse.AnyChar, Parse.Char('"')).Text()
    select Expression.Constant(remainder);

static readonly Parser<Expression> Function =
    from name in Parse.Letter.AtLeastOnce().Text()
    from lparen in Parse.Char('(')
    from expr in StringConstant.XOr(Parse.Ref(() => Expr)).DelimitedBy(Parse.Char(',').Token()).Optional()
    from rparen in Parse.Char(')')
    select CallFunction(name,
        expr.IsDefined
        ? expr.Get().ToArray()
        : Array.Empty<Expression>()
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants