1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/gnu-smalltalk/util.st
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

91 lines
2.4 KiB
Smalltalk

SequenceableCollection extend [
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.
text := self copyFrom: 2 to: self size - 1.
text := text copyReplaceAll: '\\' with: canary.
text := text copyReplaceAll: '\"' with: '"'.
text := text copyReplaceAll: '\n' with: '
'.
text := text copyReplaceAll: canary with: '\'.
^text
]
repr [
|text|
text := self copyReplaceAll: '\' with: '\\'.
text := text copyReplaceAll: '
' with: '\n'.
text := text copyReplaceAll: '"' with: '\"'.
^'"', text, '"'
]
]
BlockClosure extend [
valueWithExit [
^self value: [ ^nil ]
]
]
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
]
]
]