How to "overload" AND with &

  • Thread starter Thread starter Kim Stubberup
  • Start date Start date
K

Kim Stubberup

I am making a script parser and I need to interpret the word AND and
"replace" it with an &, so that it can be compiled at runtime with the
C# compiler.

My script would read e.g: if( (iCounter1 < 20) AND (iCounter2 <
iCounter1) )

How can I make the compiler understand that AND means &? (or means &&)

I do not want to use the Replace("AND","&") on the string I read,
because this gives me some validation implementation which I want to
avoid.

I have seen some nasty operator overloads of ** doing what Math.Pow
does and I hope there could be a way to do a reverse operator overload
of some kind on the AND to return &. (or &&)

Can I define an operator called AND which would be the same as & .....?

I have looked at the opCode-thing but found nothing.

Hmm, hope somebody has a good answer to this one. Any input is more
than welcome.
Thanks in advance.
 
Hi,

If it's the token "AND" that your require for "&&" functionality then you'll
have to look for it when you parse the line of code. In other words, your
parser must expect an operator at the current position in the string, which
must be either AND or OR, for example. Any other value should throw an
exception.

Note: You have to determine whether you really mean & or && since they are
different. I assume it's actually the boolean && that you want.

Instead of creating your own scripting language, it might be better to
create a sandboxed AppDomain in which to compile and load the code, as is.
That way any .NET language could be chosen, even JScript.NET.
 
Note: You have to determine whether you really mean & or && since they are
different. I assume it's actually the boolean && that you want.

& works also for booleans, it's an "AND" without short-circuit
evaluation, so both sides are always evaluated (whereas && only
evaluates the second expression when the first returned true).

and for completeness: & for int's is a bitwise-AND

Hans Kesting
 
Hi Hans,

True, but that doesn't mean that he can use either one arbitrarily. If he's
designing a language he has to choose a mapping for AND to && or &, but
obviously it can't be both - they are different.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top