Skip to content

Use 8-bit encoding of Bytes #2257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions kore/src/Kore/Builtin/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ License : NCSA
module Kore.Builtin.Encoding
( encode8Bit
, decode8Bit
, parse8Bit
, parseBase16
, toBase16
) where
Expand Down Expand Up @@ -33,6 +34,7 @@ import Data.Word
)
import Text.Megaparsec
( Parsec
, (<?>)
)
import qualified Text.Megaparsec as Parsec

Expand Down Expand Up @@ -62,6 +64,23 @@ encode8Bit =
, show int
]

{- | Encode text using an 8-bit encoding.

Each 'Char' in the text is interpreted as a 'Data.Word.Word8'. It is an error if
any character falls outside that representable range.

-}
parse8Bit :: Parsec Void Text ByteString
parse8Bit =
ByteString.pack <$> many parseByte
where
parseByte :: Parsec Void Text Word8
parseByte =
fromIntegral . Char.ord <$> Parsec.satisfy is8Bit <?> "8-bit value"

is8Bit :: Char -> Bool
is8Bit c = c < '\x100'

decode8Bit :: ByteString -> Text
decode8Bit =
ByteString.unpack
Expand Down
2 changes: 1 addition & 1 deletion kore/src/Kore/Builtin/InternalBytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ patternVerifierHook =
patternVerifierWorker external =
case externalChild of
StringLiteral_ literal -> do
bytesValue <- Builtin.parseString Encoding.parseBase16 literal
bytesValue <- Builtin.parseString Encoding.parse8Bit literal
(return . InternalBytesF . Const)
InternalBytes { bytesSort, bytesValue }
_ -> Kore.Error.koreFail "Expected literal string"
Expand Down
27 changes: 15 additions & 12 deletions kore/src/Kore/Builtin/InternalBytes/InternalBytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ sort :: Text
sort = "BYTES.Bytes"


-- | Render a 'ByteString' as an internal domain value of the given sort.
-- | The result sort should be hooked to the builtin @Bytes@ sort, but this is
-- | not checked.
-- |
-- | See also: 'sort'.
{- | Render a 'ByteString' as an internal domain value of the given sort.

The result sort should be hooked to the builtin @Bytes@ sort, but this is
not checked.

See also: 'sort'.
-}
asInternal
:: InternalVariable variable
=> Sort -- ^ resulting sort
Expand All @@ -80,20 +82,21 @@ asInternal
asInternal bytesSort bytesValue =
TermLike.markSimplified $ mkInternalBytes bytesSort bytesValue

-- | Render a 'Bytes' as a domain value pattern of the given sort.
-- |
-- | The result sort should be hooked to the builtin @Bytes@ sort, but this is
-- | not checked.
-- |
-- | See also: 'sort'.
{- | Render a 'Bytes' as a domain value pattern of the given sort.

The result sort should be hooked to the builtin @Bytes@ sort, but this is
not checked.

See also: 'sort'.
-}
asTermLike
:: InternalVariable variable
=> InternalBytes -- ^ builtin value to render
-> TermLike variable
asTermLike InternalBytes { bytesSort, bytesValue } =
mkDomainValue DomainValue
{ domainValueSort = bytesSort
, domainValueChild = mkStringLiteral $ Encoding.toBase16 bytesValue
, domainValueChild = mkStringLiteral $ Encoding.decode8Bit bytesValue
}

internalize
Expand Down
8 changes: 8 additions & 0 deletions kore/test/Test/Kore/Builtin/InternalBytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ test_InternalBytes =
mkDomainValue
$ DomainValue bytesSort
$ mkStringLiteral "00"
expect = Right $ asInternal "00"
actual = verifyPattern (Just bytesSort) unverified
assertEqual "" expect actual
, testCase "\\dv{Bytes{}}(\"\x00\")" $ do
let unverified =
mkDomainValue
$ DomainValue bytesSort
$ mkStringLiteral "\x00"
expect = Right $ asInternal "\x00"
actual = verifyPattern (Just bytesSort) unverified
assertEqual "" expect actual
Expand Down
1 change: 1 addition & 0 deletions test/regression-c-semantics/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition.kore
6 changes: 6 additions & 0 deletions test/regression-c-semantics/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include $(CURDIR)/../include.mk

definition.kore: definition.kore.gz
zcat $< >$@

test-regression-c-semantics.sh.out: definition.kore executable.kore
Binary file added test/regression-c-semantics/definition.kore.gz
Binary file not shown.
46 changes: 46 additions & 0 deletions test/regression-c-semantics/executable.kore

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions test/regression-c-semantics/test-regression-c-semantics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

$KORE_PARSER --no-print-definition --no-print-pattern definition.kore --pattern executable.kore --module C
Empty file.