forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.c
3196 lines (3038 loc) · 92.3 KB
/
base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <stdint.h>
#include <time.h>
#include <regex.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifdef forLinux
#include <bsd/stdlib.h>
#include <bsd/string.h>
#endif
#include "citrine.h"
#include "siphash.h"
/**
* Nil
*
* Nil represents 'nothing' or NULL in other languages.
* Any object property that has not been assigned a value
* will contain Nil. Unlike some other programming languages
* Citrine has no concept of 'undefined' or isset, Nil is actually the
* same as 'undefined' or not set.
*
* Literal:
*
* Nil
*/
ctr_object* ctr_build_nil() {
return CtrStdNil;
}
/**
* [Nil] isNil
*
* Nil always answers this message with a boolean object 'True'.
*/
ctr_object* ctr_nil_is_nil(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(1);
}
/**
* [Nil] toString
*
* Returns the string representation of Nil: 'Nil'.
*/
ctr_object* ctr_nil_to_string(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_string_from_cstring( "Nil" );
}
/**
* [Nil] toNumber
*
* Returns the numerical representation of Nil: 0.
*/
ctr_object* ctr_nil_to_number(ctr_object* myself, ctr_argument* ctr_argumentList) {
return ctr_build_number_from_float(0);
}
/**
* [Nil] toBoolean
*
* Returns the boolean representation of Nil: False.
*/
ctr_object* ctr_nil_to_boolean(ctr_object* myself, ctr_argument* ctr_argumentList) {
return ctr_build_bool(0);
}
/**
* Object
*
* This is the base object, the parent of all other objects.
* It contains essential object oriented programming features.
*/
ctr_object* ctr_object_make(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* objectInstance = NULL;
objectInstance = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
objectInstance->link = myself;
return objectInstance;
}
/**
* [Object] type
*
* Returns a string representation of the type of object.
*/
ctr_object* ctr_object_type(ctr_object* myself, ctr_argument* argumentList) {
switch(myself->info.type){
case CTR_OBJECT_TYPE_OTNIL:
return ctr_build_string_from_cstring("Nil");
case CTR_OBJECT_TYPE_OTBOOL:
return ctr_build_string_from_cstring("Boolean");
case CTR_OBJECT_TYPE_OTNUMBER:
return ctr_build_string_from_cstring("Number");
case CTR_OBJECT_TYPE_OTSTRING:
return ctr_build_string_from_cstring("String");
case CTR_OBJECT_TYPE_OTBLOCK:
case CTR_OBJECT_TYPE_OTNATFUNC:
return ctr_build_string_from_cstring("Block");
default:
return ctr_build_string_from_cstring("Object");
}
}
/**
* [Object] toString
*
* Returns a string representation of a generic object.
* This string representation will be:
*
* [Object]
*/
ctr_object* ctr_object_to_string( ctr_object* myself, ctr_argument* argumentList ) {
return ctr_build_string_from_cstring( "[Object]" );
}
/**
* [Object] toNumber
*
* Returns a numerical representation of the object. This basic behavior, part
* of any object will just return 1. Other objects typically override this
* behavior with more useful implementations.
*/
ctr_object* ctr_object_to_number(ctr_object* myself, ctr_argument* ctr_argumentList) {
return ctr_build_number_from_float(1);
}
/**
* [Object] toBoolean
*
* Returns a boolean representation of the object. This basic behavior, part
* of any object will just return True. Other objects typically override this
* behavior with more useful implementations.
*/
ctr_object* ctr_object_to_boolean(ctr_object* myself, ctr_argument* ctr_argumentList) {
return ctr_build_bool(1);
}
/**
* [Object] equals: [other]
*
* Tests whether the current instance is the same as
* the argument.
*
* Alias: =
*
* Usage:
* object equals: other
*/
ctr_object* ctr_object_equals(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherObject = argumentList->object;
if (otherObject == myself) return ctr_build_bool(1);
return ctr_build_bool(0);
}
/**
* [Object] myself
*
* Returns the object itself.
*/
ctr_object* ctr_object_myself(ctr_object* myself, ctr_argument* argumentList) {
return myself;
}
/**
* [Object] learn: [String] means: [String].
*
* Teaches any object to repsond to the first specified message just like
* it would upon receiving the second. This allows you to map existing
* responses to new messages. You can use this to translate messages into your native
* language. After mapping, sending the alias message will be just as fast
* as sending the original message. You can use this to create programs
* in your native language without sacrficing performance. Of course the mapping itself
* has a cost, but the mapped calls will be 'toll-free'.
*
* Usage:
*
* #in this example we'll map a message to a Dutch word:
*
* Boolean learn: 'alsWaar:'
* means: 'ifTrue:'.
*
* (2 > 1) alsWaar: {
* Pen write: 'alsWaar means ifTrue in Dutch'.
* }
*/
ctr_object* ctr_object_learn_meaning(ctr_object* myself, ctr_argument* ctr_argumentList) {
char* current_method_name_str;
ctr_size current_method_name_len;
ctr_size i = 0;
ctr_size len = 0;
ctr_mapitem* current_method = myself->methods->head;
ctr_object* target_method_name = ctr_internal_cast2string( ctr_argumentList->next->object );
char* target_method_name_str = target_method_name->value.svalue->value;
ctr_size target_method_name_len = target_method_name->value.svalue->vlen;
ctr_object* alias = ctr_internal_cast2string( ctr_argumentList->object );
while( i < myself->methods->size ) {
current_method_name_str = current_method->key->value.svalue->value;
current_method_name_len = current_method->key->value.svalue->vlen;
if ( current_method_name_len > target_method_name_len ) {
len = current_method_name_len;
} else {
len = target_method_name_len;
}
if ( strncmp( current_method_name_str, target_method_name_str, len ) == 0 ) {
ctr_internal_object_add_property( myself, alias, current_method->value, 1);
break;
}
current_method = current_method->next;
i ++;
}
return myself;
}
/**
* [Object] do
*
* Activates 'chain mode'. If chain mode is active, all messages will
* return the recipient object regardless of their return signature.
*
* Usage:
*
* a := Array < 'hello' ; 'world' ; True ; Nil ; 666.
* a do pop shift unshift: 'hi', push: 999, done.
*
* Because of 'chain mode' you can do 'a do pop shift' etc, instead of
*
* a pop.
* a shift.
* etc..
*
* The 'do' message tells the object to always return itself and disgard
* the original return value until the message 'done' has been received.
*/
ctr_object* ctr_object_do( ctr_object* myself, ctr_argument* argumentList ) {
myself->info.chainMode = 1;
return myself;
}
/**
* [Object] done
*
* Deactivates 'chain mode'.
*/
ctr_object* ctr_object_done( ctr_object* myself, ctr_argument* argumentList ) {
myself->info.chainMode = 0;
return myself;
}
/**
* [Object] case: [Object] do: [Block].
*
* This message makes the recipient compare itself to the specified object.
* If the recipient considers itself to be equal, it will carry out the
* instructions in the associated block of code. The recipient will send
* the message '=' to itself with the other object as an argument. This leaves
* it up to the recipient to determine whether the objects are considered
* equal. If the recipient decides the objects are not equal, the associated
* code block will be ignored. Note that this allows you to implement a
* so-called switch-statement like those found in other languages. Because
* of the generic implementation, you can use the case statements on almost
* any object. Case-do statements may provide a readable alternative to a
* long list of if-else messages.
*
* The example program below will print the text 'It's a Merlot!'.
*
* Usage:
*
* #Can we use a switch statement in Citrine?
*
* #create a good wine
* ☞ wine := Object new.
*
* #define a string representation for this wine
* wine on: 'toString' do: {
* ↲ 'merlot'.
* }.
*
* #define how wines are to be compared
* wine on: '=' do: { :other wine
* ↲ ( me toString = other wine ).
*}.
*
* #now select the correct wine from the list
* wine
* case: 'cabernet' do: { ✎ write: 'it\'s a Cabernet!'. },
* case: 'syrah' do: { ✎ write: 'it\'s a Syrah!'. },
* case: 'merlot' do: { ✎ write: 'it\'s a Merlot!'. },
* case: 'malbec' do: { ✎ write: 'it\'s a Malbec!'. }.
*/
ctr_object* ctr_object_case_do( ctr_object* myself, ctr_argument* argumentList ) {
ctr_object* block = argumentList->next->object;
ctr_argument* compareArguments;
if (block->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdFlow = ctr_build_string_from_cstring("Expected block for case.");
return CtrStdNil;
}
compareArguments = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
compareArguments->object = argumentList->object;
compareArguments->next = NULL;
if (
ctr_internal_cast2bool(
ctr_send_message( myself, "=", 1, compareArguments )
)->value.bvalue == 1) {
block->info.sticky = 1;
ctr_block_run(block, compareArguments, NULL);
if (CtrStdFlow == CtrStdContinue) CtrStdFlow = NULL; /* consume continue */
if (CtrStdFlow == CtrStdBreak) CtrStdFlow = NULL; /* consume break */
block->info.mark = 0;
block->info.sticky = 0;
}
ctr_heap_free( compareArguments );
return myself;
}
/**
* [Object] message: [String] arguments: [Array]
*
* Sends a custom or 'dynamic' message to an object. This takes a string containing
* the message to be send to the object and an array listing the arguments at the
* correct indexes. If the array fails to provide the correct indexes this will
* generate an out-of-bounds error coming from the Array object. If something other
* than an Array is provided an error will be thrown as well.
*
* Usage:
*
* var str := 'write:'.
* Pen message: 'write:' arguments: (Array < 'Hello World').
*
* This will print the string 'Hello world' on the screen using a dynamically
* crafted message.
*/
ctr_object* ctr_object_message( ctr_object* myself, ctr_argument* argumentList ) {
ctr_object* message = ctr_internal_cast2string( argumentList->object );
ctr_object* arr = argumentList->next->object;
if ( arr->info.type != CTR_OBJECT_TYPE_OTARRAY ) {
CtrStdFlow = ctr_build_string_from_cstring( "Dynamic message expects array." );
return CtrStdNil;
}
ctr_size length = (int) ctr_array_count( arr, NULL )->value.nvalue;
int i = 0;
ctr_argument* args = ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* cur = args;
for ( i = 0; i < length; i ++ ) {
ctr_argument* index = ctr_heap_allocate( sizeof( ctr_argument ) );
if ( i > 0 ) {
cur->next = ctr_heap_allocate( sizeof( ctr_argument ) );
cur = cur->next;
}
index->object = ctr_build_number_from_float( (double) i );
cur->object = ctr_array_get( arr, index );
ctr_heap_free( index );
}
char* flatMessage = ctr_heap_allocate_cstring( message );
ctr_object* answer = ctr_send_message( myself, flatMessage, message->value.svalue->vlen, args);
cur = args;
if ( length == 0 ) {
ctr_heap_free(args);
} else {
for ( i = 0; i < length; i ++ ) {
ctr_argument* a = cur;
if ( i < length - 1 ) cur = cur->next;
ctr_heap_free( a );
}
}
ctr_heap_free( flatMessage );
return answer;
}
/**
* [Object] on: [String] do: [Block]
*
* Makes the object respond to a new kind of message.
* Use the semicolons to indicate the positions of the arguments to be
* passed.
*
* Usage:
*
* object on: 'greet' do: { ... }.
* object on: 'between:and:' do: { ... }.
*
*/
ctr_object* ctr_object_on_do(ctr_object* myself, ctr_argument* argumentList) {
ctr_argument* nextArgument;
ctr_object* methodBlock;
ctr_object* methodName = argumentList->object;
if (methodName->info.type != CTR_OBJECT_TYPE_OTSTRING) {
CtrStdFlow = ctr_build_string_from_cstring("Expected on: argument to be of type string.");
CtrStdFlow->info.sticky = 1;
return myself;
}
nextArgument = argumentList->next;
methodBlock = nextArgument->object;
if (methodBlock->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdFlow = ctr_build_string_from_cstring("Expected argument do: to be of type block.");
CtrStdFlow->info.sticky = 1;
return myself;
}
ctr_internal_object_add_property(myself, methodName, methodBlock, 1);
return myself;
}
ctr_object* ctr_sock_error( int fd, int want2close ) {
CtrStdFlow = ctr_build_string_from_cstring( strerror( errno ) );
if (want2close) {
shutdown(fd, SHUT_RDWR);
close(fd);
}
return CtrStdNil;
}
ctr_object* ctr_object_send2remote(ctr_object* myself, ctr_argument* argumentList) {
char* ip;
int sockfd = 0, n = 0;
char* responseBuff;
size_t responseLength;
struct sockaddr_in6 serv_addr;
ctr_object* answer;
ctr_object* messageObj;
ctr_object* ipObj;
struct hostent *server;
ipObj = ctr_internal_object_find_property(
myself,
ctr_build_string_from_cstring("@"),
CTR_CATEGORY_PRIVATE_PROPERTY
);
if (ipObj == NULL) return CtrStdNil;
ipObj = ctr_internal_cast2string(ipObj);
ip = ctr_heap_allocate_cstring(ipObj);
answer = ctr_build_empty_string();
messageObj = ctr_internal_cast2string(
argumentList->object
);
if((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) return ctr_sock_error( sockfd, 0 );
memset(&serv_addr, '0', sizeof(serv_addr));
server = gethostbyname2(ip,AF_INET6);
if (server == NULL) {
fprintf(stderr, "ERROR, no such host\n");
exit(0);
}
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_family = AF_INET6;
memmove((char *) &serv_addr.sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
serv_addr.sin6_port = htons(ctr_default_port);
int c = 0;
c = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if ( c != 0 ) return ctr_sock_error( sockfd, 1 );
c = send(sockfd, (size_t*) &messageObj->value.svalue->vlen, sizeof(size_t), 0);
if ( c < 0 ) ctr_sock_error( sockfd, 1 );
c = send(sockfd, messageObj->value.svalue->value, messageObj->value.svalue->vlen, 0);
if ( c < 0 ) ctr_sock_error( sockfd, 1 );
n = read(sockfd, (size_t*) &responseLength, sizeof(responseLength));
if ( n == 0 ) ctr_sock_error( sockfd, 1 );
responseBuff = ctr_heap_allocate( responseLength + 1 );
n = read(sockfd, responseBuff, responseLength);
if ( n == 0 ) ctr_sock_error( sockfd, 1 );
answer = ctr_build_string_from_cstring( responseBuff );
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
ctr_heap_free(ip);
ctr_heap_free(responseBuff);
return answer;
}
/**
* [Object] respondTo: [String]
*
* Variations:
*
* [Object] respondTo: [String] with: [String]
* [Object] respondTo: [String] with: [String] and: [String]
*
* Default respond-to implemention, does nothing.
*/
ctr_object* ctr_object_respond(ctr_object* myself, ctr_argument* argumentList) {
if (myself->info.remote == 0) return myself;
ctr_object* arr;
ctr_object* answer;
ctr_argument* newArgumentList;
arr = ctr_array_new( CtrStdArray, argumentList );
newArgumentList = ctr_heap_allocate( sizeof(ctr_argument) );
newArgumentList->object = argumentList->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = ctr_array_to_string( arr, NULL );
answer = ctr_object_send2remote( myself, newArgumentList );
ctr_heap_free(newArgumentList);
return answer;
}
ctr_object* ctr_object_respond_and(ctr_object* myself, ctr_argument* argumentList) {
if (myself->info.remote == 0) return myself;
ctr_object* arr;
ctr_object* answer;
ctr_argument* newArgumentList;
arr = ctr_array_new( CtrStdArray, argumentList );
newArgumentList = ctr_heap_allocate( sizeof(ctr_argument) );
newArgumentList->object = argumentList->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = argumentList->next->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = ctr_array_to_string( arr, NULL );
answer = ctr_object_send2remote( myself, newArgumentList );
ctr_heap_free(newArgumentList);
return answer;
}
ctr_object* ctr_object_respond_and_and(ctr_object* myself, ctr_argument* argumentList) {
if (myself->info.remote == 0) return myself;
ctr_object* arr;
ctr_object* answer;
ctr_argument* newArgumentList;
arr = ctr_array_new( CtrStdArray, argumentList );
newArgumentList = ctr_heap_allocate( sizeof(ctr_argument) );
newArgumentList->object = argumentList->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = argumentList->next->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = argumentList->next->next->object;
ctr_array_push( arr, newArgumentList );
newArgumentList->object = ctr_array_to_string( arr, NULL );
answer = ctr_object_send2remote( myself, newArgumentList );
ctr_heap_free(newArgumentList);
return answer;
}
/**
* [Object] isNil
*
* Default isNil implementation.
*
* Always returns boolean object False.
*/
ctr_object* ctr_object_is_nil(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(0);
}
/**
* Boolean
*
* Literal:
*
* True
* False
*/
ctr_object* ctr_build_bool(int truth) {
ctr_object* boolObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTBOOL);
if (truth) boolObject->value.bvalue = 1; else boolObject->value.bvalue = 0;
boolObject->info.type = CTR_OBJECT_TYPE_OTBOOL;
boolObject->link = CtrStdBool;
return boolObject;
}
/**
* [Boolean] = [other]
*
* Tests whether the other object (as a boolean) has the
* same value (boolean state True or False) as the current one.
*
* Usage:
*
* (True = False) ifFalse: { Pen write: 'This is not True!'. }.
*/
ctr_object* ctr_bool_eq(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(ctr_internal_cast2bool(argumentList->object)->value.bvalue == myself->value.bvalue);
}
/**
* [Boolean] != [other]
*
* Tests whether the other object (as a boolean) has the
* same value (boolean state True or False) as the current one.
*
* Usage:
*
* (True != False) ifTrue: { Pen write: 'This is not True!'. }.
*/
ctr_object* ctr_bool_neq(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(ctr_internal_cast2bool(argumentList->object)->value.bvalue != myself->value.bvalue);
}
/**
* [Boolean] toString
*
* Simple cast function.
*/
ctr_object* ctr_bool_to_string(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.bvalue == 1) {
return ctr_build_string_from_cstring( "True" );
} else {
return ctr_build_string_from_cstring( "False" );
}
}
/**
* [Boolean] break
*
* Breaks out of the current block and bubbles up to the parent block if
* the value of the receiver equals boolean True.
*
* Usage:
*
* (iteration > 10) break. #breaks out of loop after 10 iterations
*/
ctr_object* ctr_bool_break(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.bvalue) {
CtrStdFlow = CtrStdBreak; /* If error = Break it's a break, there is no real error. */
}
return myself;
}
/**
* [Boolean] continue
*
* Skips the remainder of the current block in a loop, continues to the next
* iteration.
*
* Usage:
*
* (iteration > 10) continue.
*/
ctr_object* ctr_bool_continue(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.bvalue) {
CtrStdFlow = CtrStdContinue; /* If error = Continue, then it breaks only one iteration (return). */
}
return myself;
}
/**
* [Boolean] ifTrue: [block]
*
* Executes a block of code if the value of the boolean
* object is True.
*
* Usage:
* (some expression) ifTrue: { ... }.
*
* You can also use ifFalse and ifTrue with other objects because the
* Object instance also responds to these messages.
*/
ctr_object* ctr_bool_if_true(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* result;
if (myself->value.bvalue) {
ctr_object* codeBlock = argumentList->object;
ctr_argument* arguments = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
arguments->object = myself;
result = ctr_block_run(codeBlock, arguments, NULL);
ctr_heap_free( arguments );
return result;
}
if (CtrStdFlow == CtrStdBreak) CtrStdFlow = NULL; /* consume break */
return myself;
}
/**
* [Boolean] ifFalse: [block]
*
* Executes a block of code if the value of the boolean
* object is True.
*
* Usage:
* (some expression) ifFalse: { ... }.
*
* You can also use ifFalse and ifTrue with other objects because the
* Object instance also responds to these messages.
*/
ctr_object* ctr_bool_if_false(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* result;
if (!myself->value.bvalue) {
ctr_object* codeBlock = argumentList->object;
ctr_argument* arguments = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
arguments->object = myself;
result = ctr_block_run(codeBlock, arguments, NULL);
ctr_heap_free( arguments );
return result;
}
if (CtrStdFlow == CtrStdBreak) CtrStdFlow = NULL; /* consume break */
return myself;
}
/**
* @internal
*/
ctr_object* ctr_object_if_false( ctr_object* myself, ctr_argument* argumentList ) {
return ctr_bool_if_false( ctr_internal_cast2bool( myself ), argumentList );
}
/**
* @internal
*/
ctr_object* ctr_object_if_true( ctr_object* myself, ctr_argument* argumentList ) {
return ctr_bool_if_true( ctr_internal_cast2bool( myself ), argumentList );
}
/**
* [Boolean] not
*
* Returns the opposite of the current value.
*
* Usage:
* True := False not.
*
*/
ctr_object* ctr_bool_not(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(!myself->value.bvalue);
}
/**
* [Boolean] either: [this] or: [that]
*
* Returns argument #1 if boolean value is True and argument #2 otherwise.
*
* Usage:
* Pen write: 'the coin lands on: ' + (Boolean flip either: 'head' or: 'tail').
*/
ctr_object* ctr_bool_either_or(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.bvalue) {
return argumentList->object;
} else {
return argumentList->next->object;
}
}
/**
* [Boolean] & [other]
*
* Returns True if both the object value is True and the
* argument is True as well.
*
* Usage:
*
* a & b
*
*/
ctr_object* ctr_bool_and(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* other = ctr_internal_cast2bool(argumentList->object);
return ctr_build_bool((myself->value.bvalue && other->value.bvalue));
}
/**
* [Boolean] ! [other]
*
* Returns True if the object value is False and the
* argument is False as well.
*
* Usage:
*
* a ! b
*
*/
ctr_object* ctr_bool_nor(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* other = ctr_internal_cast2bool(argumentList->object);
return ctr_build_bool((!myself->value.bvalue && !other->value.bvalue));
}
/**
* [Boolean] | [other]
*
* Returns True if either the object value is True or the
* argument is True or both are True.
*
* Usage:
*
* a | b
*/
ctr_object* ctr_bool_or(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* other = ctr_internal_cast2bool(argumentList->object);
return ctr_build_bool((myself->value.bvalue || other->value.bvalue));
}
/**
* [Boolean] ? [other]
*
* Returns True if either the object value is True or the
* argument is True but not both.
*
* Usage:
*
* a ? b
*/
ctr_object* ctr_bool_xor(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* other = ctr_internal_cast2bool(argumentList->object);
return ctr_build_bool((myself->value.bvalue ^ other->value.bvalue));
}
/**
* [Boolean] toNumber
*
* Returns 0 if boolean is False and 1 otherwise.
*/
ctr_object* ctr_bool_to_number(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float( (ctr_number) myself->value.bvalue );
}
/**
* Number
*
* Literal:
*
* 0
* 1
* -8
* 2.5
* pi
* 𝛑
*
* Represents a number object in Citrine.
*/
ctr_object* ctr_build_number(char* n) {
ctr_object* numberObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNUMBER);
numberObject->value.nvalue = atof(n);
numberObject->link = CtrStdNumber;
return numberObject;
}
/**
* @internal
* BuildNumberFromString
*/
ctr_object* ctr_build_number_from_string(char* str, ctr_size length) {
char* numCStr;
ctr_object* numberObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNUMBER);
/* turn string into a C-string before feeding it to atof */
int stringNumberLength = ( length <= 40 ) ? length : 40;
/* max length is 40 (and that's probably even too long... ) */
numCStr = (char*) ctr_heap_allocate( 41 * sizeof( char ) );
memcpy( numCStr, str, stringNumberLength );
numberObject->value.nvalue = atof(numCStr);
numberObject->link = CtrStdNumber;
ctr_heap_free( numCStr );
return numberObject;
}
/**
* @internal
* BuildNumberFromFloat
*
* Creates a number object from a float.
* Internal use only.
*/
ctr_object* ctr_build_number_from_float(ctr_number f) {
ctr_object* numberObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTNUMBER);
numberObject->value.nvalue = f;
numberObject->link = CtrStdNumber;
return numberObject;
}
/**
* [Number] > [other]
*
* Returns True if the number is higher than other number.
*/
ctr_object* ctr_number_higherThan(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool((myself->value.nvalue > otherNum->value.nvalue));
}
/**
* [Number] >=: [other]
*
* Returns True if the number is higher than or equal to other number.
*/
ctr_object* ctr_number_higherEqThan(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool((myself->value.nvalue >= otherNum->value.nvalue));
}
/**
* [Number] < [other]
*
* Returns True if the number is less than other number.
*/
ctr_object* ctr_number_lowerThan(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool((myself->value.nvalue < otherNum->value.nvalue));
}
/**
* [Number] <=: [other]
*
* Returns True if the number is less than or equal to other number.
*/
ctr_object* ctr_number_lowerEqThan(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool((myself->value.nvalue <= otherNum->value.nvalue));
}
/**
* [Number] = [other]
*
* Returns True if the number equals the other number.
*/
ctr_object* ctr_number_eq(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool(myself->value.nvalue == otherNum->value.nvalue);
}
/**
* [Number] !=: [other]
*
* Returns True if the number does not equal the other number.
*/
ctr_object* ctr_number_neq(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
return ctr_build_bool(myself->value.nvalue != otherNum->value.nvalue);
}
/**
* [Number] between: [low] and: [high]
*
* Returns True if the number instance has a value between the two
* specified values.
*
* Usage:
*
* q between: x and: y
*/
ctr_object* ctr_number_between(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_argument* nextArgumentItem = argumentList->next;
ctr_object* nextArgument = ctr_internal_cast2number(nextArgumentItem->object);
return ctr_build_bool((myself->value.nvalue >= otherNum->value.nvalue) && (myself->value.nvalue <= nextArgument->value.nvalue));
}
/**
* [Number] odd
*
* Returns True if the number is odd and False otherwise.
*/
ctr_object* ctr_number_odd(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool((int)myself->value.nvalue % 2);
}
/**
* [Number] even
*
* Returns True if the number is even and False otherwise.
*/
ctr_object* ctr_number_even(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool(!((int)myself->value.nvalue % 2));
}
/**
* [Number] + [Number]
*
* Adds the other number to the current one. Returns a new
* number object.
*/
ctr_object* ctr_number_add(ctr_object* myself, ctr_argument* argumentList) {
ctr_argument* newArg;
ctr_object* otherNum = argumentList->object;
ctr_object* result;
ctr_number a;
ctr_number b;
ctr_object* strObject;
if (otherNum->info.type == CTR_OBJECT_TYPE_OTSTRING) {
strObject = ctr_internal_cast2string(myself);
newArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
newArg->object = otherNum;
result = ctr_string_concat(strObject, newArg);
ctr_heap_free( newArg );
return result;
} else {
otherNum = ctr_internal_cast2number( otherNum );
}
a = myself->value.nvalue;
b = otherNum->value.nvalue;
return ctr_build_number_from_float((a+b));
}
/**
* [Number] +=: [Number]
*
* Increases the number ITSELF by the specified amount, this message will change the
* value of the number object itself instead of returning a new number.
*/
ctr_object* ctr_number_inc(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
myself->value.nvalue += otherNum->value.nvalue;
return myself;
}
/**
* [Number] - [Number]
*
* Subtracts the other number from the current one. Returns a new