🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

boolean operator xor

Started by
7 comments, last by zola 19 years, 8 months ago
Hi a boolean xor / ^ would be convenient. Thanks
Advertisement
Does it exist in C++?

I believe that for booleans the not-equal operator, !=, does the exact same thing as xor.

0 != 0 -> 0
0 != 1 -> 1
1 != 0 -> 1
1 != 1 -> 0

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Yeah [smile]
But readability for unexperienced programmers goes down if you use != in a long boolean expression.

I would have needed it for a little program where students can learn to convert logical expressions. It would have been nice to have an easy way to evaluate the resulting string. With anglescript executeString this would have been a peace of cake [cool]

However I'll have to do the find and replace way then

Cheers
Tom
For a quick fix you could just create an alias for the != token. You just need to change the as_tokendef.h file.

I'll probably end up adding the operator in the library, now that you gave me the idea. Especially since it is so easy to do.

Any suggestions on what the operator should look like? I'm thinking 'xor'/'^^' to keep consistency with 'and'/'&&', and 'or'/'||'.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Done! [smile]

I'll describe the necessary changes here soon.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

as_tokendef.h

Add the following line to enum eTokenType
	ttXor,                 // xor

Add the following lines to sTokenWord const tokenWords[]
	{"^^"      , ttXor},	{"xor"     , ttXor},

as_parser.cpp

Change the if statement in IsDeclaration() to
	if( t2.type == ttStar || t2.type == ttOpenBracket || t2.type == ttIdentifier )		return true;

Add the following line to IsOperator(int tokenType)
		tokenType == ttXor ||

as_compiler.cpp

Change the if statement in GetPrecedence() to
	if( tokenType == ttEqual || tokenType == ttNotEqual || tokenType == ttXor )

Change all if statements in CompileOperatorOnBool() to let ttXor pass where ttNotEqual passes.
	if( op == ttEqual || op == ttNotEqual || op == ttXor )

and
			else if( op == ttNotEqual || op == ttXor )

and
			else if( op == ttNotEqual || op == ttXor )


Let me know if it works ok.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Wow, thanks a lot man.

It didn't work though :)
I have now a compile error with this statement:
// 'entity' and 'ud' are pointer to registered classes ...if(entity==0 || ud==0) return;


compiler message:
media/scripts/EditorUtilities.asc (40, 1) : Info : Compiling void UpdateEntity(IEntity*, CEditorUserData*)

media/scripts/EditorUtilities.asc (42, 15) : Error : Illegal operation on this datatype

media/scripts/EditorUtilities.asc (42, 20) : Error : '==' is not available for '<unrecognized token>'

media/scripts/EditorUtilities.asc (42, 5) : Error : Expression must be of boolean type

CScriptServerAngelScript::load() Build failed. [module: escape_utilities, file: media/scripts/EditorUtilities.asc]

This didn't give errors before I did the changes.

Nevermind, I can just as well exchange all occurances of XOR with != in the statements I want to check.

Thanks
Tom
Strange, the alterations that you made shouldn't affect the compilation of that expression. What you made was basically the addition of an alias for !=, but only for boolean expressions.

I might have given the wrong instructions for making the alterations. Though I can't see what it is.

Oh, well. The new operator is in the library now, and as of the next WIP release you will be able to use 'xor' or '^^' on booleans.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Guess I'll have to switch to 1.10 then :)

This topic is closed to new replies.

Advertisement