Skip to content

Commit

Permalink
Add bitwise operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Aug 6, 2024
1 parent a88b198 commit 5f9c078
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
33 changes: 33 additions & 0 deletions base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions citrine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/exp/xx2/test0371.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
8
10
2
4 changes: 2 additions & 2 deletions tests/test-o-mat/Configuration
Original file line number Diff line number Diff line change
Expand Up @@ -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 ≔ ‘../../’.
6 changes: 6 additions & 0 deletions tests/test0371.ctr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
‘SINGLE_LANGUAGE’.
✎ write: (8 bit-and: 10), stop.
✎ write: (8 bit-or: 10), stop.
✎ write: (8 bit-xor: 10), stop.


3 changes: 3 additions & 0 deletions world.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5f9c078

Please sign in to comment.