packages feed

zwirn-0.2.3.1: app/zwirnmill/Setup.hs

module Setup where

{-
    Setup.hs - setup of the various components of the backend
    Copyright (C) 2023, Martin Gius

    This library is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this library.  If not, see <http://www.gnu.org/licenses/>.
-}

import Config as C
import Data.Maybe (fromMaybe)
import Data.Ratio ((%))
import Zwirn.Doux.Env
import Zwirn.Doux.Types (Stream (..))
import Zwirn.Doux.UI
import Zwirn.Language.Compiler as Compiler
import Zwirn.Language.Macro (defaultMacroMap)
import Prelude hiding (log)

setup :: FullConfig -> (String -> IO ()) -> IO (Environment, Stream)
setup config log = do
  str <- setupStream config
  let initE = getInitialEnv (toCiConfig $ fullConfigCi config) str
  env <- fromMaybe initE <$> checkBoot log (ciConfigBootPath $ fullConfigCi config) initE
  return (env, str)

setupStream :: FullConfig -> IO Stream
setupStream config = startStream (toStream (1 % fromIntegral (C.ciConfigPrecision $ fullConfigCi config)) $ fullConfigStream config)

getInitialEnv :: Compiler.CiConfig -> Stream -> Environment
getInitialEnv config str = Environment (sState str) (playEnvFromStream str) (builtinEnvironmentWithStream str) (Just $ ConfigEnv configPath resetConfig) config defaultMacroMap