Jump to content
  • Advertisement
Jason Goepel

Bitwise Operator

Recommended Posts

I would like to request an engine property that would disable bitwise operations on non-integer types, producing a compile error.

I have many users whose only coding experience is in Excel, either through Excel formulas or VBA.  Both of those languages use the ^ operator as a "pow" operator, so these users will often make the mistake of using the ^ operator in AngelScript.  In almost every case, these users are using the ^ operator by mistake with at least one floating-point type or a class that can convert to a floating-point type.  A compile error that catches "(double) ^ (int)" would help these users learn that ^ is an XOR operator.

Since the engine doesn't actually support bitwise, floating-point operations (instead, implicitly converting floating-point types to integer types before the operation), it doesn't seem like an option to produce a compile error would cause much trouble.

Let me know what you think.  Thanks.

Share this post


Link to post
Share on other sites
Advertisement

I've implemented this now. 

I decided not to add any engine property though. Perhaps if someone has a need for it I'll add it, but for now the compiler will always give an error when attempting bitwise operations with float or double.

Share this post


Link to post
Share on other sites

Thanks for implementing this.  I do have one question.  I notice you are testing the left-hand and right-hand contexts for floating-point types.  Since the bit-wise comparison only applies to integral types, why not test more explicitly for those (like below)?

// Also do not permit implicit convertion to integer in this case as the user may think
// the result is a bitwise operation on the original type but it's not
if (!lctx->type.dataType.IsIntegerType() && !lctx->type.dataType.IsUnsignedType())
{
	asCString str;
	str.Format(TXT_ILLEGAL_OPERATION_ON_s, lctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
	Error(str, node);

	// Set an integer value and allow the compiler to continue
	ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
	return;
}
if (!rctx->type.dataType.IsIntegerType() && !rctx->type.dataType.IsUnsignedType())
{
	asCString str;
	str.Format(TXT_ILLEGAL_OPERATION_ON_s, rctx->type.dataType.Format(outFunc->nameSpace).AddressOf());
	Error(str, node);

	// Set an integer value and allow the compiler to continue
	ctx->type.SetConstantDW(asCDataType::CreatePrimitive(ttInt, true), 0);
	return;
}

 

Share this post


Link to post
Share on other sites

Only float and double are explicitly restricted here, because these types have an implicit conversion to integer but I do not want to allow them here. 

Object types that implement implicit conversion to integer are still allowed.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!