1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-14 17:32:00 +03:00
juvix/app/Commands/Dev/Asm/Run.hs

47 lines
1.7 KiB
Haskell
Raw Normal View History

2022-09-29 18:44:55 +03:00
module Commands.Dev.Asm.Run where
import Commands.Base
import Commands.Dev.Asm.Run.Options
import Juvix.Compiler.Asm.Data.InfoTable qualified as Asm
import Juvix.Compiler.Asm.Error qualified as Asm
import Juvix.Compiler.Asm.Extra qualified as Asm
import Juvix.Compiler.Asm.Interpreter qualified as Asm
import Juvix.Compiler.Asm.Pretty qualified as Asm
import Juvix.Compiler.Asm.Transformation.Validate qualified as Asm
2022-09-29 18:44:55 +03:00
import Juvix.Compiler.Asm.Translation.FromSource qualified as Asm
runCommand :: forall r. Members '[Embed IO, App] r => AsmRunOptions -> Sem r ()
runCommand opts = do
s <- embed (readFile file)
case Asm.runParser file s of
2022-09-29 18:44:55 +03:00
Left err -> exitJuvixError (JuvixError err)
Right tab ->
let v = if opts ^. asmRunNoValidate then Nothing else Asm.validate' tab
2022-09-29 18:44:55 +03:00
in case v of
Just err ->
exitJuvixError (JuvixError err)
Nothing ->
case tab ^. Asm.infoMainFunction of
Just sym -> do
r <- doRun tab (Asm.getFunInfo tab sym)
case r of
Left err ->
exitJuvixError (JuvixError err)
2022-12-06 13:33:20 +03:00
Right Asm.ValVoid ->
2022-09-29 18:44:55 +03:00
return ()
Right val -> do
renderStdOut (Asm.ppOut (Asm.defaultOptions tab) val)
embed (putStrLn "")
Nothing ->
exitMsg (ExitFailure 1) "no 'main' function"
where
file :: FilePath
file = opts ^. asmRunInputFile . pathPath
doRun ::
Asm.InfoTable ->
Asm.FunctionInfo ->
Sem r (Either Asm.AsmError Asm.Val)
doRun tab funInfo =
embed $ Asm.catchRunErrorIO (Asm.runCodeIO tab funInfo)