definedConstant :: Parser Constant
definedConstant = identifier >>= resolve
  where
    resolve cst = do
        st <- getState
        let ent = lookup cst $ consttbl st
        when (isNothing ent) $ unexpected $ ": No definition of '" ++ cst ++ "'"
        let cused = cst : used st
            cunused = delete cst $ unused st
        setState st { used = cused, unused = cunused }
        let cd@(typ, CV_Index n) = fromJust ent
        if typ == DT_Res then return (typ, CV_Result (reslol st !! n))
                         else return cd
definedConstant :: Parser Constant
definedConstant = identifier >>= resolve
  where
    resolve cst = do
        st <- getState
        let ent = lookup cst $ consttbl st
        when (isNothing ent) $ unexpected $ ": No definition of '" ++ cst ++ "'"
        let cused = cst : used st
            cunused = delete cst $ unused st
        setState st { used = cused, unused = cunused }
        let cd@(typ, CV_Index n) = fromJust ent
        if typ == DT_Res then return (typ, CV_Result (reslol st !! n))
                         else return cd
definedConstant = identifier >>= resolve
  where
    resolve cst = do
        st <- getState
        let ent = lookup cst $ consttbl st
        when (isNothing ent) $ unexpected $ ": No definition of '" ++ cst ++ "'"
        let cused = cst : used st
            cunused = delete cst $ unused st
        setState st { used = cused, unused = cunused }
        let cd@(typ, CV_Index n) = fromJust ent
        if typ == DT_Res then return (typ, CV_Result (reslol st !! n))
                         else return cd