| 1 | -- Compiler Toolkit: compiler state management basics |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 7 November 97 |
| 5 | -- |
| 6 | -- Version $Revision: 1.1.1.1 $ |
| 7 | -- |
| 8 | -- Copyright (C) [1997..1999] Manuel M. T. Chakravarty |
| 9 | -- |
| 10 | -- This file is free software; you can redistribute it and/or modify |
| 11 | -- it under the terms of the GNU General Public License as published by |
| 12 | -- the Free Software Foundation; either version 2 of the License, or |
| 13 | -- (at your option) any later version. |
| 14 | -- |
| 15 | -- This file is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | -- GNU General Public License for more details. |
| 19 | -- |
| 20 | --- DESCRIPTION --------------------------------------------------------------- |
| 21 | -- |
| 22 | -- This module provides basic types and services used to realize the state |
| 23 | -- management of the compiler. |
| 24 | -- |
| 25 | --- DOCU ---------------------------------------------------------------------- |
| 26 | -- |
| 27 | -- language: Haskell 98 |
| 28 | -- |
| 29 | -- * The monad `PreCST' is an instance of `STB' where the base state is fixed. |
| 30 | -- However, the base state itself is parametrized by an extra state |
| 31 | -- component that can be instantiated by the compiler that uses the toolkit |
| 32 | -- (to store information like compiler switches) -- this is the reason for |
| 33 | -- adding the prefix `Pre'. |
| 34 | -- |
| 35 | -- * The module exports the details of the `BaseState' etc as they have to be |
| 36 | -- know by `State'. The latter ensures the necessary abstraction for |
| 37 | -- modules that do not belong to the state management. |
| 38 | -- |
| 39 | -- * Due to this module, the state management modules can share internal |
| 40 | -- information about the data types hidden to the rest of the system. |
| 41 | -- |
| 42 | -- * The following state components are maintained: |
| 43 | -- |
| 44 | -- + idBS (triple of strings) -- version, copyright, and disclaimer |
| 45 | -- + errorsBS (type `ErrorState') -- keeps track of raised errors |
| 46 | -- + namesBS (type `NameSupply') -- provides unique names |
| 47 | -- + extraBS (generic type) -- extra compiler-dependent state |
| 48 | -- information, e.g., for compiler |
| 49 | -- switches |
| 50 | -- |
| 51 | --- TODO ---------------------------------------------------------------------- |
| 52 | -- |
| 53 | |
| 54 | module StateBase (PreCST(..), ErrorState(..), BaseState(..), |
| 55 | nop, yield, (+>=), (+>), fixCST, |
| 56 | unpackCST, readCST, writeCST, transCST, liftIO) |
| 57 | where |
| 58 | |
| 59 | import Control.Applicative (Applicative(..)) |
| 60 | import Control.Monad (liftM, ap) |
| 61 | import Control.Monad.Fail (MonadFail (..)) |
| 62 | |
| 63 | import Position (Position) |
| 64 | import UNames (NameSupply) |
| 65 | import StateTrans (STB, |
| 66 | fixSTB, readGeneric, writeGeneric, transGeneric, readBase, |
| 67 | transBase) |
| 68 | import qualified |
| 69 | StateTrans (liftIO) |
| 70 | import Errors (ErrorLvl(..), Error) |
| 71 | |
| 72 | |
| 73 | infixr 1 +>=, +> |
| 74 | |
| 75 | |
| 76 | -- state used in the whole compiler |
| 77 | -- -------------------------------- |
| 78 | |
| 79 | -- form of the error state |
| 80 | -- |
| 81 | -- * when no error was raised yet, the error level is the lowest possible one |
| 82 | -- |
| 83 | data ErrorState = ErrorState ErrorLvl -- worst error level that was raised |
| 84 | Int -- number of errors (excl warnings) |
| 85 | [Error] -- already raised errors |
| 86 | |
| 87 | -- base state (EXPORTED) |
| 88 | -- |
| 89 | data BaseState e = BaseState { |
| 90 | idTKBS :: (String, String, String), -- toolkit id |
| 91 | idBS :: (String, String, String), -- compiler id |
| 92 | errorsBS :: ErrorState, |
| 93 | suppliesBS :: [NameSupply], |
| 94 | extraBS :: e -- extra state |
| 95 | } |
| 96 | |
| 97 | -- the compiler state transformer (EXPORTED) |
| 98 | -- |
| 99 | |
| 100 | newtype PreCST e s a = CST (STB (BaseState e) s a) |
| 101 | |
| 102 | instance Functor (PreCST e s) where |
| 103 | fmap = liftM |
| 104 | |
| 105 | instance Applicative (PreCST e s) where |
| 106 | pure = yield |
| 107 | (<*>) = ap |
| 108 | (*>) = (+>) |
| 109 | |
| 110 | instance Monad (PreCST e s) where |
| 111 | return = pure |
| 112 | (>>=) = (+>=) |
| 113 | (>>) = (*>) |
| 114 | |
| 115 | instance MonadFail (PreCST e s) where |
| 116 | fail = error |
| 117 | |
| 118 | -- unwrapper coercion function (EXPORTED) |
| 119 | -- |
| 120 | unpackCST :: PreCST e s a -> STB (BaseState e) s a |
| 121 | unpackCST m = let CST m' = m in m' |
| 122 | |
| 123 | |
| 124 | -- monad operations |
| 125 | -- ---------------- |
| 126 | |
| 127 | -- the monad's unit |
| 128 | -- |
| 129 | yield :: a -> PreCST e s a |
| 130 | yield a = CST $ return a |
| 131 | |
| 132 | -- the monad's bind |
| 133 | -- |
| 134 | (+>=) :: PreCST e s a -> (a -> PreCST e s b) -> PreCST e s b |
| 135 | m +>= k = CST $ unpackCST m >>= (\a -> unpackCST (k a)) |
| 136 | |
| 137 | -- bind dropping the result of the first state transformer |
| 138 | -- |
| 139 | (+>) :: PreCST e s a -> PreCST e s b -> PreCST e s b |
| 140 | k +> m = k +>= const m |
| 141 | |
| 142 | -- unit with no result |
| 143 | -- |
| 144 | nop :: PreCST e s () |
| 145 | nop = yield () |
| 146 | |
| 147 | -- fixpoint combinator in the monad (EXPORTED) |
| 148 | -- |
| 149 | fixCST :: (a -> PreCST e s a) -> PreCST e s a |
| 150 | fixCST m = CST $ fixSTB (unpackCST . m) |
| 151 | |
| 152 | |
| 153 | -- generic state manipulation |
| 154 | -- -------------------------- |
| 155 | |
| 156 | -- given a reader function for the state, wrap it into an CST monad (EXPORTED) |
| 157 | -- |
| 158 | readCST :: (s -> a) -> PreCST e s a |
| 159 | readCST f = CST $ readGeneric f |
| 160 | |
| 161 | -- given a new state, inject it into an CST monad (EXPORTED) |
| 162 | -- |
| 163 | writeCST :: s -> PreCST e s () |
| 164 | writeCST s' = CST $ writeGeneric s' |
| 165 | |
| 166 | -- given a transformer function for the state, wrap it into an CST monad |
| 167 | -- (EXPORTED) |
| 168 | -- |
| 169 | transCST :: (s -> (s, a)) -> PreCST e s a |
| 170 | transCST f = CST $ transGeneric f |
| 171 | |
| 172 | -- interaction with the encapsulated `IO' monad |
| 173 | -- -------------------------------------------- |
| 174 | |
| 175 | -- lifts an `IO' state transformer into `CST' |
| 176 | -- |
| 177 | liftIO :: IO a -> PreCST e s a |
| 178 | liftIO m = CST $ (StateTrans.liftIO m) |