-
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.
Add
dev nockma encode
command (#3135)
- New command `juvix dev nockma encode --help` ``` Usage: juvix dev nockma encode --to ENCODING --from ENCODING Encode and decode nockma terms Available options: --to ENCODING Choose the source encoding. • base64: Jam and Base 64 encoding • bytes: Jam encoding • debug: Nockma code with annotations • text: Nockma code without annotations --from ENCODING Choose the target encoding. • base64: Jam and Base 64 encoding • bytes: Jam encoding • debug: Nockma code with annotations • text: Nockma code without annotations ```
- Loading branch information
1 parent
0961d87
commit bf09ee2
Showing
8 changed files
with
144 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Commands.Dev.Nockma.Encode where | ||
|
||
import Commands.Base | ||
import Commands.Dev.Nockma.Encode.Options | ||
import Data.ByteString qualified as B | ||
import Juvix.Compiler.Nockma.Encoding | ||
import Juvix.Compiler.Nockma.Language (Term) | ||
import Juvix.Compiler.Nockma.Pretty qualified as Nockma | ||
import Juvix.Compiler.Nockma.Translation.FromSource.Base | ||
|
||
runCommand :: forall r. (Members AppEffects r) => NockmaEncodeOptions -> Sem r () | ||
runCommand opts = runSimpleErrorIO $ do | ||
from :: Term Natural <- case opts ^. nockmaEncodeFrom of | ||
EncodeBytes -> do | ||
bs <- liftIO B.getContents | ||
decodeCue bs | ||
EncodeBase64 -> do | ||
bs <- getContents | ||
decodeCue64 bs | ||
EncodeText -> fromTextEncoding | ||
EncodeDebug -> fromTextEncoding | ||
case opts ^. nockmaEncodeTo of | ||
EncodeBytes -> do | ||
renderStdOutRaw (jamToByteString from) | ||
EncodeBase64 -> renderStdOut (encodeJam64 from) | ||
EncodeText -> renderStdOut (Nockma.ppSerialize from) | ||
EncodeDebug -> renderStdOut (Nockma.ppPrint from) | ||
where | ||
fromTextEncoding :: (Members '[App, EmbedIO] r') => Sem r' (Term Natural) | ||
fromTextEncoding = do | ||
bs <- getContents | ||
getRight (parseText bs) |
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,72 @@ | ||
module Commands.Dev.Nockma.Encode.Options where | ||
|
||
import CommonOptions | ||
import Prelude (show) | ||
|
||
data EncodeType | ||
= EncodeBase64 | ||
| EncodeBytes | ||
| EncodeDebug | ||
| EncodeText | ||
deriving stock (Eq, Enum, Bounded, Ord, Data) | ||
|
||
instance Show EncodeType where | ||
show = \case | ||
EncodeBase64 -> "base64" | ||
EncodeBytes -> "bytes" | ||
EncodeDebug -> "debug" | ||
EncodeText -> "text" | ||
|
||
instance Pretty EncodeType where | ||
pretty = pretty . Prelude.show | ||
|
||
data NockmaEncodeOptions = NockmaEncodeOptions | ||
{ _nockmaEncodeFrom :: EncodeType, | ||
_nockmaEncodeTo :: EncodeType | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''NockmaEncodeOptions | ||
|
||
base64Help :: AnsiDoc | ||
base64Help = "Jam and Base 64 encoding" | ||
|
||
bytesHelp :: AnsiDoc | ||
bytesHelp = "Jam encoding" | ||
|
||
encodingHelp :: Doc AnsiStyle | ||
encodingHelp = | ||
enumHelp | ||
( \case | ||
EncodeBase64 -> base64Help | ||
EncodeBytes -> bytesHelp | ||
EncodeDebug -> "Nockma code with annotations" | ||
EncodeText -> "Nockma code without annotations" | ||
) | ||
|
||
parseNockmaEncodeOptions :: Parser NockmaEncodeOptions | ||
parseNockmaEncodeOptions = do | ||
_nockmaEncodeFrom <- | ||
option | ||
(enumReader Proxy) | ||
( long "to" | ||
<> metavar "ENCODING" | ||
<> completer (enumCompleter @EncodeType Proxy) | ||
<> helpDoc | ||
( "Choose the source encoding.\n" | ||
<> encodingHelp | ||
) | ||
) | ||
|
||
_nockmaEncodeTo <- | ||
option | ||
(enumReader Proxy) | ||
( long "from" | ||
<> metavar "ENCODING" | ||
<> completer (enumCompleter @EncodeType Proxy) | ||
<> helpDoc | ||
( "Choose the target encoding.\n" | ||
<> encodingHelp | ||
) | ||
) | ||
pure NockmaEncodeOptions {..} |
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