module Ivory.Language.Ion.Util where
import Data.Char ( isAlpha, isDigit )
import Ivory.Language
import Ivory.Language.Proc ( Def(..), IvoryCall_ )
import qualified Ivory.Language.Syntax.AST as AST
import qualified Ivory.Language.Syntax.Type as Ty
procName :: Def proc -> String
procName def = case def of
DefProc p -> AST.procSym p
DefImport i -> AST.importSym i
fitWordType :: Integer -> Ty.Type
fitWordType i =
if (i < 0)
then error ("fitWordType: Integer " ++ show i ++ " is negative.")
else
if (i < 2^8) then Ty.TyWord Ty.Word8
else
if (i < 2^16) then Ty.TyWord Ty.Word16
else
if (i < 2^32) then Ty.TyWord Ty.Word32
else
if (i < 2^64) then Ty.TyWord Ty.Word64
else error ("fitWordType: Integer " ++ show i ++ " is too large.")
checkCName :: String -> Maybe Int
checkCName [] = Just 0
checkCName str = check str 0
where check :: String -> Int -> Maybe Int
check [] _ = Nothing
check (c:cs) i = if (isAlpha c || '_' == c || (i > 0 && isDigit c))
then check cs (i + 1)
else Just i