1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
module State where
import AbstractCurry.Types hiding ( QName )
import FlatCurry.Types
import Data.FiniteMap
import StateMonad
import Translate
type OptState m a = StateT State m a
data VarKind = VarIDSupply
| VarID
deriving Show
type SMap = FM QName [(VarKind, CTVarIName)]
type QMap = FM QName QName
type TList = [(CTypeExpr, CTypeExpr)]
data State = State
{
currentProg :: Prog,
currentCProg :: CurryProg,
currentModule :: String,
currentFunction :: QName,
typeMap :: QMap,
typeSTMap :: QMap,
consMap :: QMap,
funcMap :: QMap,
supplyVarMap :: SMap,
funcTypes :: TList,
maxVar :: VarIndex
}
defaultState :: State
defaultState = State
{ currentProg = Prog "" [] [] [] []
, currentCProg = CurryProg "" [] Nothing [] [] [] [] []
, currentModule = ""
, currentFunction = ("", "")
, typeMap = emptyFM (<)
, typeSTMap = emptyFM (<)
, consMap = emptyFM (<)
, funcMap = emptyFM (<)
, supplyVarMap = emptyFM (<)
, funcTypes = []
, maxVar = 0
}
setCurrentModule :: String -> State -> State
setCurrentModule m state = state { currentModule = m }
setCurrentFunction :: QName -> State -> State
setCurrentFunction qn state = state { currentFunction = qn }
setTypeMap :: QMap -> State -> State
setTypeMap tmap state = state { typeMap = tmap }
setTypeSTMap :: QMap -> State -> State
setTypeSTMap tstmap state = state { typeSTMap = tstmap }
setConsMap :: QMap -> State -> State
setConsMap cmap state = state { consMap = cmap }
setFuncMap :: QMap -> State -> State
setFuncMap funMap state = state { funcMap = funMap }
addToSupplyVarMap :: QName -> (VarKind, CVarIName) -> State -> State
addToSupplyVarMap qn v state = state { supplyVarMap = smap' }
where
smap = supplyVarMap state
smap' = case lookupFM smap qn of
Just _ -> updFM smap qn (v :)
Nothing -> addToFM smap qn [v]
setFuncTypes :: TList -> State -> State
setFuncTypes funTypes state = state { funcTypes = funTypes }
addnfFDs :: [FuncDecl] -> State -> State
addnfFDs fds' state =
let (Prog name imps tds fds opds) = currentProg state
in state { currentProg = Prog name imps tds (fds ++ fds') opds }
setProg :: Prog -> State -> State
setProg p state = state { currentProg = p }
freshVar :: Monad m => OptState m CVarIName
freshVar = do
state <- get
let m = maxVar state
m' = m + 1
put $ state { maxVar = m' }
return (translVar m')
freshVars :: Monad m => Int -> OptState m [CVarIName]
freshVars c = do
state <- get
let m = maxVar state
ms = [m + 1 .. m + c]
put $ state { maxVar = m + c }
return (map translVar ms)
addCurryFDs :: [CFuncDecl] -> State -> State
addCurryFDs fds' state =
let (CurryProg n is dds cds ids tds fds ods) = currentCProg state
in state { currentCProg = CurryProg n is dds cds ids tds (fds ++ fds') ods }
addCurryTDs :: [CTypeDecl] -> State -> State
addCurryTDs tds' state =
let (CurryProg n is dds cds ids tds fds ods) = currentCProg state
in state { currentCProg = CurryProg n is dds cds ids (tds ++ tds') fds ods }
addCurryImports :: [String] -> State -> State
addCurryImports is' state =
let (CurryProg n is dds cds ids tds fds ods) = currentCProg state
in state { currentCProg = CurryProg n (is ++ is') dds cds ids tds fds ods }
addCurryInstances :: [CInstanceDecl] -> State -> State
addCurryInstances ids' state =
let (CurryProg n is dds cds ids tds fds ods) = currentCProg state
in state { currentCProg = CurryProg n is dds cds (ids ++ ids') tds fds ods }
|