feat: add bitwise NOT operator (~) with token, parser, and native implementation

Add TOKEN_TILDE token type and lexer support for the '~' character in the compiler. Implement the bitwise NOT operation as a native method on the Num class, operating on 32-bit unsigned integers. Include a new OPERATOR macro for prefix/infix operator grammar rules and add comprehensive test cases covering positive, negative, floating-point, and boundary values.
This commit is contained in:
Bob Nystrom
2013-12-05 06:09:31 +00:00
parent d076abc8b1
commit 071239aa35
4 changed files with 49 additions and 3 deletions
+9
View File
@@ -297,6 +297,13 @@ DEF_NATIVE(num_bangeq)
return BOOL_VAL(AS_NUM(args[0]) != AS_NUM(args[1]));
}
DEF_NATIVE(num_bitwiseNot)
{
// Bitwise operators always work on 32-bit unsigned ints.
uint32_t value = (uint32_t)AS_NUM(args[0]);
return NUM_VAL(~value);
}
DEF_NATIVE(object_eqeq)
{
return BOOL_VAL(wrenValuesEqual(args[0], args[1]));
@@ -478,6 +485,8 @@ void wrenInitializeCore(WrenVM* vm)
NATIVE(vm->numClass, "> ", num_gt);
NATIVE(vm->numClass, "<= ", num_lte);
NATIVE(vm->numClass, ">= ", num_gte);
NATIVE(vm->numClass, "~", num_bitwiseNot);
// TODO(bob): The only reason there are here is so that 0 != -0. Is that what
// we want?
NATIVE(vm->numClass, "== ", num_eqeq);