From 5f9c0784c42247f68c7db7b88f4eb794dfe93f3c Mon Sep 17 00:00:00 2001 From: Gabor de Mooij Date: Wed, 7 Aug 2024 00:36:31 +0200 Subject: [PATCH] Add bitwise operators. --- base.c | 33 +++++++++++++++++++++++++++++++++ citrine.h | 5 +++-- tests/exp/xx2/test0371.exp | 3 +++ tests/test-o-mat/Configuration | 4 ++-- tests/test0371.ctr | 6 ++++++ world.c | 3 +++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 tests/exp/xx2/test0371.exp create mode 100644 tests/test0371.ctr diff --git a/base.c b/base.c index 92ae52f4..4a747b7c 100644 --- a/base.c +++ b/base.c @@ -1481,6 +1481,39 @@ ctr_object* ctr_number_log(ctr_object* myself, ctr_argument* argumentList) { return ctr_build_number_from_float(log(myself->value.nvalue)); } +/** + * @def + * [ Number ] bit-and: [ Number ] + * + * @example + * ✎ write: (8 bit-and: 10), stop. + */ +ctr_object* ctr_number_bit_and(ctr_object* myself, ctr_argument* argumentList) { + return ctr_build_number_from_float((int)myself->value.nvalue & (int)ctr_tonum(argumentList->object)); +} + +/** + * @def + * [ Number ] bit-or: [ Number ] + * + * @example + * ✎ write: (8 bit-or: 10), stop. + */ +ctr_object* ctr_number_bit_or(ctr_object* myself, ctr_argument* argumentList) { + return ctr_build_number_from_float((int)myself->value.nvalue | (int)ctr_tonum(argumentList->object)); +} + +/** + * @def + * [ Number ] bit-xor: [ Number ] + * + * @example + * ✎ write: (8 bit-xor: 10), stop. + */ +ctr_object* ctr_number_bit_xor(ctr_object* myself, ctr_argument* argumentList) { + return ctr_build_number_from_float((int)myself->value.nvalue ^ (int)ctr_tonum(argumentList->object)); +} + /** * @internal * Generic method, used by: diff --git a/citrine.h b/citrine.h index 90931878..f1f1c56f 100644 --- a/citrine.h +++ b/citrine.h @@ -538,8 +538,9 @@ extern ctr_object* ctr_number_cos(ctr_object* myself, ctr_argument* argumentList extern ctr_object* ctr_number_tan(ctr_object* myself, ctr_argument* argumentList); extern ctr_object* ctr_number_atan(ctr_object* myself, ctr_argument* argumentList); extern ctr_object* ctr_number_log(ctr_object* myself, ctr_argument* argumentList); - - +extern ctr_object* ctr_number_bit_and(ctr_object* myself, ctr_argument* argumentList); +extern ctr_object* ctr_number_bit_or(ctr_object* myself, ctr_argument* argumentList); +extern ctr_object* ctr_number_bit_xor(ctr_object* myself, ctr_argument* argumentList); /** * String Interface diff --git a/tests/exp/xx2/test0371.exp b/tests/exp/xx2/test0371.exp new file mode 100644 index 00000000..2e1cfda6 --- /dev/null +++ b/tests/exp/xx2/test0371.exp @@ -0,0 +1,3 @@ +8 +10 +2 \ No newline at end of file diff --git a/tests/test-o-mat/Configuration b/tests/test-o-mat/Configuration index 3d14513e..515b3a02 100644 --- a/tests/test-o-mat/Configuration +++ b/tests/test-o-mat/Configuration @@ -30,7 +30,7 @@ . ☞ one way languages ≔ List ← ‘hy’ ; ‘en’. -☞ variations ≔ memory management modes + languages. -☞ number of tests ≔ 370. +☞ variations ≔ memory management modes + languages. +☞ number of tests ≔ 371. ☞ base path for tests ≔ ‘../../’. diff --git a/tests/test0371.ctr b/tests/test0371.ctr new file mode 100644 index 00000000..2dbec689 --- /dev/null +++ b/tests/test0371.ctr @@ -0,0 +1,6 @@ +‘SINGLE_LANGUAGE’. +✎ write: (8 bit-and: 10), stop. +✎ write: (8 bit-or: 10), stop. +✎ write: (8 bit-xor: 10), stop. + + diff --git a/world.c b/world.c index 30d44f73..f1bbe1bc 100644 --- a/world.c +++ b/world.c @@ -779,6 +779,9 @@ void ctr_initialize_world() { ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "tan" ), &ctr_number_tan ); ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "atan" ), &ctr_number_atan ); ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "log" ), &ctr_number_log ); + ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "bit-and:" ), &ctr_number_bit_and ); + ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "bit-or:" ), &ctr_number_bit_or ); + ctr_internal_create_func(CtrStdNumber, ctr_build_string_from_cstring( "bit-xor:" ), &ctr_number_bit_xor ); ctr_internal_object_add_property(CtrStdWorld, ctr_build_string_from_cstring( CTR_DICT_NUMBER ), CtrStdNumber, 0); CtrStdNumber->link = CtrStdObject; CtrStdNumber->info.sticky = 1;