-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzylogo_populateMethods.coffee
1738 lines (1367 loc) · 52.5 KB
/
fizzylogo_populateMethods.coffee
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
# Overall comments -------------------------------------------
# ----
# in the "for" and in the "repeat" and in all the branches in the if-then-else,
# the loopCode/branch is passed as UNevaluated.
# This is because if it were passed as evaluated, it'd have to be passed as
# repeat 3 'code
# which would turn it into a closure.
# Since out closures are read-only, that usually creates chaos, because
# you are very likely to reference several variables from outside the
# loop, and "closing" those as read only is problematic for example
# this never ends, because the "a" inside the branch "a=a minus 1" is
# replaced with a 5 when closed, so it never decreases to zero!
#
# a=5
#
# repeat forever:
# ﹍if a==0:
# ﹍﹍done
# ﹍else:
# ﹍﹍a=a minus 1
# console print a
#
# On the other hand, in "to" and "answer", the code block is passed as
# evaluated so it's actually closed (in read-only mode). This is
# because in those cases one is less prone towards changing variables
# from the outer scope. You can read them, but you can't write them.
#
# ---
# For the precedence, we use the same numbers as here
# https://en.wikipedia.org/wiki/Order_of_operations
# (plus the the power operator which has precedence 1)
commonSimpleValueEqualityFunction = (context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @value == toCompare.value
return FLBoolean.createNew true
else
return FLBoolean.createNew false
commonSimpleValueInequalityFunction = (context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @value != toCompare.value
return FLBoolean.createNew true
else
return FLBoolean.createNew false
# helper to add default methods -------------------------------------------
addDefaultMethods = (classToAddThemTo) ->
classToAddThemTo.addMethod \
(flTokenize "isPrimitiveType"),
flTokenize \
"""
if (self.class == Class) or (self.class == String) or (self.class == Number) or (self.class == List) or (self.class == Boolean):
﹍return true
else:
﹍return false
"""
classToAddThemTo.addMethod \
(flTokenize "postfixPrint"),
(context) ->
#yield
if methodsExecutionDebug
log "///////// program printout: " + @flToString()
rWorkspace.environmentPrintout += @flToString()
return @
classToAddThemTo.addMethod \
(flTokenize "toString"),
(context) ->
#yield
return FLString.createNew @flToString()
classToAddThemTo.addMethod \
(flTokenize "whenNew"),
(context) ->
#yield
return @
classToAddThemTo.addMethod \
(flTokenize "eval"),
(context) ->
context.isTransparent = true
if methodsExecutionDebug
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
newContext = new FLContext context
newContext.isTransparent = true
if methodsExecutionDebug
log "newContext now tramsparent at depth: " + newContext.depth() + " with self: " + newContext.self.flToString?()
#flContexts.jsArrayPush newContext
# yield from
toBeReturned = @eval newContext, @
#flContexts.pop()
return toBeReturned
# this is the common object identity function
# strings, numbers and booleans override this
# by just comparing the values.
classToAddThemTo.addMethod \
(flTokenize "== ( toCompare )"),
(context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @ == toCompare
return FLBoolean.createNew true
else
return FLBoolean.createNew false
,7
# this is the common object identity function
# strings, numbers and booleans override this
# by just comparing the values.
classToAddThemTo.addMethod \
(flTokenize "!= ( toCompare )"),
(context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @ != toCompare
return FLBoolean.createNew true
else
return FLBoolean.createNew false
,7
classToAddThemTo.addMethod \
(flTokenize "else if ( predicate ): ('trueBranch)"),
(context) ->
#yield
return @
classToAddThemTo.addMethod \
(flTokenize "else: ('trueBranch)"),
(context) ->
#yield
return @
classToAddThemTo.addMethod \
(flTokenize "catch all : ( ' errorHandle )"),
(context) ->
#yield
return @
classToAddThemTo.addMethod \
(flTokenize "catch ( 'theError ) : ( ' errorHandle )"),
(context) ->
#yield
return @
commonPropertyAssignmentFunction = (context) ->
#yield
context.isTransparent = true
if methodsExecutionDebug
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
variable = context.lookupTemp "variable"
value = context.lookupTemp "value"
@instanceVariablesDict[ValidIDfromString variable.value] = value
return @
commonPropertyAccessFunction = (context) ->
#yield
context.isTransparent = true
if methodsExecutionDebug
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
variable = context.lookupTemp "variable"
if methodsExecutionDebug
log ". ('variable) : checking instance variables"
# somewhat similar to Javascript, the lookup starts at the object
# and climbs up to its class.
objectsBeingChecked = @
loop
if objectsBeingChecked.instanceVariablesDict[ValidIDfromString variable.value]?
if methodsExecutionDebug
log "yes it's an instance variable: "
#dir objectsBeingChecked.instanceVariablesDict[ValidIDfromString variable.value]
context.justDidAFieldOrArrayAccess = true
return objectsBeingChecked.instanceVariablesDict[ValidIDfromString variable.value]
if objectsBeingChecked == objectsBeingChecked.flClass
break
else objectsBeingChecked = objectsBeingChecked.flClass
return FLNil.createNew()
classToAddThemTo.addMethod \
(flTokenize ". ('variable) = (value)"),
commonPropertyAssignmentFunction
classToAddThemTo.addMethod \
(flTokenize ". ('variable) ← (value)"),
commonPropertyAssignmentFunction
classToAddThemTo.addMethod \
(flTokenize ". evaluating (variable)"),
commonPropertyAccessFunction
classToAddThemTo.addMethod \
(flTokenize ". ('variable) += (value)"),
(context) ->
# this is a token
variable = context.lookupTemp "variable"
value = context.lookupTemp "value"
runThis = flTokenize "(self . evaluating variable) += value"
# yield from
toBeReturned = runThis.eval context, runThis
@instanceVariablesDict[ValidIDfromString variable.value] = toBeReturned
return toBeReturned
classToAddThemTo.addMethod \
(flTokenize ". ('variable) *= (value)"),
(context) ->
# this is a token
variable = context.lookupTemp "variable"
value = context.lookupTemp "value"
runThis = flTokenize "(self . evaluating variable) *= value"
# yield from
toBeReturned = runThis.eval context, runThis
@instanceVariablesDict[ValidIDfromString variable.value] = toBeReturned
return toBeReturned
classToAddThemTo.addMethod \
(flTokenize ". ('variable) ++"),
(context) ->
# this is a token
variable = context.lookupTemp "variable"
runThis = flTokenize "(self . evaluating variable) ++"
# yield from
toBeReturned = runThis.eval context, runThis
@instanceVariablesDict[ValidIDfromString variable.value] = toBeReturned
return toBeReturned
classToAddThemTo.addMethod \
(flTokenize ". ('variable)"),
commonPropertyAccessFunction
# we DON't want the signature to be "closed", so we use
# (flTokenize "answer : ( 'signature ) by ( methodBody )"),
# however then we need another variant of "answer" called "answerEvalSignatureAndBody"
# that can take an evaluated parameter, because the implementation for
# "to" needs it.
#
# Note that you can call "answer" on both a Class or on any instance
# of any class. In the second case, you'll still have to add the method
# to the class, not to the instance.
classToAddThemTo.addMethod \
(flTokenize "answer: ( 'signature ) by: ( 'methodBody )"),
(context) ->
#yield
signature = context.lookupTemp "signature"
methodBody = context.lookupTemp "methodBody"
if @isClass()
@addMethod signature, methodBody
else
@flClass.addMethod signature, methodBody
return @
classToAddThemTo.addMethod \
(flTokenize "answerEvalSignatureAndBody ( signature ) by ( methodBody )"),
(context) ->
#yield
signature = context.lookupTemp "signature"
methodBody = context.lookupTemp "methodBody"
log "answer: giving the method body a definitionContext!"
methodBody.definitionContext = context.previousContext
methodBody.giveDefinitionContextToElements context.previousContext
if @isClass()
@addMethod signature, methodBody
else
@flClass.addMethod signature, methodBody
return @
classToAddThemTo.addMethod \
(flTokenize "answer with priority (priority) : ( 'signature ) by: ( 'methodBody )"),
(context) ->
#yield
signature = context.lookupTemp "signature"
methodBody = context.lookupTemp "methodBody"
priority = context.lookupTemp "priority"
if @isClass()
@addMethod signature, methodBody, priority.value
else
@flClass.addMethod signature, methodBody, priority.value
return @
classToAddThemTo.addMethod \
(flTokenize "answer with priority (priority) associativeRightToLeft: ( 'signature ) by: ( 'methodBody )"),
(context) ->
#yield
signature = context.lookupTemp "signature"
methodBody = context.lookupTemp "methodBody"
priority = context.lookupTemp "priority"
if @isClass()
@addMethod signature, methodBody, priority.value, ASSOCIATIVITY_RIGHT_TO_LEFT
else
@flClass.addMethod signature, methodBody, priority.value, ASSOCIATIVITY_RIGHT_TO_LEFT
return @
# with time, allClasses contains all the classes
# (boot classes and user-defined classes), but right
# now only the boot classes (i.e. primitive classes +
# helper classes such as FLForClass, etc.) have been defined
# We call this set the "bootClasses"
bootClasses = allClasses.slice()
clearClasses = ->
# user might have modified some methods in the boot
# classes.
for eachClass in bootClasses
eachClass.resetInstanceVariables()
eachClass.resetMethods()
allClasses = []
initBootClasses = ->
# Common to all -------------------------------------------------------------------
# first, add the methods common to them all
# then we'll proceed to add the class-specific classes
for eachClass in bootClasses
addDefaultMethods eachClass
# WorkSpace -----------------------------------------------------------------------
# Token ---------------------------------------------------------------------------
FLToken.addMethod \
(flTokenize "← ( valueToAssign )"),
(context, definitionContext) ->
#yield
valueToAssign = context.lookupTemp "valueToAssign"
assigneeTokenString = @value
if methodsExecutionDebug
log "evaluation " + indentation() + "assignment to token " + assigneeTokenString
log "evaluation " + indentation() + "value to assign to token: " + assigneeTokenString + " : " + valueToAssign.value
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
context.isTransparent = true
@assignValue context, definitionContext, valueToAssign
context.isTransparent = false
return valueToAssign
FLToken.addMethod \
(flTokenize "= ( valueToAssign )"),
(context, definitionContext) ->
#yield
valueToAssign = context.lookupTemp "valueToAssign"
assigneeTokenString = @value
if methodsExecutionDebug
log "evaluation " + indentation() + "assignment to token " + assigneeTokenString
log "evaluation " + indentation() + "value to assign to token: " + assigneeTokenString + " : " + valueToAssign.value
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
context.isTransparent = true
@assignValue context, definitionContext, valueToAssign
context.isTransparent = false
return valueToAssign
commonClassCreationFunction = (context, definitionContext, assigneeToken, className) ->
#yield
valueToAssign = FLClass.createNew className
if methodsExecutionDebug
log "evaluation " + indentation() + "assignment to token " + assigneeToken.value
log "evaluation " + indentation() + "value to assign to token: " + assigneeToken.value + " : " + valueToAssign.value
log "context now tramsparent at depth: " + context.depth() + " with self: " + context.self.flToString?()
context.isTransparent = true
assigneeToken.assignValue context, definitionContext, valueToAssign
return valueToAssign
FLToken.addMethod \
(flTokenize "= Class new"),
(context, definitionContext) ->
# yield from
toBeReturned = commonClassCreationFunction context, definitionContext, @, @value
return toBeReturned
FLToken.addMethod \
(flTokenize "= Class new named (theName)"),
(context, definitionContext) ->
theName = context.lookupTemp "theName"
# yield from
toBeReturned = commonClassCreationFunction context, definitionContext, @, theName.value
return toBeReturned
FLToken.addMethod \
(flTokenize "+= ( operandum )"),
(flTokenize "self ← self eval + operandum")
FLToken.addMethod \
(flTokenize "*= ( operandum )"),
(flTokenize "self ← self eval * operandum")
FLToken.addMethod \
(flTokenize "++"),
(flTokenize "self ← self eval + 1")
# Nil ---------------------------------------------------------------------------
FLNil.addMethod \
(flTokenize "== ( toCompare )"),
(context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if toCompare.flClass == FLNil
return FLBoolean.createNew true
else
return FLBoolean.createNew false
,7
# In ---------------------------------------------------------------------------
FLIn.addMethod \
(flTokenize "(object) do ('code)"),
(context) ->
object = context.lookupTemp "object"
code = context.lookupTemp "code"
newContext = new FLContext context, object
# yield from
toBeReturned = code.eval newContext, code
return toBeReturned
# To -------------------------------------------------------------------------
# TODO it'd be nice if there was a way not to leak the TempClass
# Note 1----
# we DON't want the signature to be "closed", so we use
# (flTokenize "( ' functionObjectName ) : ( 'signature ) do: ( 'functionBody )"),
# however at that point we also had to adjust the signature in
# "answer" otherwise you
# have a discrepancy, and "answer" needs an additional version
# that evaluates the signature because it needs
# a version where the signature is evalled exactly to implement the "to"
# here below.
#
# Note 2----
# the "functionObjectName" is not evalled to keep definitions more logo-like
# otherwise if you want to eval it you need to have it on its own line
# otherwise its evaluation might eat up the signature definition that
# follows it.
# The drawback is that you can't conditionally name a functionObjectName
# but that's not that common in other languages either.
FLTo.addMethod \
(flTokenize "( ' functionObjectName ) : ( 'signature ) do: ( 'functionBody )"),
flTokenize \
# functionObjectName contains a token i.e.
# it's a pointer. So to put something inside the
# variable *it's pointing at*,
# you need to do "functionObjectName eval"
# we take the "if" when the token is completely new, or if it's
# a string or a number for example, because it's clear that we don't
# want to add new methods to String or Number using "to" in
# that way. So we create a temp class and put a single instance
# of such class in the token.
#
# we skip the "if" when the token is bound to anything else other than
# a primitive type. In that case we just add/replace the
# method to whatever the token is bound to
"""
accessUpperContext
if (nil == functionObjectName eval) or (functionObjectName eval isPrimitiveType):
﹍'TempClass ← Class new
﹍TempClass nameit "Class_of_" + functionObjectName
﹍functionObjectName ← TempClass new
﹍TempClass answerEvalSignatureAndBody (signature) by (functionBody)
functionObjectName eval answerEvalSignatureAndBody (signature) by (functionBody)
"""
# TODO it'd be nice if there was a way not to leak the TempClass
FLTo.addMethod \
(flTokenize "( ' functionObjectName ) : ( 'functionBody )"),
flTokenize \
# see comments in method definition above
"""
accessUpperContext
if (nil == functionObjectName eval) or (functionObjectName eval isPrimitiveType):
﹍'TempClass ← Class new
﹍TempClass nameit "Class_of_" + functionObjectName
﹍functionObjectName ← TempClass new
functionObjectName eval answerEvalSignatureAndBody: () by (functionBody)
"""
# Class -------------------------------------------------------------------------
# Class. Like all classes, it's also an object, but it's the only object
# in the system that has the capacity to create
# new classes, via the "new" message below.
FLClass.addMethod \
(flTokenize "new"),
(context) ->
#yield
if methodsExecutionDebug
log "///////// creating a new class for the user!"
@createNew()
# Exception -------------------------------------------------------------------------
FLException.addMethod \
(flTokenize "new"),
(context) ->
#yield
@createNew ""
FLException.addMethod \
(flTokenize "initWith ( errorMessage )"),
(context) ->
#yield
errorMessage = context.lookupTemp "errorMessage"
@value = errorMessage.value
return @
FLException.addMethod \
(flTokenize "catch all : ( ' errorHandle )"),
(context) ->
errorHandle = context.lookupTemp "errorHandle"
if methodsExecutionDebug
log "catch: being thrown? " + context.throwing
log "catch: got right exception, catching it"
if @thrown
# yield from
toBeReturned = errorHandle.eval context, errorHandle
else
toBeReturned = @
return toBeReturned
FLException.addMethod \
# theError here is a token!
(flTokenize "catch ( 'theError ) : ( ' errorHandle )"),
(context) ->
#yield
theError = context.lookupTemp "theError"
errorHandle = context.lookupTemp "errorHandle"
# OK this is tricky: we'd normally just evaluate this from the
# signature BUT we can't, because it's going to be in this form:
# WITHOUT parens
# catch someError :
# so it's going to try to match the ":" token, and
# whenever an exception touches
# anything else other than a catch, it ALWAYS matches and
# re-throws itself, because this
# is how we get exceptions to bubble up when they are not
# caught. So, we get the token instead, and we look it up
# here.
# theError here is a token, with this evaluation we get an
# actual exception.
# yield from
theError = theError.eval context, theError
if methodsExecutionDebug
log "catch: same as one to catch?" + (@ == theError) + " being thrown? " + context.throwing
if @ == theError
if methodsExecutionDebug
log "catch: got right exception, catching it"
if @thrown
# yield from
toBeReturned = errorHandle.eval context, errorHandle
else
toBeReturned = @
else
if methodsExecutionDebug
log "catch: got wrong exception, propagating it"
toBeReturned = @
return toBeReturned
FLException.addMethod \
FLList.emptyMessage(),
(context) ->
#yield
if @thrown
context.throwing = true
return @
# String -------------------------------------------------------------------------
FLString.addMethod \
(flTokenize "new"),
(context) ->
#yield
@createNew ""
FLString.addMethod \
(flTokenize "+ ( stringToBeAppended )"),
(context) ->
#yield
stringToBeAppended = context.lookupTemp "stringToBeAppended"
return FLString.createNew @value + stringToBeAppended.flToString()
,4
FLString.addMethod \
(flTokenize "== ( toCompare )"),
commonSimpleValueEqualityFunction,
7
FLString.addMethod \
(flTokenize "!= ( toCompare )"),
commonSimpleValueInequalityFunction,
7
# Number -------------------------------------------------------------------------
FLNumber.addMethod \
(flTokenize "new"),
(context) ->
#yield
@createNew 0
FLNumber.addMethod \
(flTokenize "anotherPrint"),
flTokenize "console print self"
FLNumber.addMethod \
(flTokenize "doublePrint"),
flTokenize "console print(console print self)"
# mutates the very object
FLNumber.addMethod \
(flTokenize "incrementInPlace"),
# this one below actually mutates the number
# object
flTokenize "self ← self + 1"
FLNumber.addMethod \
(flTokenize "factorialtwo"),
flTokenize "if self == 0: ( 1 ) else: (self * ( ( self minus 1 ) factorialtwo ))"
FLNumber.addMethod \
(flTokenize "factorialthree"),
flTokenize "if self == 0: ( 1 ) else: ('temp ← self;console print temp; ( self minus 1 ) factorialthree * temp )"
FLNumber.addMethod \
(flTokenize "factorialfour"),
flTokenize \
"if self == 0: ( 1 ) else: ('temp ← self;\
( self minus 1 ) factorialfour * temp )"
FLNumber.addMethod \
(flTokenize "factorialfive"),
flTokenize \
"if self == 0: ( 1 ) else: (1 + 1;'temp ← self;\
( self minus 1 ) factorialfive * temp )"
FLNumber.addMethod \
(flTokenize "factorialsix"),
flTokenize "if self == 0: ( 1 ) else: (( self minus 1 ) factorialsix * self)"
FLNumber.addMethod \
(flTokenize "amIZero"),
flTokenize "self == 0"
FLNumber.addMethod \
(flTokenize "printAFromDeeperCall"),
flTokenize "console print a"
# generates a range list including the extremes
# e.g. 1...1 is (1), 1...0 is (1 0), 1...2 is (1 2)
#
# hs higher priority NUMBER (i.e. lower precedence)
# than +,-,*,/ so things like this can work:
#
# for each number in:
# numParams-1...0
#
FLNumber.addMethod \
(flTokenize "...(endRange)"),
(context) ->
#yield
endRange = context.lookupTemp "endRange"
listToBeReturned = FLList.createNew()
for i in [@value..endRange.value]
listToBeReturned.value.jsArrayPush FLNumber.createNew i
listToBeReturned.cursorEnd++
return listToBeReturned
,5
# ---
BasePlusFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
# todo more type conversions needed, and also in the other operations
if operandum.flClass == FLString
return FLString.createNew @value + operandum.value
else
return FLNumber.createNew @value + operandum.value
FLNumber.addMethod \
(flTokenize "$plus_binary_default ( operandum )"),
BasePlusFunction
BasePowerFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew Math.pow(@value, operandum.value)
FLNumber.addMethod \
(flTokenize "$power_binary_default ( operandum )"),
BasePowerFunction
FLNumber.addMethod \
(flTokenize "+ ( operandum )"),
(flTokenize "self $plus_binary_default operandum"),
4
# for power operator precedence discussions see
# https://esdiscuss.org/topic/exponentiation-operator-precedence
# Also, you can check Wolfram Alpha for what the
# mathematically-accurate results should be
FLNumber.addMethod \
(flTokenize "^ ( operandum )"),
(flTokenize "self $power_binary_default operandum"),
1,
ASSOCIATIVITY_RIGHT_TO_LEFT
FLNumber.addMethod \
(flTokenize "^ - ( operandum )"),
(flTokenize "self $power_binary_default ( - operandum )"),
1,
ASSOCIATIVITY_RIGHT_TO_LEFT
# although there are some good reasons to have this,
# it can get confusing, consider for example
# a++ ++
# the first ++ does this: a = a+1 and returns a number
# the second just increments the number without modifying
# a.
FLNumber.addMethod \
(flTokenize "++"),
flTokenize "self + 1"
# see "++" regarding why this could be confusing
FLNumber.addMethod \
(flTokenize "+= (value)"),
flTokenize "self + value"
# ---
BasePercentFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew @value % operandum.value
FLNumber.addMethod \
(flTokenize "$percent_binary_default ( operandum )"),
BasePercentFunction
FLNumber.addMethod \
(flTokenize "% ( operandum )"),
(flTokenize "self $percent_binary_default operandum"),
3
# ---
BaseFloorDivisionFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew Math.floor(@value / operandum.value)
FLNumber.addMethod \
(flTokenize "$floordivision_binary_default ( operandum )"),
BaseFloorDivisionFunction
FLNumber.addMethod \
(flTokenize "/_ ( operandum )"),
(flTokenize "self $floordivision_binary_default operandum"),
3
# ---
BaseMinusFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew @value - operandum.value
FLNumber.addMethod \
(flTokenize "$minus_binary_default ( operandum )"),
BaseMinusFunction
FLNumber.addMethod \
(flTokenize "- ( operandum )"),
(flTokenize "self $minus_binary_default operandum"),
4
# ---
BaseDivideFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew @value / operandum.value
FLNumber.addMethod \
(flTokenize "$divide_binary_default ( operandum )"),
BaseDivideFunction
FLNumber.addMethod \
(flTokenize "/ ( operandum )"),
(flTokenize "self $divide_binary_default operandum"),
3
# ---
BaseMultiplyFunction = (context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew @value * operandum.value
FLNumber.addMethod \
(flTokenize "$multiply_binary_default ( operandum )"),
BaseMultiplyFunction
FLNumber.addMethod \
(flTokenize "* ( operandum )"),
(flTokenize "self $multiply_binary_default operandum"),
3
# see "++" regarding why this could be confusing
FLNumber.addMethod \
(flTokenize "*= (value)"),
flTokenize "self * value"
# ---
FLNumber.addMethod \
(flTokenize "minus ( operandum )"),
(context) ->
#yield
operandum = context.lookupTemp "operandum"
return FLNumber.createNew @value - operandum.value
FLNumber.addMethod \
(flTokenize "selftimesminusone"),
flTokenize "self * self minus 1"
FLNumber.addMethod \
(flTokenize "times ( ' loopCode )"),
(context) ->
loopCode = context.lookupTemp "loopCode"
if methodsExecutionDebug
log "FLNumber: times loop code is: " + loopCode.flToString()
for i in [0...@value]
# yield from
toBeReturned = loopCode.eval context, loopCode
#flContexts.pop()
# catch any thrown "done" object, used to
# exit from a loop.
if toBeReturned?
if context.throwing and (toBeReturned.flClass == FLDone or toBeReturned.flClass == FLBreak)
context.throwing = false
if toBeReturned.value?
toBeReturned = toBeReturned.value
if methodsExecutionDebug
log "times loop exited with Done "
break
if context.throwing and toBeReturned.flClass == FLReturn
if methodsExecutionDebug
log "times loop exited with Return "
break
return toBeReturned
FLNumber.addMethod \
(flTokenize "== ( toCompare )"),
commonSimpleValueEqualityFunction,
7
FLNumber.addMethod \
(flTokenize "!= ( toCompare )"),
commonSimpleValueInequalityFunction,
7
FLNumber.addMethod \
(flTokenize "< ( toCompare )"),
(context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @value < toCompare.value
return FLBoolean.createNew true
else
return FLBoolean.createNew false
,6
FLNumber.addMethod \
(flTokenize "> ( toCompare )"),
(context) ->
#yield
toCompare = context.lookupTemp "toCompare"
if @value > toCompare.value
return FLBoolean.createNew true
else
return FLBoolean.createNew false
,6
# mutating the number
FLNumber.addMethod \
(flTokenize "← ( valueToAssign )"),
(context) ->
#yield
if methodsExecutionDebug
log "evaluation " + indentation() + "assigning to number! "
valueToAssign = context.lookupTemp "valueToAssign"
@value = valueToAssign.value
return @
# Boolean -------------------------------------------------------------------------
FLBoolean.addMethod \
(flTokenize "negate"),
(context) ->
#yield
return FLBoolean.createNew !@value
FLBoolean.addMethod \
(flTokenize "and ( operandum )"),
(context) ->