3
import Position(noPosition)
4
import Id(Id, UId, getIdPosition, addId, remId, SymTab, rangeST, initST, lookupST,toId)
7
import PPrint(ppReadable)
9
type CITrans = (SymTab, -- free + case-bound vars
11
[UId], -- case-bound vars
12
Maybe -- if in the rhs ... (not quite.) ..
13
(UId, -- current const being defined.
14
[UId])) -- its tel vars + lambda vars after it
15
-- i.e, those vars whose values are to be printed
16
-- together with the const. IN TEL ORDER.
19
freeVarScope :: CITrans -> [UId]
20
freeVarScope (vst,cst,cb,_) = [x | x <- rangeST vst, x `notElem` cb]
23
varScope :: CITrans -> [UId]
24
varScope (vst,cst,cb,_) = rangeST vst
27
cstScope :: CITrans -> [UId]
28
cstScope (vst,cst,cb,_) = rangeST cst
30
caseVarScope :: CITrans -> [UId]
31
caseVarScope (vst,cst,cb,_) = cb
33
currentConstPV :: CITrans -> Maybe (UId,[UId])
34
currentConstPV (_,_,_,cpvs) = cpvs
37
scope :: CITrans -> [UId]
38
scope (vst,cst,cb,_) = rangeST vst ++ rangeST cst
40
addVar :: Id -> UId -> CITrans -> CITrans
41
addVar x x' (vst,cst,cb,cpvs) = let vst' = addId x x' vst
43
in (vst',cst',cb,cpvs)
45
addCst :: Id -> UId -> CITrans -> CITrans
46
addCst c c' (vst,cst,cb,cpvs) = let cst' = addId c c' cst
48
in (vst',cst',cb,cpvs)
50
addCsts :: [(Id,UId)] -> CITrans -> CITrans
51
addCsts ccs cit = foldr (uncurry addCst) cit ccs
55
addCaseVar :: UId -> CITrans -> CITrans
56
addCaseVar x (vst,cst,cb,cpvs) = (vst,cst,x:cb,cpvs)
58
updateCCPV :: UId -> [UId] -> CITrans -> CITrans
59
updateCCPV cc pvs (vst,cst,cb,_) = (vst,cst,cb, Just (cc,pvs))
61
addPV :: [UId] -> CITrans -> CITrans
62
addPV xs (vst,cst,cb, Just (cc,pvs)) = (vst,cst,cb, Just (cc,pvs++xs))
63
addPV xs cit@(_,_,_,Nothing) = cit
64
-- this happens when Clam is in the lhs.
66
lookupId :: CITrans -> Id -> Error (Either UId UId)
67
lookupId (vst,cst,cb,_) i = case lookupST vst i of
68
Just x -> return (Left x)
69
Nothing -> case lookupST cst i of
70
Just c -> return (Right c)
71
Nothing -> raise (scopeError i)
73
lookupVar :: CITrans -> Id -> Error UId
74
lookupVar (vst,cst,_,_) x = liftMaybeE (lookupST vst x) (scopeError x)
76
lookupCst :: CITrans -> Id -> Error UId
77
lookupCst (vst,cst,_,_) c = liftMaybeE (lookupST cst c) (scopeError c)
81
scopeError :: Id -> EMsg
82
scopeError i = eMsg (getIdPosition i) (EUnbound (ppReadable i))
85
getCstSymTab :: CITrans -> SymTab
86
getCstSymTab (_,cst,_,_) = cst
88
getVarSymTab :: CITrans -> SymTab
89
getVarSymTab (vst,_,_,_) = vst
91
getCaseBoundVar :: CITrans -> [UId]
92
getCaseBoundVar (_,_,cb,_) = cb
95
initCIT = (initST,initST,[],dummyCCPV "initCIT:")
97
initCIT_CST :: SymTab -> CITrans
98
initCIT_CST st = (initST,st,[],dummyCCPV "initCIT:")
102
dummyCCPV::String -> Maybe (UId,[UId])
103
dummyCCPV s = Nothing
106
inScopeVar :: CITrans -> UId -> Bool
107
inScopeVar (vst,_,_,_) x = maybe False (x==) (lookupST vst (toId x))
110
inScopeCst :: CITrans -> UId -> Bool
111
inScopeCst (_,cst,_,_) x = maybe False (x==) (lookupST cst (toId x))