forked from gabordemooij/citrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.c
1940 lines (1831 loc) · 52.8 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 "citrine.h"
#include "siphash.h"
/**
* Nil
*
* 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);
}
/**
* 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] 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] 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) {
CtrStdError = ctr_build_string_from_cstring("Expected on: argument to be of type string.");
return myself;
}
nextArgument = argumentList->next;
methodBlock = nextArgument->object;
if (methodBlock->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdError = ctr_build_string_from_cstring("Expected argument do: to be of type block.");
return myself;
}
ctr_internal_object_add_property(myself, methodName, methodBlock, 1);
return myself;
}
/**
* [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) {
return myself;
}
/**
* [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) {
return ctr_internal_cast2string(myself);
}
/**
* [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) {
CtrStdError = 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) {
CtrStdError = 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: {\ ... }.
*
*/
ctr_object* ctr_bool_iftrue(ctr_object* myself, ctr_argument* argumentList) {
if (myself->value.bvalue) {
ctr_object* codeBlock = argumentList->object;
ctr_argument* arguments = CTR_CREATE_ARGUMENT();
arguments->object = myself;
return ctr_block_run(codeBlock, arguments, codeBlock);
}
if (CtrStdError == CtrStdBreak) CtrStdError = 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: {\ ... }.
*
*/
ctr_object* ctr_bool_ifFalse(ctr_object* myself, ctr_argument* argumentList) {
if (!myself->value.bvalue) {
ctr_object* codeBlock = argumentList->object;
ctr_argument* arguments = CTR_CREATE_ARGUMENT();
arguments->object = myself;
return ctr_block_run(codeBlock, arguments, codeBlock);
}
if (CtrStdError == CtrStdBreak) CtrStdError = NULL; /* consume break */
return myself;
}
/**
* [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] flip
*
* 'Flips a coin'. Returns a random boolean value True or False.
*
* Usage:
* coinLandsOn := (Boolean flip).
*/
ctr_object* ctr_bool_flip(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool((rand() % 2));
}
/**
* [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] and: [other]
*
* Returns True if both the object value is True and the
* argument is True as well.
*
* Usage:
*
* a and: 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] nor: [other]
*
* Returns True if the object value is False and the
* argument is False as well.
*
* Usage:
*
* a nor: 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] or: [other]
*
* Returns True if either the object value is True or the
* argument is True or both are True.
*
* Usage:
*
* a or: 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] xor: [other]
*
* Returns True if either the object value is True or the
* argument is True but not both.
*
* Usage:
*
* a xor: 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
*
* 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 */
numCStr = (char*) calloc(40, sizeof(char));
memcpy(numCStr, str, length);
numberObject->value.nvalue = atof(numCStr);
numberObject->link = CtrStdNumber;
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_number a;
ctr_number b;
ctr_object* strObject;
if (otherNum->info.type == CTR_OBJECT_TYPE_OTSTRING) {
strObject = ctr_internal_create_object(CTR_OBJECT_TYPE_OTSTRING);
strObject = ctr_internal_cast2string(myself);
newArg = CTR_CREATE_ARGUMENT();
newArg->object = otherNum;
return ctr_string_concat(strObject, newArg);
}
a = myself->value.nvalue;
b = otherNum->value.nvalue;
return ctr_build_number_from_float((a+b));
}
/**
* [Number] add: [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
* number object.
*/
ctr_object* ctr_number_minus(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
return ctr_build_number_from_float((a-b));
}
/**
* [Number] subtract: [number]
*
* Decreases 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_dec(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
myself->value.nvalue -= otherNum->value.nvalue;
return myself;
}
/**
* [Number] * [Number or Block]
*
* Multiplies the number by the specified multiplier. Returns a new
* number object.
*/
ctr_object* ctr_number_multiply(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum;
ctr_number a;
ctr_number b;
otherNum = ctr_internal_cast2number(argumentList->object);
a = myself->value.nvalue;
b = otherNum->value.nvalue;
return ctr_build_number_from_float(a*b);
}
/**
* [Number] times: [Block]
*
* Runs the block of code a 'Number' of times.
* This is the most basic form of a loop.
*
* Usage:
*
* 7 times: { i | Pen write: i. }.
*
* The example above runs the block 7 times. The current iteration
* number is passed to the block as a parameter (i in this example).
*/
ctr_object* ctr_number_times(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* indexNumber;
ctr_object* block = argumentList->object;
ctr_argument* arguments;
int t;
int i;
if (block->info.type != CTR_OBJECT_TYPE_OTBLOCK) { printf("Expected code block."); exit(1); }
block->info.sticky = 1;
t = myself->value.nvalue;
for(i=0; i<t; i++) {
indexNumber = ctr_build_number_from_float((ctr_number) i);
arguments = CTR_CREATE_ARGUMENT();
arguments->object = indexNumber;
ctr_block_run(block, arguments, block);
if (CtrStdError == CtrStdContinue) CtrStdError = NULL; /* consume continue */
if (CtrStdError) break;
}
if (CtrStdError == CtrStdBreak) CtrStdError = NULL; /* consume break */
block->info.mark = 0;
block->info.sticky = 0;
return myself;
}
/**
* [Number] multiplyBy: [Number]
*
* Multiplies the number ITSELF by multiplier, this message will change the
* value of the number object itself instead of returning a new number.
*
* Usage:
*
* x := 5.
* x multiplyBy: 2. #x is now 10.
*
* Use this message to apply the operation to the object itself instead
* of creating and returning a new object.
*/
ctr_object* ctr_number_mul(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
myself->value.nvalue *= otherNum->value.nvalue;
return myself;
}
/**
* [Number] / [Number]
*
* Divides the number by the specified divider. Returns a new
* number object.
*/
ctr_object* ctr_number_divide(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
if (b == 0) {
CtrStdError = ctr_build_string_from_cstring("Division by zero.");
return myself;
}
return ctr_build_number_from_float((a/b));
}
/**
* [Number] divideBy: [Number]
*
* Divides the number ITSELF by divider, this message will change the
* value of the number object itself instead of returning a new number.
*
* Usage:
*
* x := 10.
* x divideBy: 2. #x will now be 5.
*
* Use this message to apply the operation to the object itself instead
* of generating a new object.
*/
ctr_object* ctr_number_div(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
if (otherNum->value.nvalue == 0) {
CtrStdError = ctr_build_string_from_cstring("Division by zero.");
return myself;
}
myself->value.nvalue /= otherNum->value.nvalue;
return myself;
}
/**
* [Number] modulo: [modulo]
*
* Returns the modulo of the number. This message will return a new
* object representing the modulo of the recipient.
*
* Usage:
*
* x := 11 modulo: 3. #x will now be 2
*
* Use this message to apply the operation of division to the
* object itself instead of generating a new one.
*/
ctr_object* ctr_number_modulo(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
if (b == 0) {
CtrStdError = ctr_build_string_from_cstring("Division by zero.");
return myself;
}
return ctr_build_number_from_float(fmod(a,b));
}
/**
* [Number] toPowerOf: [power]
*
* Returns a new object representing the
* number to the specified power.
*
* Usage:
*
* x := 2 toPowerOf: 8. #x will be 256
*
* The example above will raise 2 to the power of 8 resulting in
* a new Number object: 256.
*/
ctr_object* ctr_number_pow(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
return ctr_build_number_from_float(pow(a,b));
}
/**
* [Number] positive
*
* Returns a boolean indicating wether the number is positive.
* This message will return a boolean object 'True' if the recipient is
* positive and 'False' otherwise.
*
* Usage:
*
* hope := 0.1.
* ( hope positive ) ifTrue: {\ Pen write: 'Still a little hope for humanity'. }.
*
* The example above will print the message because hope is higher than 0.
*/
ctr_object* ctr_number_positive(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool( ( myself->value.nvalue > 0) );
}
/**
* [Number] negative
*
* Returns a boolean indicating wether the number is negative.
* This message will return a boolean object 'True' if the recipient is
* negative and 'False' otherwise. It's the eaxct opposite of the 'positive'
* message.
*
* Usage:
*
* hope := -1.
* (hope negative) ifTrue: {\ Pen write: 'No hope left'. }.
*
* The example above will print the message because the value of the variable
* hope is less than 0.
*/
ctr_object* ctr_number_negative(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_bool( ( myself->value.nvalue < 0) );
}
/**
* [Number] max: [other]
*
* Returns the biggest number of the two.
*
* Usage:
*
* x := 6 max: 4. #x is 6
* x := 6 max: 7. #x is 7
*/
ctr_object* ctr_number_max(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
return ctr_build_number_from_float((a >= b) ? a : b);
}
/**
* [Number] min: [other]
*
* Returns a the smallest number.
*
* Usage:
*
* x := 6 min: 4. #x is 4
* x := 6 min: 7. #x is 7
*/
ctr_object* ctr_number_min(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
return ctr_build_number_from_float((a <= b) ? a : b);
}
/**
* [Number] factorial
*
* Calculates the factorial of a number.
*/
ctr_object* ctr_number_factorial(ctr_object* myself, ctr_argument* argumentList) {
ctr_number t = myself->value.nvalue;
int i;
ctr_number a = 1;
for(i = (int) t; i > 0; i--) {
a = a * i;
}
return ctr_build_number_from_float(a);
}
/**
* [Number] to: [number] by: [step] do: [block]
*
* Runs the specified block for each step it takes to go from
* the start value to the target value using the specified step size.
* This is basically how you write for-loops in Citrine.
*
* Usage:
*
* 1 to: 5 by: 1 do: { step | Pen write: 'this is step #'+step. }.
*/
ctr_object* ctr_number_to_by_do(ctr_object* myself, ctr_argument* argumentList) {
double startValue = myself->value.nvalue;
double endValue = ctr_internal_cast2number(argumentList->object)->value.nvalue;
double incValue = ctr_internal_cast2number(argumentList->next->object)->value.nvalue;
double curValue = startValue;
ctr_object* codeBlock = argumentList->next->next->object;
ctr_argument* arguments;
int forward = 0;
if (startValue == endValue) return myself;
forward = (startValue < endValue);
if (codeBlock->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdError = ctr_build_string_from_cstring("Expected block.\0");
return myself;
}
while(((forward && curValue <= endValue) || (!forward && curValue >= endValue)) && !CtrStdError) {
arguments = CTR_CREATE_ARGUMENT();
arguments->object = ctr_build_number_from_float(curValue);
ctr_block_run(codeBlock, arguments, codeBlock);
if (CtrStdError == CtrStdContinue) CtrStdError = NULL; /* consume continue and go on */
curValue += incValue;
}
if (CtrStdError == CtrStdBreak) CtrStdError = NULL; /* consume break */
return myself;
}
/**
* [Number] floor
*
* Gives the largest integer less than the recipient.
*
* Usage:
*
* x := 4.5
* y := x floor. #y will be 4
*
* The example above applies the floor function to the recipient (4.5)
* returning a new number object (4).
*/
ctr_object* ctr_number_floor(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(floor(myself->value.nvalue));
}
/**
* [Number] ceiling
*
* Rounds up the recipient number and returns the next higher integer number
* as a result.
*
* Usage:
*
* x := 4.5.
* y = x ceiling. #y will be 5
*
* The example above applies the ceiling function to the recipient (4.5)
* returning a new number object (5).
*/
ctr_object* ctr_number_ceil(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(ceil(myself->value.nvalue));
}
/**
* [Number] round
*
* Returns the rounded number.
*/
ctr_object* ctr_number_round(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(round(myself->value.nvalue));
}
/**
* [Number] absolute
*
* Returns the absolute (unsigned, positive) value of the number.
*
* Usage:
*
* x := -7.
* y := x absolute. #y will be 7
*
* The example above strips the sign off the value -7 resulting
* in 7.
*/
ctr_object* ctr_number_abs(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(fabs(myself->value.nvalue));
}
/**
* [Number] squareRoot
*
* Returns the square root of the recipient.
*
* Usage:
*
* x := 49.
* y := x squareRoot. #y will be 7
*
* The example above takes the square root of 49, resulting in the
* number 7.
*/
ctr_object* ctr_number_sqrt(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(sqrt(myself->value.nvalue));
}
/**
* [Number] exponent
*
* Returns the exponent of the number.
*/
ctr_object* ctr_number_exp(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(exp(myself->value.nvalue));
}
/**
* [Number] sine
*
* Returns the sine of the number.
*/
ctr_object* ctr_number_sin(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(sin(myself->value.nvalue));
}
/**
* [Number] cosine
*
* Returns the cosine of the number.
*/
ctr_object* ctr_number_cos(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(cos(myself->value.nvalue));
}
/**
* [Number] tangent
*
* Caculates the tangent of a number.
*/
ctr_object* ctr_number_tan(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(tan(myself->value.nvalue));
}
/**
* [Number] arctangent
*
* Caculates the arctangent of a number.
*/
ctr_object* ctr_number_atan(ctr_object* myself, ctr_argument* argumentList) {
return ctr_build_number_from_float(atan(myself->value.nvalue));
}