1
1
mirror of https://github.com/anoma/juvix.git synced 2025-01-07 08:08:44 +03:00
juvix/app/Commands/Dev/Asm/Run.hs

48 lines
1.8 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
2022-12-20 15:05:40 +03:00
afile :: Path Abs File <- someBaseToAbs' file
s <- embed (readFile (toFilePath afile))
case Asm.runParser (toFilePath afile) 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
2022-12-20 15:05:40 +03:00
file :: SomeBase File
2022-09-29 18:44:55 +03:00
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)