2023-01-09 20:21:30 +03:00
|
|
|
module AsmInterpreter where
|
|
|
|
|
|
|
|
import App
|
|
|
|
import CommonOptions
|
|
|
|
import Juvix.Compiler.Asm.Data.InfoTable 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
|
|
|
|
|
2024-02-13 21:00:01 +03:00
|
|
|
runAsm :: forall r. (Members '[EmbedIO, App] r) => Bool -> Asm.InfoTable -> Sem r ()
|
2023-01-09 20:21:30 +03:00
|
|
|
runAsm bValidate tab =
|
|
|
|
let v = if bValidate then Asm.validate' tab else Nothing
|
|
|
|
in case v of
|
|
|
|
Just err ->
|
|
|
|
exitJuvixError (JuvixError err)
|
|
|
|
Nothing ->
|
|
|
|
case tab ^. Asm.infoMainFunction of
|
|
|
|
Just sym -> do
|
2023-04-04 19:58:05 +03:00
|
|
|
r <- doRun tab (Asm.lookupFunInfo tab sym)
|
2023-01-09 20:21:30 +03:00
|
|
|
case r of
|
|
|
|
Left err ->
|
|
|
|
exitJuvixError (JuvixError err)
|
|
|
|
Right Asm.ValVoid ->
|
|
|
|
return ()
|
|
|
|
Right val -> do
|
|
|
|
renderStdOut (Asm.ppOut (Asm.defaultOptions tab) val)
|
2024-01-16 19:22:10 +03:00
|
|
|
putStrLn ""
|
2023-01-09 20:21:30 +03:00
|
|
|
Nothing ->
|
|
|
|
exitMsg (ExitFailure 1) "no 'main' function"
|
|
|
|
where
|
|
|
|
doRun ::
|
|
|
|
Asm.InfoTable ->
|
|
|
|
Asm.FunctionInfo ->
|
|
|
|
Sem r (Either Asm.AsmError Asm.Val)
|
|
|
|
doRun tab' funInfo =
|
|
|
|
embed $ Asm.catchRunErrorIO (Asm.runCodeIO tab' funInfo)
|