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

Left hand side of subtraction misinterpreted as cast #155

Open
void80 opened this issue Feb 6, 2024 · 2 comments
Open

Left hand side of subtraction misinterpreted as cast #155

void80 opened this issue Feb 6, 2024 · 2 comments
Assignees

Comments

@void80
Copy link

void80 commented Feb 6, 2024

Have a look at the following test cases

    [TestMethod]
    [DataRow("#1", "a1", 1)]
    [DataRow("#2", "(a1)", 1)]
    [DataRow("#3", "(a1)+(a1)", 2)]
    [DataRow("#4", "(a1)+a1", 2)]
    [DataRow("#5", "(a1)-(a1)", 0)]
    [DataRow("#6", "(a1)-a1", 0)]
    public void TestSimpleFalseCastExpressions(string id, string expression, int expected)
    {
        var context = new EvalContext();

        var types = new Dictionary<string, Type>
        {
            { "a1", typeof(double) },
        };

        var values = new Dictionary<object, double>
        {
            { "a1", 1.0 },
        };

        var compiled = context.Compile(expression, types);
        var result = compiled.Invoke(values);

        result.Should().Be(expected);
    }

The cases #5 and #6 fail with the following exception:

Test method EvalExpressionsTests.TestSimpleFalseCastExpressions threw exception: 
Z.Expressions.Compiler.Shared.EvalException: Oops! The type was not found.. The error occurred for expression "(" at position 0 near "(a1)-(a1)".
    at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at .(ExpressionScope , SyntaxNode , Expression , Boolean )
   at Z.Expressions.CodeCompiler.CSharp.ExpressionParser.ParseSyntax(ExpressionScope scope, SyntaxNode node, Type resultType)
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , ExpressionScope , String , Boolean , Boolean )
   at .[](EvalContext , String , IDictionary`2 , Type , EvalCompilerParameterKind , Boolean , Boolean , Boolean )
   at Z.Expressions.EvalContext.Compile(String code, IDictionary`2 parameterTypes)
   at EvalExpressionsTests.TestSimpleFalseCastExpressions(String id, String expression, Int32 expected) in EvalExpressionsTests.cs:line 141
   at InvokeStub_EvalExpressionsTests.TestSimpleFalseCastExpressions(Object, Span`1)
   at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

My suspicion is that (a1) is (wrongly) interpreted as cast when followed by a - sign, because the - is interpreted as unary negation operator.

I would expect the parser to be able to distinguish between a real cast and an expression in parens.

For example "(double) -1" should be identified as a cast, "(a1)-1" should be identified as a subtraction.

@JonathanMagnan JonathanMagnan self-assigned this Feb 6, 2024
@JonathanMagnan
Copy link
Member

Hello @void80 ,

Thank you for reporting.

Your suspicion is probably 100% right. I remember we fixed a similar issue recently but slightly different #154 (he wanted to cast it on his side).

We will look at it.

Best Regards,

Jon

@strohhaecker
Copy link

We've figured out that a downgrade to 6.0.1 works around the problem, in case this helps you.
Best regards,
Sebastian

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

No branches or pull requests

3 participants