Skip to content

Commit

Permalink
CLI: dev casm from-cairo
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Apr 10, 2024
1 parent cbc2e01 commit aec5378
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/Commands/Dev/Casm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Commands.Dev.Casm where

import Commands.Base
import Commands.Dev.Casm.Compile as Compile
import Commands.Dev.Casm.FromCairo as FromCairo
import Commands.Dev.Casm.Options
import Commands.Dev.Casm.Read as Read
import Commands.Dev.Casm.Run as Run
Expand All @@ -11,3 +12,4 @@ runCommand = \case
Compile opts -> Compile.runCommand opts
Run opts -> Run.runCommand opts
Read opts -> Read.runCommand opts
FromCairo opts -> FromCairo.runCommand opts
24 changes: 24 additions & 0 deletions app/Commands/Dev/Casm/FromCairo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Commands.Dev.Casm.FromCairo where

import Commands.Base
import Commands.Dev.Casm.FromCairo.Options
import Data.Aeson
import Data.ByteString.Lazy qualified as BS
import Juvix.Compiler.Backend.Cairo.Extra.Deserialization qualified as Cairo
import Juvix.Compiler.Casm.Data.Result qualified as Casm
import Juvix.Compiler.Casm.Pretty qualified as Casm
import Juvix.Compiler.Casm.Translation.FromCairo qualified as Casm

runCommand :: forall r. (Members '[EmbedIO, App] r) => CasmFromCairoOptions -> Sem r ()
runCommand opts = do
afile :: Path Abs File <- fromAppPathFile file
bs <- liftIO $ BS.readFile (toFilePath afile)
case decode bs of
Just r -> do
let Casm.Result {..} = Casm.fromCairo (Cairo.deserialize r)
renderStdOut (Casm.ppProgram _resultCode)
Nothing ->
exitMsg (ExitFailure 1) "error reading input file"
where
file :: AppPath File
file = opts ^. casmFromCairoInputFile
15 changes: 15 additions & 0 deletions app/Commands/Dev/Casm/FromCairo/Options.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Commands.Dev.Casm.FromCairo.Options where

import CommonOptions

newtype CasmFromCairoOptions = CasmFromCairoOptions
{ _casmFromCairoInputFile :: AppPath File
}
deriving stock (Data)

makeLenses ''CasmFromCairoOptions

parseCasmFromCairoOptions :: Parser CasmFromCairoOptions
parseCasmFromCairoOptions = do
_casmFromCairoInputFile <- parseInputFile FileExtJson
pure CasmFromCairoOptions {..}
14 changes: 13 additions & 1 deletion app/Commands/Dev/Casm/Options.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Commands.Dev.Casm.Options where

import Commands.Dev.Casm.Compile.Options
import Commands.Dev.Casm.FromCairo.Options
import Commands.Dev.Casm.Read.Options
import Commands.Dev.Casm.Run.Options
import CommonOptions
Expand All @@ -9,6 +10,7 @@ data CasmCommand
= Compile CompileOptions
| Run CasmRunOptions
| Read CasmReadOptions
| FromCairo CasmFromCairoOptions
deriving stock (Data)

parseCasmCommand :: Parser CasmCommand
Expand All @@ -17,7 +19,8 @@ parseCasmCommand =
mconcat
[ commandCompile,
commandRun,
commandRead
commandRead,
commandFromCairo
]
where
commandCompile :: Mod CommandFields CasmCommand
Expand All @@ -29,6 +32,9 @@ parseCasmCommand =
commandRead :: Mod CommandFields CasmCommand
commandRead = command "read" readInfo

commandFromCairo :: Mod CommandFields CasmCommand
commandFromCairo = command "from-cairo" fromCairoInfo

compileInfo :: ParserInfo CasmCommand
compileInfo =
info
Expand All @@ -46,3 +52,9 @@ parseCasmCommand =
info
(Read <$> parseCasmReadOptions)
(progDesc "Parse a CASM file and pretty print it")

fromCairoInfo :: ParserInfo CasmCommand
fromCairoInfo =
info
(FromCairo <$> parseCasmFromCairoOptions)
(progDesc "Disassemble Cairo bytecode into CASM")
4 changes: 2 additions & 2 deletions src/Juvix/Compiler/Backend/Cairo/Extra/Deserialization.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ deserialize Result {..} = go [] (map (fromHexText . unpack) _resultData)
where
fromHexText :: String -> Natural
fromHexText s
| isPrefixOf "0x" s = case readHex s of
| isPrefixOf "0x" s = case readHex (drop 2 s) of
[(n, "")] -> n
_ -> error "error parsing field element"
_ -> error ("error parsing field element: " <> pack s)
| otherwise = error "not a hexadecimal number"

go :: [Element] -> [Natural] -> [Element]
Expand Down

0 comments on commit aec5378

Please sign in to comment.