packages feed

cao-0.1: src/Language/CAO/Index/Utils.hs

{- |
Module      :  $Header$
Description :  Useful function to manipulate indexes.
Copyright   :  (c) SMART Team / HASLab
License     :  GPL

Maintainer  :  Paulo Silva <paufil@di.uminho.pt>
Stability   :  experimental
Portability :  non-portable

-}

module Language.CAO.Index.Utils where

import Language.CAO.Common.Literal
import Language.CAO.Common.SrcLoc
import Language.CAO.Common.Var

import Language.CAO.Index

import Language.CAO.Syntax

import Language.CAO.Type

ind2Expr :: IExpr Var -> LExpr Var
-- TODO: Not handling int's
ind2Expr (IInt n) = genLoc $ Lit $ ILit n
ind2Expr (IInd v) = genLoc $ Var v
ind2Expr (ISum l) = aux l
    where
    aux :: [IExpr Var] -> LExpr Var
    aux [] = error "<Utils.hs><ind2Expr>: empty sum list"
    aux [e] =  ind2Expr e
    -- TODO: HACK to handle type annotations
    aux (e:es) = genLoc $ BinaryOp (ArithOp Plus) (annL RInt (ind2Expr e)) (annL RInt (aux es))
    
    
ind2Expr (IArith op e1 e2) = genLoc $ 
    BinaryOp (ArithOp (iAOp2AOp op)) (annL RInt (ind2Expr e1)) (annL RInt (ind2Expr e2))
ind2Expr (ISym e) = genLoc $ UnaryOp Sym (annL RInt (ind2Expr e))

iAOp2AOp :: IAOp -> AOp
iAOp2AOp IMinus = Minus
iAOp2AOp ITimes = Times
iAOp2AOp IPower = Power
iAOp2AOp IDiv = Div
iAOp2AOp IModOp = ModOp

queryIndexTy :: IExpr Var -> Type Var
queryIndexTy (IInt _) = RInt -- TODO: what about Int's??
queryIndexTy (IInd v) = varType v
queryIndexTy (ISym e) = queryIndexTy e
queryIndexTy (IArith _ e _) = queryIndexTy e
queryIndexTy (ISum (e:_)) = queryIndexTy e
queryIndexTy _ = error "<queryIndexTy>: not expected"

mapAOp :: AOp -> (IExpr id -> IExpr id -> IExpr id)
mapAOp Minus = (.-.)
mapAOp Times = (.*.)
mapAOp Power = (.**.)
mapAOp Div   = (./.)
mapAOp ModOp = (.%.)
mapAOp _ = error "<mapAOp>: not expected"

mapBOp :: BOp -> (ICond id -> ICond id -> ICond id)
mapBOp Or  = (.||.)
mapBOp Xor = (.^^.)
mapBOp _ = error "<mapBOp>: not expected"

mapCOp :: COp -> (IExpr id -> IExpr id -> ICond id)
mapCOp Eq  = (.==.)
mapCOp Neq = (./=.)
mapCOp Lt  = (.<.)
mapCOp Leq = (.<=.)
mapCOp Gt  = (.>.)
mapCOp Geq = (.>=.)