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
|
2022-11-08 15:42:40 +03:00
|
|
|
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)
|
2022-10-19 17:55:16 +03:00
|
|
|
case Asm.runParser file s of
|
2022-09-29 18:44:55 +03:00
|
|
|
Left err -> exitJuvixError (JuvixError err)
|
|
|
|
Right tab ->
|
2022-11-08 15:42:40 +03:00
|
|
|
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)
|