Skip to content

Parsing expression from string

WhiteBlackGoose edited this page Feb 15, 2023 · 1 revision

There are a few ways to parse an expression from string.

With FromString

using AngouriMath;

var expr = MathS.FromString("1 + 1");

It will throw an exception if input is bad.

With implicit cast

using AngouriMath;

Entity expr = "1 + 1";

It will also throw an exception in case of bad input.

With method Parse

Unlike others, this one won't throw. However, it returns an object which requries validation:

var expr5 = Parse("a + b").Switch(
    res => res,
    failure => failure.Reason.Switch<object>(
        unknown => throw new("Unknown reason"),
        missingOp => throw new("Missing operator"),
        internalError => throw new("Internal error") 
    )
);
Clone this wiki locally