-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseLibrary.smalltalk
2975 lines (2969 loc) · 78.1 KB
/
BaseLibrary.smalltalk
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
nil
subclass: #Object
instanceVariableNames: ''!
Object
subclass: #Behavior
instanceVariableNames: 'name instanceSize methods superClass variables'!
Behavior
subclass: #Class
instanceVariableNames: ''!
Behavior
subclass: #Metaclass
instanceVariableNames: ''!
Object
subclass: #Block
instanceVariableNames: 'context argCount argLoc bytePointer'!
Object
subclass: #Boolean
instanceVariableNames: ''!
Boolean
subclass: #False
instanceVariableNames: ''!
Boolean
subclass: #True
instanceVariableNames: ''!
Object
subclass: #Context
instanceVariableNames: 'linkLocation method arguments temporaries'!
Object
subclass: #Encoder
instanceVariableNames: 'parser name byteCodes index literals stackSize maxStack'!
Object
subclass: #File
instanceVariableNames: 'name number mode'!
Object
subclass: #Link
instanceVariableNames: 'key value nextLink'!
Object
subclass: #Magnitude
instanceVariableNames: ''!
Magnitude
subclass: #Char
instanceVariableNames: 'value'!
Magnitude
subclass: #Collection
instanceVariableNames: ''!
Collection
subclass: #IndexedCollection
instanceVariableNames: ''!
IndexedCollection
subclass: #Array
instanceVariableNames: ''!
Array
subclass: #ByteArray
instanceVariableNames: ''!
ByteArray
subclass: #String
instanceVariableNames: ''!
IndexedCollection
subclass: #Dictionary
instanceVariableNames: 'hashTable'!
Dictionary
subclass: #SymbolTable
instanceVariableNames: ''!
Collection
subclass: #Interval
instanceVariableNames: 'lower upper step'!
Collection
subclass: #List
instanceVariableNames: 'links'!
List
subclass: #Set
instanceVariableNames: ''!
Magnitude
subclass: #Number
instanceVariableNames: ''!
Number
subclass: #Float
instanceVariableNames: ''!
Number
subclass: #Fraction
instanceVariableNames: 'top bottom'!
Number
subclass: #Integer
instanceVariableNames: ''!
Integer
subclass: #LongInteger
instanceVariableNames: 'negative digits'!
Object
subclass: #Method
instanceVariableNames: 'text message bytecodes literals stackSize temporarySize class watch'!
Object
subclass: #Parser
instanceVariableNames: 'text index tokenType token argNames tempNames instNames maxTemps errBlock'!
Object
subclass: #ParserNode
instanceVariableNames: ''!
ParserNode
subclass: #ArgumentNode
instanceVariableNames: 'position'!
ParserNode
subclass: #AssignNode
instanceVariableNames: 'target expression'!
ParserNode
subclass: #BlockNode
instanceVariableNames: 'statements temporaryLocation argumentCount temporaryCount'!
ParserNode
subclass: #BodyNode
instanceVariableNames: 'statements'!
ParserNode
subclass: #CascadeNode
instanceVariableNames: 'head list'!
ParserNode
subclass: #InstNode
instanceVariableNames: 'position'!
ParserNode
subclass: #LiteralNode
instanceVariableNames: 'value'!
ParserNode
subclass: #MessageNode
instanceVariableNames: 'receiver name arguments'!
ParserNode
subclass: #PrimitiveNode
instanceVariableNames: 'number arguments'!
ParserNode
subclass: #ReturnNode
instanceVariableNames: 'expression'!
ParserNode
subclass: #TemporaryNode
instanceVariableNames: 'position'!
ParserNode
subclass: #ValueNode
instanceVariableNames: 'name'!
Object
subclass: #Process
instanceVariableNames: 'stack stackTop linkPointer overflowed'!
Object
subclass: #Random
instanceVariableNames: ''!
Object
subclass: #Scheduler
instanceVariableNames: 'notdone processList currentProcess'!
Object
subclass: #Semaphore
instanceVariableNames: 'count processList'!
Object
subclass: #Smalltalk
instanceVariableNames: ''!
Object
subclass: #Switch
instanceVariableNames: 'const notdone'!
Object
subclass: #Symbol
instanceVariableNames: ''!
Object
subclass: #UndefinedObject
instanceVariableNames: ''!
{!
ArgumentNode methods!
compile: encoder block: inBlock
position = 0
ifTrue: [ encoder genHigh: 2 low: 0 ]
ifFalse: [ encoder genHigh: 2 low: position - 1 ]!
isSuper
^ position = 0!
position: p
position <- p!
}!
{!
ArrayMeta methods!
basicNew
^ self basicNew: 0!
new
^ self new: 0!
}!
{!
Array methods!
< coll
(coll isKindOf: Array)
ifTrue: [ self with: coll
do: [:x :y | (x = y) ifFalse:
[ ^ x < y ]].
^ self size < coll size ]
ifFalse: [ ^ super < coll ]!
= coll
(coll isKindOf: Array)
ifTrue: [ (self size = coll size)
ifFalse: [ ^ false ].
self with: coll
do: [:x :y | (x = y)
ifFalse: [ ^ false ] ].
^ true ]
ifFalse: [ ^ super = coll ]!
at: index put: value
(self includesKey: index)
ifTrue: [ self basicAt: index put: value ]
ifFalse: [ smalltalk error:
'illegal index to at:put: for array' ]!
binaryDo: aBlock
(1 to: self size) do:
[:i | aBlock value: i value: (self at: i) ]!
collect: aBlock | s newArray |
s <- self size.
newArray <- Array new: s.
(1 to: s) do: [:i | newArray at: i put:
(aBlock value: (self at: i))].
^ newArray!
copyFrom: low to: high | newArray newlow newhigh |
newlow <- low max: 1.
newhigh <- high min: self size.
newArray <- self class new: (0 max: newhigh - newlow + 1).
(newlow to: newhigh)
do: [:i | newArray at: ((i - newlow) + 1)
put: (self at: i) ].
^ newArray!
deepCopy
^ self deepCopyFrom: 1 to: self size!
deepCopyFrom: low to: high | newArray newlow newhigh |
newlow <- low max: 1.
newhigh <- high min: self size.
newArray <- self class new: (0 max: newhigh - newlow + 1).
(newlow to: newhigh)
do: [:i | newArray at: ((i - newlow) + 1)
put: (self at: i) copy ].
^ newArray!
do: aBlock
(1 to: self size) do:
[:i | aBlock value: (self at: i) ]!
exchange: a and: b | temp |
temp <- self at: a.
self at: a put: (self at: b).
self at: b put: temp!
grow: aValue | s newArray |
s <- self size.
newArray <- Array new: s + 1.
(1 to: s) do: [:i | newArray at: i put: (self at: i)].
newArray at: s+1 put: aValue.
^ newArray!
includesKey: index
^ index between: 1 and: self size!
reverseDo: aBlock
(self size to: 1 by: -1) do:
[:i | aBlock value: (self at: i) ]!
select: aCond | newList |
newList <- List new.
self do: [:i | (aCond value: i) ifTrue: [newList addLast: i]].
^ newList asArray!
shallowCopy
^ self copyFrom: 1 to: self size!
size
^ self basicSize!
with: newElement | s newArray |
s <- self size.
newArray <- Array new: (s + 1).
(1 to: s) do: [:i | newArray at: i put: (self at: i) ].
newArray at: s+1 put: newElement.
^ newArray!
with: coll do: aBlock
(1 to: (self size min: coll size))
do: [:i | aBlock value: (self at: i)
value: (coll at: i) ]!
with: coll ifAbsent: z do: aBlock | xsize ysize |
xsize <- self size.
ysize <- coll size.
(1 to: (xsize max: ysize))
do: [:i | aBlock value:
(i <= xsize ifTrue: [ self at: i ] ifFalse: [ z ])
value:
(i <= ysize ifTrue: [ coll at: i ] ifFalse: [ z ])]!
}!
{!
AssignNode methods!
compile: encoder block: inBlock
target class == ValueNode ifTrue: [ "fix"
target assign: encoder value: expression block: inBlock ]
ifFalse: [
expression compile: encoder block: inBlock.
target assign: encoder ]!
target: t expression: e
target <- t.
expression <- e!
}!
{!
Behavior methods!
addMethod | m |
(m <- self doEdit: '') notNil ifTrue: [
self install: m ]!
basicNew
^ self primOrefs: instanceSize!
basicNew: size
^ self primOrefs: size!
display
('Class name: ', name asString) print.
(superClass notNil)
ifTrue: [ ('Superclass: ', superClass ) print ].
'Instance Variables:' print.
variables isNil
ifTrue: [ 'no instance variables ' print ]
ifFalse: [ variables display ].
'Subclasses: ' print.
self subClasses display!
doEdit: aString | tmp ans |
" edit a method definition until it compiles correctly "
tmp <- aString.
[ tmp <- tmp edit trimmed.
ans <- Parser new parse: tmp in: self.
ans notNil ifTrue: [
^ ans ]
ifFalse: [
smalltalk inquire: 'edit again (yn) ? ' ] ] whileTrue.
^ nil!
editMethod: name | m |
m <- self methodNamed: name.
m notNil ifTrue: [
(m <- self doEdit: m text) notNil ifTrue: [
self install: m ] ]
ifFalse: [
'no such method' print ]!
fileOut: aSym | aMth cStr mStr aStr aFile |
" file out one method on class.method.st "
(aMth <- self methodNamed: aSym) isNil ifTrue: [
^ self ].
cStr <- aMth methodClass name asString.
mStr <- aMth name asString.
aStr <- cStr , '.' , mStr , '.st'.
(aFile <- File name: aStr mode: 'w') open.
aFile putChunk: '{'.
aFile putChunk: cStr , ' methods'.
aFile putChunk: aMth trimmedText.
aFile putChunk: '}'.
aFile close!
fileOutMethodsOn: aFile | sorted |
" file out all methods "
methods isNil ifTrue: [
methods <- Dictionary new ]. "fix"
methods isEmpty ifFalse: [
sorted <- methods sort: [ :x :y |
x name asString < y name asString ].
aFile putChunk: '{'.
aFile putChunk: name asString , ' methods'.
sorted do: [ :y |
aFile putChunk: y trimmedText ].
aFile putChunk: '}' ]!
install: aMethod | sel old | "fix?"
sel <- aMethod name.
old <- self methodNamed: sel. "avoid GC lossage?"
methods at: sel put: aMethod.
<38 sel self>. "primFlushCache"
self logMethod: aMethod!
instanceSize
^ instanceSize!
logMethod: aMethod
'{' logChunk.
(self name asString , ' methods') logChunk.
aMethod trimmedText logChunk.
'}' logChunk!
methodNamed: name
(methods includesKey: name)
ifTrue: [ ^ methods at: name ].
(superClass notNil)
ifTrue: [ ^ superClass methodNamed: name ].
^ nil!
methods
^ methods!
name
^ name!
name: aString
name <- aString!
name: nSym instanceSize: iInt methods: mDict superClass: sClass variables: vArray
name <- nSym.
instanceSize <- iInt.
methods <- mDict.
superClass <- sClass.
variables <- vArray!
new
^ self primOrefs: instanceSize!
new: size
^ self primOrefs: size!
newMethod: aStr | m |
(m <- self doEdit: aStr) notNil ifTrue: [
self install: m ]!
primBytes: size
"create a new block, set its class"
^ <22 <59 size> self>!
primOrefs: size
"create a new block, set its class"
^ <22 <58 size> self>!
printString
^ name asString!
readMethods
[ smalltalk inquire: 'Add a method (yn) ? ' ]
whileTrue: [ self addMethod ]!
removeMethod: name | m |
m <- self methodNamed: name.
(m notNil and: [m methodClass == self]) ifTrue: [
methods removeKey: name.
<38 name self> ] "primFlushCache"
ifFalse: [
'no such method' print ]!
respondsTo | theSet |
theSet <- Dictionary new.
self upSuperclassChain:
[:x | theSet addAll: x methods ].
^ theSet!
superClass
^ superClass!
superClass: aClass
superClass <- aClass!
upSuperclassChain: aBlock
aBlock value: self.
(superClass notNil)
ifTrue: [ superClass upSuperclassChain: aBlock ]!
variables
^ variables!
variables: nameArray
variables <- nameArray.
instanceSize <- superClass instanceSize + nameArray size!
viewMethod: methodName | m |
m <- self methodNamed: methodName.
(m notNil)
ifTrue: [ m signature print. m trimmedText print ]
ifFalse: [ 'no such method' print ]!
watch: name | m |
m <- self methodNamed: name.
(m notNil)
ifTrue: [ ^ m watch:
[:a | ('executing ', name) print. a print] ]
ifFalse: [ ^ 'no such method' ]!
}!
{!
Block methods!
blockContext: ctx
context <- ctx!
checkArgumentCount: count
^ (argCount = count)
ifTrue: [ true ]
ifFalse: [ smalltalk error:
'wrong number of arguments passed to block'.
false ]!
fork
self newProcess resume!
forkWith: args
(self newProcessWith: args) resume!
newProcess
" create a new process to execute block "
^ Process context: context startAt: bytePointer!
newProcessWith: args
(self checkArgumentCount: args size)
ifTrue: [ (1 to: args size) do: [:i |
context at: (argLoc + i - 1)
put: (args at: i)]].
^ self newProcess!
value
^ (self checkArgumentCount: 0)
ifTrue: [ context returnToBlock: bytePointer ]!
value: x
^ (self checkArgumentCount: 1)
ifTrue: [ context at: argLoc put: x.
context returnToBlock: bytePointer ]!
value: x value: y
^ (self checkArgumentCount: 2)
ifTrue: [ context at: argLoc put: x.
context at: argLoc + 1 put: y.
context returnToBlock: bytePointer ]!
value: x value: y value: z
^ (self checkArgumentCount: 3)
ifTrue: [ context at: argLoc put: x.
context at: argLoc + 1 put: y.
context at: argLoc + 2 put: z.
context returnToBlock: bytePointer ]!
whileFalse: aBlock
[ self value not ] whileTrue: aBlock!
whileTrue
self whileTrue: []!
whileTrue: aBlock
( self value ) ifTrue:
[ aBlock value.
self whileTrue: aBlock ]!
}!
{!
BlockNode methods!
compile: encoder block: inBlock | blk fwd |
blk <- self newBlock. "fix"
encoder genHigh: 4 low: (encoder genLiteral: blk).
encoder genHigh: 5 low: 4. "ldc thisContext"
encoder genHigh: 13 low: 2. "prim 29"
encoder genCode: 29.
encoder genHigh: 15 low: 6. "jmp <fwd>"
fwd <- encoder genCode: 0.
blk basicAt: 4 put: encoder currentLocation + 1.
self compileInLine: encoder block: true.
encoder genHigh: 15 low: 2. "rtnt"
encoder hack: fwd "<fwd>:" "fix?"!
compileInLine: encoder block: inBlock
| base |
temporaryCount > 0 ifTrue: [
base <- temporaryLocation + argumentCount.
(1 to: temporaryCount) do: [ :i |
encoder genHigh: 5 low: 5. "ldc nil"
encoder genHigh: 7 low: base + (i - 1). "stt"
encoder genHigh: 15 low: 5 "pop" ] ].
statements reverseDo:
[ :stmt | stmt compile: encoder block: inBlock.
encoder genHigh: 15 low: 5 "pop" ].
encoder backUp!
isBlock
^ true!
newBlock "fix"
| ans |
ans <- <22 <58 6> Block>.
ans basicAt: 2 put: argumentCount. "argCount"
ans basicAt: 3 put: temporaryLocation + 1. "argLoc"
ans basicAt: 4 put: 0. "bytePointer"
^ ans!
statements: s temporaryLocation: t argumentCount: ac temporaryCount: tc
statements <- s.
temporaryLocation <- t.
argumentCount <- ac.
temporaryCount <- tc!
}!
{!
BodyNode methods!
compile: encoder block: inBlock
statements reverseDo:
[ :stmt | stmt compile: encoder block: inBlock.
encoder genHigh: 15 low: 5 " pop "].
encoder genHigh: 15 low: 1 " return self "!
statements: s
statements <- s!
}!
{!
Boolean methods!
and: aBlock
^ self ifTrue: aBlock ifFalse: [ false ]!
ifFalse: falseBlock
^ self ifTrue: [] ifFalse: falseBlock!
ifFalse: falseBlock ifTrue: trueBlock
^ self ifTrue: trueBlock
ifFalse: falseBlock!
ifTrue: trueBlock
^ self ifTrue: trueBlock ifFalse: []!
or: aBlock
^ self ifTrue: [ true ] ifFalse: aBlock!
}!
{!
ByteArrayMeta methods!
basicNew: size
^ self primBytes: size!
new: size
^ self primBytes: size!
}!
{!
ByteArray methods!
asByteArray
^ self!
asString | newString i |
newString <- String new: self size.
i <- 0.
self do: [:x | i <- i + 1. newString at: i put: x asCharacter].
^ newString!
basicAt: index
^ <26 self index>!
basicAt: index put: value
^ ((value isMemberOf: Integer) and: [value between: 0 and: 255])
ifTrue: [ <32 self index value > ]
ifFalse: [ value print. smalltalk error:
'assign illegal value to ByteArray']!
logChunk
^ <154 self>!
size: value
^ <22 <59 value> ByteArray>!
}!
{!
CascadeNode methods!
compile: encoder block: inBlock
| left |
head compile: encoder block: inBlock.
left <- list size.
list reverseDo: [ :stmt |
left <- left - 1.
left > 0 ifTrue: [
encoder genHigh: 15 low: 4 " duplicate " ].
stmt compile: encoder block: inBlock.
left > 0 ifTrue: [
encoder genHigh: 15 low: 5 "pop from stack " ] ]!
head: h
head <- h!
list: l
list <- l!
}!
{!
CharMeta methods!
value: aValue
^ self new value: aValue!
}!
{!
Char methods!
< aValue
" can only compare characters to characters "
^ aValue isChar
ifTrue: [ value < aValue asInteger ]
ifFalse: [ smalltalk error: 'char compared to nonchar']!
== aValue
^ aValue isChar
ifTrue: [ value = aValue asInteger ]
ifFalse: [ false ]!
asInteger
^ value!
asString
" make ourselves into a string "
^ ' ' copy yourself; at: 1 put: self; yourself!
digitValue
" return an integer representing our value "
self isDigit ifTrue: [ ^ value - $0 asInteger ].
self isUppercase ifTrue: [ ^ value - $A asInteger + 10 ].
^ smalltalk error: 'illegal conversion, char to digit'!
isAlphaNumeric
" will also accept underscores (by edict of Zak) "
^ ((self isAlphabetic) or: [ self isDigit ]) or: [ value = 95 ]!
isAlphabetic
^ (self isLowercase) or: [ self isUppercase ]!
isBlank
^ value = $ asInteger " blank char "!
isChar
^ true!
isDigit
^ value between: $0 asInteger and: $9 asInteger!
isLowercase
^ value between: $a asInteger and: $z asInteger!
isUppercase
^ value between: $A asInteger and: $Z asInteger!
printString
^ '$', self asString!
value: aValue " private - used for initialization "
value <- aValue!
}!
{!
Class methods!
fileOut | f |
" file out whole class on class.st "
(f <- File name: (name asString,'.st') mode: 'w') open.
self fileOutOn: f.
f close!
fileOutClassOn: aFile | dlm pad buf buf2 |
dlm <- 10 asCharacter asString.
pad <- 9 asCharacter asString.
buf <- superClass isNil ifTrue: [
'nil' ]
ifFalse: [
superClass name asString ].
buf <- buf , dlm , pad.
buf <- buf , 'subclass: ' , self name printString.
buf <- buf , dlm , pad.
buf2 <- ''.
variables notNil ifTrue: [
variables inject: '' into: [ :p :v |
buf2 <- buf2 , p , v.
' ' ] ].
buf <- buf , 'instanceVariableNames: ' , buf2 printString.
aFile putChunk: buf!
fileOutOn: aFile
" file out class description "
self fileOutClassOn: aFile.
self class fileOutMethodsOn: aFile.
self fileOutMethodsOn: aFile!
subClasses
^ classes inject: List new
into: [:x :y | (y superClass == self)
ifTrue: [ x add: y]. x ]!
subclass: aSymbol instanceVariableNames: aString
| newMeta varArray newClass |
newMeta <- Metaclass
metaName: (aSymbol asString , 'Meta') asSymbol
instanceSize: self class instanceSize
methods: Dictionary new
superClass: self class
variables: (Array primOrefs: 0).
varArray <- aString words: [ :x | x isAlphaNumeric ].
newClass <- newMeta
instName: aSymbol
instanceSize: self instanceSize + varArray basicSize
methods: Dictionary new
superClass: self
variables: varArray.
newMeta name assign: newMeta.
aSymbol assign: newClass.
classes at: aSymbol put: newClass.
^ newClass!
}!
{!
Collection methods!
< coll
(coll respondsTo: #includes:)
ifFalse: [ ^ smalltalk error:
'collection compared to non collection'].
self do: [:x | ((self occurrencesOf: x) <
(coll occurrencesOf: x))ifFalse: [ ^ false ]].
coll do: [:x | (self includes: x) ifFalse: [ ^ true ]].
^ false!
= coll
self do: [:x | (self occurrencesOf: x) =
(coll occurrencesOf: x) ifFalse: [ ^ false ] ].
^ true!
asArray | newArray i |
newArray <- Array new: self size.
i <- 0.
self do: [:x | i <- i + 1. newArray at: i put: x].
^ newArray!
asByteArray | newArray i |
newArray <- ByteArray new size: self size.
i <- 0.
self do: [:x | i <- i + 1. newArray at: i put: x].
^ newArray!
asSet
^ Set new addAll: self!
asString
^ self asByteArray asString!
display
self do: [:x | x print ]!
includes: value
self do: [:x | (x = value) ifTrue: [ ^ true ] ].
^ false!
inject: thisValue into: binaryBlock | last |
last <- thisValue.
self do: [:x | last <- binaryBlock value: last value: x].
^ last!
isEmpty
^ self size == 0!
occurrencesOf: anObject
^ self inject: 0
into: [:x :y | (y = anObject)
ifTrue: [x + 1]
ifFalse: [x] ]!
printString
^ ( self inject: self class printString , ' ('
into: [:x :y | x , ' ' , y printString]), ' )'!
size
^ self inject: 0 into: [:x :y | x + 1]!
sort
^ self sort: [:x :y | x < y ]!
sort: aBlock
^ self inject: List new
into: [:x :y | x add: y ordered: aBlock. x]!
}!
{!
ContextMeta methods!
method: aMeth arguments: aVec temporaries: tVec
^ self new method: aMeth arguments: aVec temporaries: tVec!
}!
{!
Context methods!
arguments: a
arguments <- a!
at: key put: value
temporaries at: key put: value!
blockReturn
<18 self>
ifFalse: [ ^ smalltalk error:
'incorrect context for block return']!
copy
^ super copy temporaries: temporaries copy!
method: m
method <- m!
method: aMeth arguments: aVec temporaries: tVec
method <- aMeth.
arguments <- aVec.
temporaries <- tVec!
returnToBlock: bytePtr
" change the location we will return to, to execute a block"
<28 self bytePtr>!
temporaries: t
temporaries <- t!
}!
{!
DictionaryMeta methods!
new
^ self basicNew
hashTable: (Array new: 39)!
}!
{!
Dictionary methods!
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.
^ (link notNil)
ifTrue: [ link at: aKey ifAbsent: exceptionBlock ]
ifFalse: exceptionBlock!
at: aKey put: aValue | hashPosition link |
hashPosition <- self hash: aKey.
((hashTable at: hashPosition + 1) isNil)
ifTrue: [ hashTable at: hashPosition + 1 put: aKey ].
((hashTable at: hashPosition + 1) = aKey)
ifTrue: [ hashTable at: hashPosition + 2 put: aValue ]
ifFalse: [ link <- hashTable at: hashPosition + 3.
(link notNil)
ifTrue: [ link at: aKey put: aValue ]
ifFalse: [ hashTable at: hashPosition + 3
put: (Link key: aKey value: aValue)]]!
basicRemoveKey: aKey | hashPosition link |
hashPosition <- self hash: aKey.
((hashTable at: hashPosition + 1) = aKey)
ifTrue: [ link <- hashTable at: hashPosition + 3.
(link notNil) ifTrue: [
hashTable at: hashPosition + 1 put: link key.
hashTable at: hashPosition + 2 put: link value.
hashTable at: hashPosition + 3 put: link next ]
ifFalse: [
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) ] ]!
binaryDo: aBlock
(1 to: hashTable size by: 3) do:
[:i | (hashTable at: i) notNil
ifTrue: [ aBlock value: (hashTable at: i)
value: (hashTable at: i+1) ].
(hashTable at: i+2) notNil
ifTrue: [ (hashTable at: i+2)
binaryDo: aBlock ] ]!
display
self binaryDo: [:x :y | (x printString , ' -> ',
y printString ) print ]!
hash: aKey
^ 3 * ((aKey hash) rem: ((hashTable size) quo: 3))!
hashTable: hArray
hashTable <- hArray!
includesKey: aKey
" look up, but throw away result "
self at: aKey ifAbsent: [ ^ false ].
^ true!
removeKey: aKey
^ self removeKey: aKey
ifAbsent: [ smalltalk error: 'remove key not found']!
removeKey: aKey ifAbsent: exceptionBlock
^ (self includesKey: aKey)
ifTrue: [ self basicRemoveKey: aKey ]
ifFalse: exceptionBlock!
}!
{!
Encoder methods!
backUp
" back up one instruction "
index <- index - 1!
currentLocation
^ index!
expandByteCodes | newarray size |
size <- byteCodes size.
newarray <- byteCodes size: size + 8. "fix"
(1 to: size) do: [:i | newarray at: i put: (byteCodes at: i)].
byteCodes <- newarray!
genCode: byte
index = 256 ifTrue: [
parser error: 'too many byte codes' ].
index <- index + 1.
(index >= byteCodes size)
ifTrue: [ self expandByteCodes].
byteCodes at: index put: byte.
^ index!
genHigh: high low: low
(low >= 16)
ifTrue: [ self genHigh: 0 low: high. self genCode: low ]
ifFalse: [ self genCode: high * 16 + low ]!
genLiteral: aValue
literals size = 256 ifTrue: [
parser error: 'too many literals' ].
literals <- literals with: aValue.
^ literals size - 1!
hack: loc "fix"
byteCodes at: loc put: index + 1!
hackByteCodes | newarray |
newarray <- byteCodes size: index. "fix"
(1 to: index) do: [:i | newarray at: i put: (byteCodes at: i)].
byteCodes <- newarray!
hackLiterals
literals size = 0 ifTrue: [
literals <- nil ]!
hackMaxStack
maxStack <- 6!
method: maxTemps class: c text: text
| ans |
ans <- Method new.
ans text: text.
ans message: name.
self hackByteCodes.
ans basicAt: 3 put: byteCodes.
self hackLiterals.
ans basicAt: 4 put: literals.
self hackMaxStack.
ans basicAt: 5 put: maxStack.
"self hackMaxTemps."
ans basicAt: 6 put: maxTemps + 1.
ans methodClass: c.
^ ans!
name: n
name <- n asSymbol.
byteCodes <- '' size: 20. "fix"
index <- 0.
literals <- Array new: 0.
stackSize <- 0.
maxStack <- 1.!
parser: aParser
parser <- aParser!
patch: loc
" patch a goto from a block "
byteCodes at: loc put: index!
popArgs: n
stackSize <- stackSize - n.!
pushArgs: n
stackSize <- stackSize + n.
maxStack <- stackSize max: maxStack!
}!
{!
False methods!
ifTrue: trueBlock ifFalse: falseBlock
^ falseBlock value!
not
^ true!
printString
^ 'false'!
xor: aBoolean
^ aBoolean!
}!
{!
FileMeta methods!
name: nStr mode: mStr
^ self new name: nStr mode: mStr!
name: nStr open: mStr
^ self new name: nStr open: mStr!
}!
{!
File methods!
asString | text line |
text <- ''.
[ (line <- self getString) notNil ]
whileTrue: [ text <- text , line ].
^ text!
close
" close file, take entry out of global variable "
number isNil ifTrue: [ ^ nil ].
files at: number put: nil.
<121 number>.
number <- nil.!
delete
('rm ', name) unixCommand!
fileIn | str |
[ (str <- self getChunk) notNil ] whileTrue: [
str = '{' ifTrue: [
self fileInSet ]
ifFalse: [
str execute ] ]!
fileIn: name
self name: name.
self open: 'r'.
self fileIn.
self close.!
fileInSet | str pos cls mth |
(str <- self getChunk) isNil ifTrue: [
self halt ].
str = '}' ifTrue: [
^ self ].
pos <- str indexOf: [ :c | c isBlank ].
cls <- (str copyFrom: 1 to: pos - 1) asSymbol value.
[ (str <- self getChunk) notNil ] whileTrue: [
str = '}' ifTrue: [
^ self ].
(mth <- Parser new parse: str in: cls) notNil ifTrue: [
cls install: mth ] ].
self halt!
getChunk
^ (number notNil)
ifTrue: [<157 number>]!
getNumber
" get a file number - called only by open"
(1 to: 15) do: [:i | (files at: i) isNil
ifTrue: [ files at: i put: self. number <- i. ^ nil]]!
getString
^ (number notNil)
ifTrue: [<125 number>]!
mode: m
mode <- m!
name
^ name!
name: string
name <- string!
name: nStr mode: mStr
name <- nStr.
mode <- mStr!
name: nStr open: mStr
name <- nStr.
mode <- mStr.
self open!
open
number notNil ifTrue: [ self close ].
self getNumber.