-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStringFunctions.hs
31 lines (24 loc) · 980 Bytes
/
StringFunctions.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module StringFunctions where
import Tokenizer
import Evaluator
import Conversion
import Control.Monad.Error
import Data.Char
functions :: [(String, PHPFunctionType)]
functions = [ ("strlen", phpStrLen)
, ("strtoupper", phpStrToUpper)
, ("strtolower", phpStrToLower)]
-- Conversion of a PHPString for a Haskell [Char]
toHaskellStr :: PHPValue -> String
toHaskellStr x = stringFromPHPValue $ castToString x
-- Throws an arity error
arityErrorFor f = throwError $ Default $ "Wrong number of arguments to " ++ f
phpStrLen :: PHPFunctionType
phpStrLen (s:[]) = return $ PHPInt $ toInteger $ length $ toHaskellStr s
phpStrLen _ = arityErrorFor "strlen"
phpStrToUpper :: PHPFunctionType
phpStrToUpper (s:[]) = return $ PHPString $ map toUpper $ toHaskellStr s
phpStrToUpper _ = arityErrorFor "strtoupper"
phpStrToLower :: PHPFunctionType
phpStrToLower (s:[]) = return $ PHPString $ map toLower $ toHaskellStr s
phpStrToLower _ = arityErrorFor "strtolower"