{- ORMOLU_DISABLE -}
-- Implicit CAD. Copyright (C) 2011, Christopher Olah (chris@colah.ca)
-- Copyright 2016, Julia Longtin (julial@turinglace.com)
-- Released under the GNU AGPLV3+, see LICENSE

module Graphics.Implicit.ExtOpenScad.Util.StateC (addMessage, getVarLookup, modifyVarLookup, lookupVar, pushVals, getVals, putVals, withPathShiftedBy, getPath, getRelPath, errorC, warnC, scadOptions) where

import Prelude(FilePath, Maybe, ($), (<>), pure)

import Graphics.Implicit.ExtOpenScad.Definitions(VarLookup(VarLookup), OVal, Symbol, SourcePosition, Message(Message), MessageType(Error, Warning), ScadOpts, StateC, CompState(scadVars, oVals, sourceDir))

import Data.Map (lookup)

import Data.Text.Lazy (Text)

import Control.Monad.State (modify, gets)

import System.FilePath((</>))
import Control.Monad.Writer (tell)
import Control.Monad.Reader.Class (ask)

getVarLookup :: StateC VarLookup
getVarLookup :: StateC VarLookup
getVarLookup = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets CompState -> VarLookup
scadVars

modifyVarLookup :: (VarLookup -> VarLookup) -> StateC ()
modifyVarLookup :: (VarLookup -> VarLookup) -> StateC ()
modifyVarLookup VarLookup -> VarLookup
f = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \CompState
c -> CompState
c { scadVars :: VarLookup
scadVars = VarLookup -> VarLookup
f forall a b. (a -> b) -> a -> b
$ CompState -> VarLookup
scadVars CompState
c }

-- | Perform a variable lookup
--   FIXME: generate a warning when we look up a variable that is not present.
lookupVar :: Symbol -> StateC (Maybe OVal)
lookupVar :: Symbol -> StateC (Maybe OVal)
lookupVar Symbol
name = do
    (VarLookup Map Symbol OVal
varlookup) <- StateC VarLookup
getVarLookup
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
lookup Symbol
name Map Symbol OVal
varlookup

pushVals :: [OVal] -> StateC ()
pushVals :: [OVal] -> StateC ()
pushVals [OVal]
vals = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \CompState
c -> CompState
c { oVals :: [OVal]
oVals = [OVal]
vals forall a. Semigroup a => a -> a -> a
<> CompState -> [OVal]
oVals CompState
c }

getVals :: StateC [OVal]
getVals :: StateC [OVal]
getVals = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets CompState -> [OVal]
oVals

putVals :: [OVal] -> StateC ()
putVals :: [OVal] -> StateC ()
putVals [OVal]
vals = forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \CompState
c -> CompState
c { oVals :: [OVal]
oVals = [OVal]
vals }

withPathShiftedBy :: FilePath -> StateC a -> StateC a
withPathShiftedBy :: forall a. FilePath -> StateC a -> StateC a
withPathShiftedBy FilePath
pathShift StateC a
s = do
  FilePath
path <- StateC FilePath
getPath
  forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \CompState
c -> CompState
c { sourceDir :: FilePath
sourceDir = FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
pathShift }
  a
x <- StateC a
s
  forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \CompState
c -> CompState
c { sourceDir :: FilePath
sourceDir = FilePath
path }
  forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

-- | Pure the path stored in the state.
getPath :: StateC FilePath
getPath :: StateC FilePath
getPath = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets CompState -> FilePath
sourceDir

getRelPath :: FilePath -> StateC FilePath
getRelPath :: FilePath -> StateC FilePath
getRelPath FilePath
relPath = do
    FilePath
path <- StateC FilePath
getPath
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
relPath

-- Add a single message to the list of messages being returned
addMesg :: Message -> StateC ()
addMesg :: Message -> StateC ()
addMesg Message
m = forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [Message
m]

addMessage :: MessageType -> SourcePosition -> Text -> StateC ()
addMessage :: MessageType -> SourcePosition -> Text -> StateC ()
addMessage MessageType
mtype SourcePosition
pos Text
text = Message -> StateC ()
addMesg forall a b. (a -> b) -> a -> b
$ MessageType -> SourcePosition -> Text -> Message
Message MessageType
mtype SourcePosition
pos Text
text

errorC :: SourcePosition -> Text -> StateC ()
errorC :: SourcePosition -> Text -> StateC ()
errorC = MessageType -> SourcePosition -> Text -> StateC ()
addMessage MessageType
Error
{-# INLINABLE errorC #-}

warnC :: SourcePosition -> Text -> StateC ()
warnC :: SourcePosition -> Text -> StateC ()
warnC = MessageType -> SourcePosition -> Text -> StateC ()
addMessage MessageType
Warning
{-# INLINABLE warnC #-}

-- Get the ScadOpts from the Reader in ImplicitCadM
scadOptions :: StateC ScadOpts
scadOptions :: StateC ScadOpts
scadOptions = forall r (m :: * -> *). MonadReader r m => m r
ask