-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.st
6135 lines (4190 loc) · 107 KB
/
bootstrap.st
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
RawClass MetaObject Class Class Dependencies
Class Object
Class HTTPRequest Object request requestLine method uri version
Class CPointer Object
Class Magnitude Object
Class Method Object text message bytecodes literals stackSize temporarySize class watch protocol
Class Number Magnitude
Class NullQueen Object
Class Random Object
Class Collection Magnitude
Class IndexedCollection Collection
Class Dictionary IndexedCollection hashTable
RawClass MetaTestCase Class MetaObject failedCheckSignal
Class TestCase Object selector
Class StringTest TestCase string emptyString subcollection3ElementsSorted nonEmpty5ElementsSorted unsortedCollection indexInNonEmptyArray arrayWithCharacters nonEmpty1element withoutEqualElements sameAtEndAndBegining elementInNonEmpty collectionNotIncluded notIn
Class TestResult Object startTime stopTime Time testName failures errors
Class ObjectMemory Object
Class Boolean Object
Class Class Object name instanceSize methods superClass variables
RawClass MetaFFI Class MetaObject libraries
Class FFI Object handle name functions
Class List Collection links
Class Set List
Class Switch Object const notdone
Class Array IndexedCollection
Class ByteArray Array
Class ExternalData Object members
Class EDTeset ExternalData
Class Float Number
Class Fraction Number top bottom
Class Exception Object message
Class Error Exception
Class nil
Class SetTest TestCase
Class Smalltalk Object
Class ExceptionHandler Object handledException handlerBlock
Class UndefinedObject Object
Class TestSuite Object name testCases
Class TestConversions TestCase
Class Char Magnitude value
Class Zebra Object
Class ArrayTest TestCase
Class Process Object stack stackTop linkPointer
Class Symbol Object
Class String ByteArray
Class EmployeeRecord Object name idNumber position salary
Class False Boolean
Class Semaphore Object count processList
Class True Boolean
Class Rectangle Object top left bottom right
Class Interval Collection lower upper step
Class Integer Number
Class LongInteger Integer negative digits
Class Block Object context argCount argLoc bytePointer
Class File Object name number mode
Class FailedCheckSignal Exception
RawClass MetaTime Class MetaMagnitude ClockAtStartup
Class Time Magnitude
Class TestRunner Object
Class Test nil
Class Context Object linkLocation method arguments temporaries
Class Circle Object center radius
Class Scheduler Object notdone processList currentProcess
Class Link Object key value nextLink
Class EmployeeDatabase Object records
RawClass MetaModule Class MetaObject modules
Class Module Object name root
Class Queen Object row column neighbor
Class Workspace Object dialog commands
Class Point Magnitude x y
Methods HTTPRequest
version
^ version
]
Methods HTTPRequest
parseRequest
| lines words |
lines <- request words: [ :x | ((x = (Char lf)) or: [ x = (Char cr)]) not ].
( lines size > 0 ) ifTrue: [
requestLine <- lines at: 1.
words <- requestLine words: [ :x | (x = $ ) not ].
(words includesKey: 1) ifTrue: [
method <- words at: 1.
(words includesKey: 2) ifTrue: [
uri <- words at: 2.
(words includesKey: 3) ifTrue: [
version <- words at: 3
]
]
]
]
]
Methods HTTPRequest
init: request
request <- request
]
Methods HTTPRequest
uri
^ uri
]
Methods HTTPRequest
method
^ method
]
Methods HTTPRequest
requestLine
^ requestLine
]
Methods CPointer
printString
^ self asString
]
Methods CPointer
asString
^ <141 self>
]
Methods Panel
init: browser
browser <- browser.
]
Methods Panel
widget
^ widget
]
Methods Magnitude
>= value
^ value <= self
]
Methods Magnitude
isChar
^ false
]
Methods Magnitude
<= value
^ (self < value) or: [ self = value ]
]
Methods Magnitude
max: value
^ (self < value)
ifTrue: [ value ]
ifFalse: [ self ]
]
Methods Magnitude
between: low and: high
^ (low <= self) and: [ self <= high ]
]
Methods Magnitude
> value
^ (value < self)
]
Methods Magnitude
< value
^ (self <= value) and: [ self ~= value ]
]
Methods Magnitude
~= value
^ (self = value) not
]
Methods Magnitude
min: value
^ (self < value)
ifTrue: [ self ]
ifFalse: [ value ]
]
Methods Magnitude
= value
^ (self == value)
]
Methods Method
disassemble: indent at: initPC for: initCount | pc low high count needCR |
pc <- initPC.
count <- initCount.
[ count > 0 ] whileTrue: [
" Default, terminate line with CR "
needCR <- true.
" Show PC and indent listing of line "
((pc rem: 20) = 0) ifTrue: [
low <- String input.
((low size > 0) and: [ (low at: 1) = $q ]) ifTrue: [
self error: 'Disassembly halted'
]
].
(pc - 1) print.
(1 to: indent) do: [:x| ' ' print].
" Fetch basic opcode fields "
high <- bytecodes at: pc.
pc <- pc + 1.
count <- count - 1.
low <- high rem: 16.
high <- high quo: 16.
(high = 0) ifTrue: [
high <- low.
low <- bytecodes at: pc.
pc <- pc + 1.
count <- count - 1
].
(high = 1) ifTrue: [
'PushInstance ' print.
low print
].
(high = 2) ifTrue: [
'PushArgument ' print.
low print
].
(high = 3) ifTrue: [
'PushTemporary ' print.
low print
].
(high = 4) ifTrue: [
'PushLiteral ' print.
low print
].
(high = 5) ifTrue: [
'PushConstant ' print.
((low >= 0) and: [low < 10]) ifTrue: [
low print
].
(low = 10) ifTrue: [
'nil' print
].
(low = 11) ifTrue: [
'true' print
].
(low = 12) ifTrue: [
'false' print
]
].
(high = 6) ifTrue: [
'AssignInstance ' print.
low print
].
(high = 7) ifTrue: [
'AssignTemporary ' print.
low print
].
(high = 8) ifTrue: [
'MarkArguments ' print.
low print
].
(high = 9) ifTrue: [
'SendMessage ' print.
(literals at: (low+1)) print
].
(high = 10) ifTrue: [
'SendUnary ' print.
(low = 0) ifTrue: [ 'isNil' print ].
(low = 1) ifTrue: [ 'notNil' print ]
].
(high = 11) ifTrue: [
'SendBinary ' print.
(low = 0) ifTrue: [ '<' print ].
(low = 1) ifTrue: [ '<=' print ].
(low = 2) ifTrue: [ '+' print ]
].
(high = 12) ifTrue: [
'PushBlock' print. Char newline print.
low <- bytecodes at: pc.
pc <- pc + 1.
low <- low + ((bytecodes at: pc) * 256).
pc <- pc + 1.
high <- low - (pc-1).
self disassemble: indent+4 at: pc for: high.
count <- count - (high + 2).
pc <- pc + high.
needCR <- false
].
(high = 13) ifTrue: [
'DoPrimitive ' print.
high <- bytecodes at: pc.
pc <- pc + 1.
count <- count - 1.
" VM generic "
(high = 1) ifTrue: [ '=' print ].
(high = 2) ifTrue: [ 'class' print ].
(high = 3) ifTrue: [ 'putchar' print ].
(high = 4) ifTrue: [ 'size' print ].
(high = 5) ifTrue: [ 'at:put:' print ].
(high = 6) ifTrue: [ 'newProcess' print ].
(high = 7) ifTrue: [ 'new' print ].
(high = 8) ifTrue: [ 'block invoke' print ].
(high = 9) ifTrue: [ 'getchar' print ].
(high = 10) ifTrue: [ 'SmallInt +' print ].
(high = 11) ifTrue: [ 'SmallInt /' print ].
(high = 12) ifTrue: [ 'SmallInt %' print ].
(high = 13) ifTrue: [ 'SmallInt <' print ].
(high = 14) ifTrue: [ 'SmallInt =' print ].
(high = 15) ifTrue: [ 'SmallInt *' print ].
(high = 16) ifTrue: [ 'SmallInt -' print ].
(high = 18) ifTrue: [ 'debug' print ].
(high = 19) ifTrue: [ 'error' print ].
(high = 20) ifTrue: [ 'ByteArray new' print ].
(high = 21) ifTrue: [ 'String at:' print ].
(high = 22) ifTrue: [ 'String at:put:' print ].
(high = 23) ifTrue: [ 'String clone' print ].
(high = 24) ifTrue: [ 'Array at:' print ].
(high = 25) ifTrue: [ 'Integer /' print ].
(high = 26) ifTrue: [ 'Integer %' print ].
(high = 27) ifTrue: [ 'Integer +' print ].
(high = 28) ifTrue: [ 'Integer *' print ].
(high = 29) ifTrue: [ 'Integer -' print ].
(high = 30) ifTrue: [ 'Integer <' print ].
(high = 31) ifTrue: [ 'Integer =' print ].
(high = 32) ifTrue: [ 'SmallInt asInteger' print ].
(high = 33) ifTrue: [ 'Integer asSmallInt' print ].
" UNIX port specific "
(high = 100) ifTrue: [ 'UNIX fopen' print ].
(high = 101) ifTrue: [ 'UNIX fgetc' print ].
(high = 102) ifTrue: [ 'UNIX fputc' print ].
(high = 103) ifTrue: [ 'UNIX fclose' print ].
(high = 104) ifTrue: [ 'UNIX fileout' print ].
(high = 105) ifTrue: [ 'UNIX edit' print ]
].
(high = 15) ifTrue: [
'DoSpecial ' print.
(low = 1) ifTrue: [ 'selfReturn' print ].
(low = 2) ifTrue: [ 'stackReturn' print ].
(low = 3) ifTrue: [ 'blockReturn' print ].
(low = 4) ifTrue: [ 'duplicate' print ].
(low = 5) ifTrue: [ 'popTop' print ].
(low = 6) ifTrue: [
'branch ' print.
high <- bytecodes at: pc.
pc <- pc + 1.
high <- high + ((bytecodes at: pc) * 256).
pc <- pc + 1.
count <- count - 2.
high print
].
(low = 7) ifTrue: [
'branchIfTrue ' print.
high <- bytecodes at: pc.
pc <- pc + 1.
high <- high + ((bytecodes at: pc) * 256).
pc <- pc + 1.
count <- count - 2.
high print
].
(low = 8) ifTrue: [
'branchIfFalse ' print.
high <- bytecodes at: pc.
pc <- pc + 1.
high <- high + ((bytecodes at: pc) * 256).
pc <- pc + 1.
count <- count - 2.
high print
].
(low = 11) ifTrue: [
'sendToSuper ' print.
low <- bytecodes at: pc.
pc <- pc + 1.
count <- count - 1.
(literals at: low+1) print
].
(low = 12) ifTrue: [ 'breakpoint' print ]
].
needCR ifTrue: [
Char newline print
]
]
]
Methods Method
printString
^ message asString
]
Methods Method
watch: aBlock
watch <- aBlock
]
Methods Method
compileWithClass: aClass
^ <39 aClass text self>
]
Methods Method
signature
^ class asString,' ', message asString
]
Methods Method
disassemble
self disassemble: 1 at: 1 for: (bytecodes size)
]
Methods Method
display
('Method ', message) print.
'text' print.
text print.
'literals' print.
literals print.
'bytecodes' print.
bytecodes class print.
bytecodes do: [:x |
(x printString, ' ', (x quo: 16), ' ', (x rem: 16))
print ]
]
Methods Method
watchWith: arguments
" note that we are being watched "
text print.
watch value: arguments.
^ self executeWith: arguments
]
Methods Method
protocol: aString
protocol <- aString
]
Methods Method
text
^ (text notNil)
ifTrue: [ text ]
ifFalse: [ 'text not saved']
]
Methods Method
text: aString
text <- aString
]
Methods Method
protocol
^ protocol
]
Methods Method
message: aSymbol
message <- aSymbol
]
Methods Method
name
^ message
]
Methods Method
executeWith: arguments
^ ( Context new ; method: self ;
temporaries: ( Array new: temporarySize) ;
arguments: arguments )
returnToBlock: 1
]
Methods MetaChar
cr
^ 13 asCharacter
]
Methods MetaChar
new: aValue
| newobj |
newobj <- super new.
newobj value: aValue.
^ newobj
]
Methods MetaChar
lf
^ 10 asCharacter
]
Methods Number
+ value
^ (self maxgen: value) + (value maxgen: self)
]
Methods Number
\\ value
" remainder after integer division "
^ self - (self // value * value)
]
Methods Number
negative
^ self < 0
]
Methods Number
squared
^ self * self
]
Methods Number
@ v
^ Point new; x: self; y: v
]
Methods Number
strictlyPositive
^ self > 0
]
Methods Number
negated
^ 0 - self
]
Methods Number
isNumber
^ true
]
Methods Number
ceiling | i |
i <- self truncated.
^ ((self positive) and: [ self ~= i ])
ifTrue: [ i + 1 ]
ifFalse: [ i ]
]
Methods Number
raisedTo: x | y |
x negative
ifTrue: [ ^ 1 / (self raisedTo: x negated) ].
x isShortInteger
ifTrue: [ (x = 0) ifTrue: [ ^ 1 ].
y <- (self raisedTo: (x quo: 2)) squared.
x odd ifTrue: [ y <- y * self ].
^ y ]
"use logrithms to do exponeneation"
ifFalse: [ ^ ( x * self ln ) exp ]
]
Methods Number
rem: value
^ self - ((self quo: value) * value)
]
Methods Number
reciprocal
^ 1 / self
]
Methods Number
sqrt
^ (self negative)
ifTrue: [ smalltalk error: 'sqrt of negative']
ifFalse: [ self raisedTo: 0.5 ]
]
Methods Number
trucateTo: value
^ (self / value) trucated * value
]
Methods Number
printWidth: width base: base | res n dig wasNeg wide |
res <- ''.
(self negative) ifTrue: [
wasNeg <- true.
wide <- width-1.
n <- self negated
] ifFalse: [
wasNeg <- false.
wide <- width.
n <- self
].
[true] whileTrue: [
res <- ((n rem: base) asDigit) + res.
n <- n quo: base.
(n = 0) ifTrue: [
((res size)+1) to: wide do: [:ignore|
res <- '0' + res
].
wasNeg ifTrue: [ res <- '-' + res ].
^res
]
]
]
Methods Number
abs
^ (self < 0)
ifTrue: [ 0 - self ]
ifFalse: [ self ]
]
Methods Number
/ value
^ (self maxgen: value) / (value maxgen: self)
]
Methods Number
ln
^ self asFloat ln
]
Methods Number
copy
^ self
]
Methods Number
quo: value
^ (self maxgen: value) quo: (value maxgen: self)
]
Methods Number
< value
^ (self maxgen: value) < (value maxgen: self)
]
Methods Number
roundTo: value
^ (self / value ) rounded * value
]
Methods Number
* value
^ (self maxgen: value) * (value maxgen: self)
]
Methods Number
floor | i |
i <- self truncated.
^ ((self negative) and: [ self ~= i ])
ifTrue: [ i - 1 ]
ifFalse: [ i ]
]
Methods Number
fractionalPart
^ self - self truncated
]
Methods Number
exp
^ self asFloat exp
]
Methods Number
maxgen: value
(self isNumber and: [ value isNumber ])
ifFalse: [ ^ smalltalk error:
'arithmetic on non-numbers' ].
^ (self generality > value generality)
ifTrue: [ self ]
ifFalse: [ value coerce: self ]
]
Methods Number
to: value by: step
^ Interval new; lower: self; upper: value; step: step
]
Methods Number
isInteger
^ self isLongInteger or: [ self isShortInteger ]
]
Methods Number
positive
^ self >= 0
]
Methods Number
- value
^ (self maxgen: value) - (value maxgen: self)
]
Methods Number
// value
" integer division, truncate towards negative infinity"
" see quo: "
^ (self / value) floor
]
Methods Number
log: value
^ self ln / value ln
]
Methods Number
sign
^ (self = 0) ifTrue: [ 0 ]
ifFalse: [ self / self abs ]
]
Methods Number
= value
^ value isNumber
ifTrue: [ (self maxgen: value) = (value maxgen: self) ]
ifFalse: [ false ]
]
Methods Number
to: value
^ Interval new; lower: self; upper: value; step: 1
]
Methods NullQueen
checkRow: row column: column
" we can't attack anything "
^ false
]
Methods NullQueen
next
^ true
]
Methods NullQueen
result
^ List new
]
Methods NullQueen
first
^ true
]
Methods Random
next: value | list |
" return a list of random numbers of given size "
list <- List new.
value timesRepeat: [ list add: self next ].
^ list
]
Methods Random
randInteger: value
^ 1 + (<3> rem: value)
]
Methods Random
between: low and: high
" return random number in given range "
^ (self next * (high - low)) + low
]
Methods Random
next
" convert rand integer into float between 0 and 1 "
^ (<3> rem: 1000) / 1000
]
Methods Random
set: value
" set seed for random number generator "
<55 value>
]
Methods Dictionary
removeKey: aKey ifAbsent: exceptionBlock
^ (self includesKey: aKey)
ifTrue: [ self basicRemoveKey: aKey ]
ifFalse: exceptionBlock
]
Methods Dictionary
new
hashTable <- Array new: 39
]
Methods Dictionary
hash: aKey
^ 3 * ((aKey hash) rem: ((hashTable size) quo: 3))
]
Methods Dictionary
display
self binaryDo: [:x :y | (x printString , ' -> ',
y printString ) print ]
]
Methods Dictionary
removeKey: aKey
^ self removeKey: aKey
ifAbsent: [ smalltalk error: 'remove key not found']
]
Methods Dictionary
basicRemoveKey: aKey | hashPosition link |
hashPosition <- self hash: aKey.
((hashTable at: hashPosition + 1) = aKey)
ifTrue: [ hashTable at: hashPosition + 1 put: nil.
hashTable at: hashPosition + 2 put: nil]
ifFalse: [ link <- hashTable at: hashPosition + 3.
(link notNil)
ifTrue: [ hashTable at: hashPosition + 3
put: (link removeKey: aKey) ]]
]
Methods Dictionary
at: aKey ifAbsent: exceptionBlock | hashPosition link |
hashPosition <- self hash: aKey.
((hashTable at: hashPosition + 1) = aKey)
ifTrue: [ ^ hashTable at: hashPosition + 2].
link <- hashTable at: hashPosition + 3.