Skip to content

Why are operators limited to "two" characters ? #27

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

Closed
kasajian opened this issue Mar 27, 2019 · 6 comments
Closed

Why are operators limited to "two" characters ? #27

kasajian opened this issue Mar 27, 2019 · 6 comments

Comments

@kasajian
Copy link

Method name "EvaluateTwoCharsOperators" implies it.

If one were to add "And" and "Or" operators as aliases for && and ||, the current implementation wouldn't work because "And" has three characters.

Making this change seems to work, but not sure it's the best way:

BEFORE

    private bool EvaluateTwoCharsOperators(string expr, Stack<object> stack, ref int i)
    {
        if (i < expr.Length - 1)
        {
            string op = expr.Substring(i, 2);
            if (operatorsDictionary.ContainsKey(op))
            {
                stack.Push(operatorsDictionary[op]);
                i++;
                return true;
            }
        }

        return false;
    }

AFTER

    private bool EvaluateTwoCharsOperators(string expr, Stack<object> stack, ref int i)
    {
        if (i < expr.Length - 1)
        {
            string op = expr.Substring(i, 2);
            var containsKey = operatorsDictionary.ContainsKey(op);
            if (!containsKey && expr.Length >= i + 3)
            {
                op = expr.Substring(i, 3);
                containsKey = operatorsDictionary.ContainsKey(op);
            }
            if (containsKey)
            {
                stack.Push(operatorsDictionary[op]);
                i += op.Length - 1;
                return true;
            }
        }

        return false;
    }
@codingseb
Copy link
Owner

Ok. I need to think a little about this one.
I didn't provide that somebody want to change operators.

I will also check if operators like or and are not in conflict with the way it parse and evaluate variables.

@kasajian
Copy link
Author

I guess I am making the assumption that this evaluator is meant to allow one to tweak the syntax depending on need. But Maybe that's wrong and this is really supposed to be primarily a C# syntax evaluator.

I need an evaluator that has a preexisting defined syntax -- and it's a combination of BASIC and C where you have both && and 'and', || and 'or', also both "=" and "==" work. There's no assignments -- just expressions.

So I think there's a decision to be made here. Decide that this code-base is not fit-for-purpose for my needs, Or, put the code-base on a path such that some of these things are user-extensible. For instance, perhaps some future version of the library can allow for the above mentioned changes be configured dynamically rather than change the source.

@codingseb
Copy link
Owner

codingseb commented Mar 28, 2019

Yes. A goal of this evaluator is to be as near as possible of C# by default.

But you can of course try to adapt it to your need if it works.

I think modify existing operators is quite easy but you need to modify ExpressionEvaluator file itself as you say. If a way to modify or extend how operators work from outside of the file could exists it would be better. It could be also easier to update the library. So I will think about it for future versions.

At first EvaluationExpression was just a small thing that I made for my own needs. I just decided at some point to share it so it can maybe help others. I appreciate ideas like yours because it help this project to live. It's just that I didn't think about this specific idea until now.
So it will probably take some time until something fully extensible will be available.
And it will also probably need some deeper changes and refactoring of the lib.

@codingseb
Copy link
Owner

Just to keep track of some ideas related to it :

  • Add a way to allow aliases for existing operators like Or, And
  • Add a way to add some custom operators and corresponding implementations
  • Flexible length of the operators
  • Types : multiple operands vs Single operands (Maybe start with only 2 operands operators)
  • Take care of operators precedences (priorities)

Examples :

x love y;
a kill b;
Not v;
~?variable;
c @+ h;
a @ b # c;
//...

@kasajian
Copy link
Author

Yes, very cool. I also like the fact that it's all in one file. thanks.

@codingseb
Copy link
Owner

codingseb commented May 28, 2019

OK I think the new release 1.4.0.0 resolve this issue. See this for more infos

it took some time, but it's here.

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

No branches or pull requests

2 participants