-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds support for a builtin `ByteArray` type and associated functions for constructing a `ByteArray` from a list of bytes and a function to query the size of the `ByteArray`. It is only available in the Anoma backend. In Core / Tree, ByteArray constant is stored using a Haskell ByteString. In Anoma the ByteArray is stored as a cell where the head is the length of the ByteArray and the tail is an integer is an integer formed by concatenating the bytes in the array using little-endian byte ordering. The Nock for constructing a `ByteArray` uses the `length`, `add`, `folder` and `lsh` functions from the Anoma hoon stdlib. See the [code comment](https://github.com/anoma/juvix/blob/fa068a30e749f34b2c78451378869df7622ea9fe/src/Juvix/Compiler/Nockma/StdlibFunction.hs#L37) for more details. Example: ``` module test082; import Stdlib.Prelude open; import Stdlib.Debug.Trace open; builtin bytearray axiom ByteArray : Type; builtin bytearray-from-list-byte axiom mkByteArray : List Byte -> ByteArray; builtin bytearray-size axiom size : ByteArray -> Nat; bs0 : ByteArray := mkByteArray []; bs1 : ByteArray := mkByteArray [0x0; 0x0; 0x0]; bs2 : ByteArray := mkByteArray [0x1; 0x0; 0x0; 0x0]; bs3 : ByteArray := mkByteArray [0x2; 0x1]; bs4 : ByteArray := mkByteArray [0x100]; main : ByteArray := trace (size bs0) >-> trace bs0 >-> trace (size bs1) >-> trace bs1 >-> trace (size bs2) >-> trace bs2 >-> trace (size bs3) >-> trace bs3 >-> trace (size bs4) >-> bs4; ``` Output using `tests/Anoma/Compilation/positive/test082.juvix` ``` $ juvix compile anoma -g test082.juvix $ juvix dev nockma run test082.pretty.nockma 0 [0 0] 3 [3 0] 4 [4 1] 2 [2 258] 1 [1 0] ```
- Loading branch information
1 parent
2b5ece7
commit ce5c2c5
Showing
72 changed files
with
723 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Juvix.Compiler.Builtins.ByteArray where | ||
|
||
import Juvix.Compiler.Builtins.Effect | ||
import Juvix.Compiler.Internal.Extra | ||
import Juvix.Prelude | ||
|
||
registerByteArray :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteArray d = do | ||
unless (isSmallUniverse' (d ^. axiomType)) (error "ByteArray should be in the small universe") | ||
registerBuiltin BuiltinByteArray (d ^. axiomName) | ||
|
||
registerByteArrayFromListByte :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteArrayFromListByte d = do | ||
let loc = getLoc d | ||
byte_ <- getBuiltinName loc BuiltinByte | ||
list_ <- getBuiltinName loc BuiltinList | ||
byteArray <- getBuiltinName loc BuiltinByteArray | ||
unless (d ^. axiomType == (list_ @@ byte_ --> byteArray)) (error "bytearray-from-list-byte has the wrong type") | ||
registerBuiltin BuiltinByteArrayFromListByte (d ^. axiomName) | ||
|
||
registerByteArrayLength :: (Member Builtins r) => AxiomDef -> Sem r () | ||
registerByteArrayLength d = do | ||
let loc = getLoc d | ||
byteArray <- getBuiltinName loc BuiltinByteArray | ||
nat_ <- getBuiltinName loc BuiltinNat | ||
unless (d ^. axiomType == (byteArray --> nat_)) (error "bytearray-length has the wrong type") | ||
registerBuiltin BuiltinByteArrayLength (d ^. axiomName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.