Skip to content

Commit 97edcf4

Browse files
authored
Use 8-bit encoding of Bytes (#2257)
1 parent 5320c08 commit 97edcf4

File tree

10 files changed

+99
-13
lines changed

10 files changed

+99
-13
lines changed

kore/src/Kore/Builtin/Encoding.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ License : NCSA
66
module Kore.Builtin.Encoding
77
( encode8Bit
88
, decode8Bit
9+
, parse8Bit
910
, parseBase16
1011
, toBase16
1112
) where
@@ -33,6 +34,7 @@ import Data.Word
3334
)
3435
import Text.Megaparsec
3536
( Parsec
37+
, (<?>)
3638
)
3739
import qualified Text.Megaparsec as Parsec
3840

@@ -62,6 +64,23 @@ encode8Bit =
6264
, show int
6365
]
6466

67+
{- | Encode text using an 8-bit encoding.
68+
69+
Each 'Char' in the text is interpreted as a 'Data.Word.Word8'. It is an error if
70+
any character falls outside that representable range.
71+
72+
-}
73+
parse8Bit :: Parsec Void Text ByteString
74+
parse8Bit =
75+
ByteString.pack <$> many parseByte
76+
where
77+
parseByte :: Parsec Void Text Word8
78+
parseByte =
79+
fromIntegral . Char.ord <$> Parsec.satisfy is8Bit <?> "8-bit value"
80+
81+
is8Bit :: Char -> Bool
82+
is8Bit c = c < '\x100'
83+
6584
decode8Bit :: ByteString -> Text
6685
decode8Bit =
6786
ByteString.unpack

kore/src/Kore/Builtin/InternalBytes.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ patternVerifierHook =
149149
patternVerifierWorker external =
150150
case externalChild of
151151
StringLiteral_ literal -> do
152-
bytesValue <- Builtin.parseString Encoding.parseBase16 literal
152+
bytesValue <- Builtin.parseString Encoding.parse8Bit literal
153153
(return . InternalBytesF . Const)
154154
InternalBytes { bytesSort, bytesValue }
155155
_ -> Kore.Error.koreFail "Expected literal string"

kore/src/Kore/Builtin/InternalBytes/InternalBytes.hs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ sort :: Text
6767
sort = "BYTES.Bytes"
6868

6969

70-
-- | Render a 'ByteString' as an internal domain value of the given sort.
71-
-- | The result sort should be hooked to the builtin @Bytes@ sort, but this is
72-
-- | not checked.
73-
-- |
74-
-- | See also: 'sort'.
70+
{- | Render a 'ByteString' as an internal domain value of the given sort.
71+
72+
The result sort should be hooked to the builtin @Bytes@ sort, but this is
73+
not checked.
74+
75+
See also: 'sort'.
76+
-}
7577
asInternal
7678
:: InternalVariable variable
7779
=> Sort -- ^ resulting sort
@@ -80,20 +82,21 @@ asInternal
8082
asInternal bytesSort bytesValue =
8183
TermLike.markSimplified $ mkInternalBytes bytesSort bytesValue
8284

83-
-- | Render a 'Bytes' as a domain value pattern of the given sort.
84-
-- |
85-
-- | The result sort should be hooked to the builtin @Bytes@ sort, but this is
86-
-- | not checked.
87-
-- |
88-
-- | See also: 'sort'.
85+
{- | Render a 'Bytes' as a domain value pattern of the given sort.
86+
87+
The result sort should be hooked to the builtin @Bytes@ sort, but this is
88+
not checked.
89+
90+
See also: 'sort'.
91+
-}
8992
asTermLike
9093
:: InternalVariable variable
9194
=> InternalBytes -- ^ builtin value to render
9295
-> TermLike variable
9396
asTermLike InternalBytes { bytesSort, bytesValue } =
9497
mkDomainValue DomainValue
9598
{ domainValueSort = bytesSort
96-
, domainValueChild = mkStringLiteral $ Encoding.toBase16 bytesValue
99+
, domainValueChild = mkStringLiteral $ Encoding.decode8Bit bytesValue
97100
}
98101

99102
internalize

kore/test/Test/Kore/Builtin/InternalBytes.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,14 @@ test_InternalBytes =
603603
mkDomainValue
604604
$ DomainValue bytesSort
605605
$ mkStringLiteral "00"
606+
expect = Right $ asInternal "00"
607+
actual = verifyPattern (Just bytesSort) unverified
608+
assertEqual "" expect actual
609+
, testCase "\\dv{Bytes{}}(\"\x00\")" $ do
610+
let unverified =
611+
mkDomainValue
612+
$ DomainValue bytesSort
613+
$ mkStringLiteral "\x00"
606614
expect = Right $ asInternal "\x00"
607615
actual = verifyPattern (Just bytesSort) unverified
608616
assertEqual "" expect actual
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
definition.kore

test/regression-c-semantics/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include $(CURDIR)/../include.mk
2+
3+
definition.kore: definition.kore.gz
4+
zcat $< >$@
5+
6+
test-regression-c-semantics.sh.out: definition.kore executable.kore
1.82 MB
Binary file not shown.

test/regression-c-semantics/executable.kore

Lines changed: 46 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
$KORE_PARSER --no-print-definition --no-print-pattern definition.kore --pattern executable.kore --module C

test/regression-c-semantics/test-regression-c-semantics.sh.out.golden

Whitespace-only changes.

0 commit comments

Comments
 (0)