Bitwise Operator

Started by
4 comments, last by WitchLord 6 years, 1 month ago

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.

Advertisement

Thanks for the suggestion. I'll certainly consider it. 

 

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

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.

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

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;
}

 

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.

 

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

This topic is closed to new replies.

Advertisement