Skip to content

Commit

Permalink
[PreSmalltalks] Implement LH "Unsafe Symbol" encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
shingarov committed Nov 12, 2024
1 parent db9b370 commit 0fc6d16
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ BaselineOfMachineArithmetic >> baseline: spec [
package:#'PreSmalltalks-Applicative';
package:#'PreSmalltalks-Substitutions';
package:#'PreSmalltalks-ZEncoding';
package:#'PreSmalltalks-FxEncoding';
package: #'PreSmalltalks' with:
[spec requires: 'PreSmalltalks-Pharo';
requires: 'PreSmalltalks-Applicative';
requires: 'PreSmalltalks-Substitutions';
requires: 'PreSmalltalks-ZEncoding'
requires: 'PreSmalltalks-ZEncoding';
requires: 'PreSmalltalks-FxEncoding'
];
package: #'PreSmalltalks-Parser' with:
[spec requires: 'PreSmalltalks';
Expand Down
18 changes: 18 additions & 0 deletions src/PreSmalltalks-FxEncoding/Character.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Extension { #name : #Character }

{ #category : #'*PreSmalltalks-FxEncoding' }
Character >> isUnsafeChar [
"
isUnsafeChar :: Char -> Bool
Cf. Names.hs
"
^(Character okSymChars includes: self) not
]

{ #category : #'*PreSmalltalks-FxEncoding' }
Character class >> okSymChars [
^($a to: $z),
($A to: $Z),
($0 to: $9),
{$_}
]
43 changes: 43 additions & 0 deletions src/PreSmalltalks-FxEncoding/String.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Extension { #name : #String }

{ #category : #'*PreSmalltalks-FxEncoding' }
String >> decodeUnsafe [
| in out |
in := ReadStream on: self.
out := WriteStream on: String new.
[ in atEnd ] whileFalse: [ | c |
c := in next.
c =
ifTrue: [ | codePoint |
codePoint := Integer readFrom: (in upTo: $ß).
out nextPut: (Character codePoint: codePoint)
] ifFalse: [
out nextPut: c
]
].
^out contents
"
'vß35ß3ß35ßß35ß1' decodeUnsafe → 'v#3##1'
"
]

{ #category : #'*PreSmalltalks-FxEncoding' }
String >> encodeUnsafe [
"
encodeUnsafe :: T.Text -> T.Text
Cf. Names.hs
In Smalltalk, we want to satisfy stricter requirements:
we want our symbols to be valid Z3 symbols (even after passing
through FFI) AND be valid Smalltalk names.
Therefore '$' is not a safe character; but 'ß' is.
"
| c cs head tail |
self isEmpty ifTrue: [ ^ self ].
c := self first.
cs := self allButFirst.
head := c isUnsafeChar
ifTrue: [ 'ß' , c codePoint printString , 'ß' ]
ifFalse: [ String with: c ].
tail := cs encodeUnsafe.
^ head , tail
]
1 change: 1 addition & 0 deletions src/PreSmalltalks-FxEncoding/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'PreSmalltalks-FxEncoding' }

0 comments on commit 0fc6d16

Please sign in to comment.