1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-11 21:57:38 +03:00
mal/impls/gnu-smalltalk/util.st

91 lines
2.4 KiB
Smalltalk
Raw Permalink Normal View History

2017-07-09 11:51:56 +03:00
SequenceableCollection extend [
2017-07-02 15:41:36 +03:00
asDictionary [
| dict assoc |
dict := Dictionary new.
1 to: self size by: 2 do:
[ :i | dict add: (self at: i) -> (self at: i + 1) ].
^dict
]
]
String extend [
parse [
|text canary|
canary := 8r177 asCharacter asString.
2017-07-02 15:41:36 +03:00
text := self copyFrom: 2 to: self size - 1.
text := text copyReplaceAll: '\\' with: canary.
2017-07-02 15:41:36 +03:00
text := text copyReplaceAll: '\"' with: '"'.
text := text copyReplaceAll: '\n' with: '
'.
text := text copyReplaceAll: canary with: '\'.
2017-07-02 15:41:36 +03:00
^text
]
repr [
|text|
text := self copyReplaceAll: '\' with: '\\'.
text := text copyReplaceAll: '
' with: '\n'.
text := text copyReplaceAll: '"' with: '\"'.
^'"', text, '"'
]
]
2017-07-06 23:54:34 +03:00
BlockClosure extend [
valueWithExit [
^self value: [ ^nil ]
]
]
2017-07-12 00:38:06 +03:00
Object extend [
toMALValue [
self = true ifTrue: [ ^MALObject True ].
self = false ifTrue: [ ^MALObject False ].
self = nil ifTrue: [ ^MALObject Nil ].
self isNumber ifTrue: [ ^MALNumber new: self ].
self isString ifTrue: [ ^MALString new: self ].
self isSymbol ifTrue: [ ^MALSymbol new: self ].
self isArray ifTrue: [
^MALVector new: (self asOrderedCollection collect:
[ :item | item toMALValue ])
].
self isSequenceable ifTrue: [
^MALList new: (self asOrderedCollection collect:
[ :item | item toMALValue ])
].
self class = Dictionary ifTrue: [
| result |
result := Dictionary new.
self keysAndValuesDo: [ :key :value |
result at: key toMALValue put: value toMALValue
].
^MALMap new: result
]
]
]
"NOTE: bugfix version from 3.2.91 for 3.2.4"
Namespace current: Kernel [
MatchingRegexResults extend [
at: anIndex [
<category: 'accessing'>
| reg text |
anIndex = 0 ifTrue: [^self match].
cache isNil ifTrue: [cache := Array new: registers size].
(cache at: anIndex) isNil
ifTrue:
[reg := registers at: anIndex.
text := reg isNil
ifTrue: [nil]
ifFalse: [
reg isEmpty
ifTrue: ['']
ifFalse: [self subject copyFrom: reg first to: reg last]].
cache at: anIndex put: text].
^cache at: anIndex
]
]
2017-07-06 23:54:34 +03:00
]