-- | Compilation runner
module Language.Giml.Compiler.Compile where

import Control.Monad.Except
import Data.Text qualified as T
import Data.Text.IO qualified as T
import Language.Backend.JS qualified as JS
import Language.Giml qualified as Giml
import Language.Giml.Compiler.Translate

-- | Compile a Giml source file to JS
compile
  :: (Giml.MonadBase b b)
  => Giml.LogAction b Giml.LogMsg
  -- ^ logging
  -> FilePath
  -- ^ Name of the source file
  -> T.Text
  -- ^ Content of the source file
  -> b (Either T.Text T.Text)
compile :: forall (b :: * -> *).
MonadBase b b =>
LogAction b LogMsg -> FilePath -> Text -> b (Either Text Text)
compile LogAction b LogMsg
logact FilePath
file Text
src = ExceptT Text b Text -> b (Either Text Text)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT Text b Text -> b (Either Text Text))
-> ExceptT Text b Text -> b (Either Text Text)
forall a b. (a -> b) -> a -> b
$ do
  File Ann
ast <- LogAction b LogMsg -> FilePath -> Text -> ExceptT Text b (File Ann)
forall (b :: * -> *).
MonadBase b b =>
LogAction b LogMsg -> FilePath -> Text -> ExceptT Text b (File Ann)
Giml.parseInferPipeline LogAction b LogMsg
logact FilePath
file Text
src
  Text -> ExceptT Text b Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> ExceptT Text b Text) -> Text -> ExceptT Text b Text
forall a b. (a -> b) -> a -> b
$
    ( (File -> Doc Any) -> File -> Text
forall a ann. (a -> Doc ann) -> a -> Text
JS.pp File -> Doc Any
forall ann. File -> Doc ann
JS.ppFile
        (File -> Text) -> (File Ann -> File) -> File Ann -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (File Ann -> StateT TranState (Reader Builtins) File)
-> Builtins -> File Ann -> File
forall a b.
(a -> StateT TranState (Reader Builtins) b) -> Builtins -> a -> b
translate File Ann -> StateT TranState (Reader Builtins) File
forall (m :: * -> *). Translate m => File Ann -> m File
translateFile Builtins
Giml.builtins
    )
      File Ann
ast

translate' :: Giml.File Giml.Ann -> T.Text
translate' :: File Ann -> Text
translate' = (File -> Doc Any) -> File -> Text
forall a ann. (a -> Doc ann) -> a -> Text
JS.pp File -> Doc Any
forall ann. File -> Doc ann
JS.ppFile (File -> Text) -> (File Ann -> File) -> File Ann -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (File Ann -> StateT TranState (Reader Builtins) File)
-> Builtins -> File Ann -> File
forall a b.
(a -> StateT TranState (Reader Builtins) b) -> Builtins -> a -> b
translate File Ann -> StateT TranState (Reader Builtins) File
forall (m :: * -> *). Translate m => File Ann -> m File
translateFile Builtins
Giml.builtins

translate'IO :: Giml.File Giml.Ann -> IO ()
translate'IO :: File Ann -> IO ()
translate'IO = Text -> IO ()
T.putStrLn (Text -> IO ()) -> (File Ann -> Text) -> File Ann -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. File Ann -> Text
translate'