{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
module Fay.Compiler.PrimOp
( fayBuiltin
, findPrimOp
, resolvePrimOp
) where
import Fay.Compiler.Prelude
import Fay.Exts.NoAnnotation (unAnn)
import qualified Fay.Exts.NoAnnotation as N
import Data.Map (Map)
import qualified Data.Map as M
import Language.Haskell.Exts
fayBuiltin :: a -> String -> QName a
fayBuiltin a = Qual a (ModuleName a "Fay$") . Ident a
primOpsMap :: Map N.Name N.QName
primOpsMap = M.fromList
[ (Symbol () ">>", fayBuiltin () "then")
, (Symbol () ">>=", fayBuiltin () "bind")
, (Ident () "return", fayBuiltin () "return")
, (Ident () "force", fayBuiltin () "force")
, (Ident () "seq", fayBuiltin () "seq")
, (Symbol () "*", fayBuiltin () "mult")
, (Symbol () "+", fayBuiltin () "add")
, (Symbol () "-", fayBuiltin () "sub")
, (Symbol () "/", fayBuiltin () "divi")
, (Symbol () "==", fayBuiltin () "eq")
, (Symbol () "/=", fayBuiltin () "neq")
, (Symbol () ">", fayBuiltin () "gt")
, (Symbol () "<", fayBuiltin () "lt")
, (Symbol () ">=", fayBuiltin () "gte")
, (Symbol () "<=", fayBuiltin () "lte")
, (Symbol () "&&", fayBuiltin () "and")
, (Symbol () "||", fayBuiltin () "or")
]
findPrimOp :: N.QName -> Maybe N.QName
findPrimOp (Qual _ (ModuleName _ "Prelude") s) = M.lookup s primOpsMap
findPrimOp _ = Nothing
resolvePrimOp :: QName a -> Maybe N.QName
resolvePrimOp (unAnn -> q) = case q of
(Qual _ (ModuleName _ "Prelude") _) -> findPrimOp q
(UnQual _ n) -> findPrimOp $ Qual () (ModuleName () "Prelude") n
_ -> Nothing