<commit_before>module Conversion where

import Tokenizer
import Text.Read
import Data.Maybe

castToBool :: PHPValue -> PHPValue
castToBool (PHPString a) | a == ""   = PHPBool False
                         | a == "0"  = PHPBool False
                         | otherwise = PHPBool True

castToBool (PHPInt a) | a == 0    = PHPBool False
                      | otherwise = PHPBool True

castToBool a@(PHPBool _) = a

castToBool (PHPFloat a) | a == 0    = PHPBool False
                        | otherwise = PHPBool True

castToBool PHPNull = PHPBool False

isTruthy :: PHPValue -> Bool
isTruthy = getBool . castToBool
    where getBool (PHPBool b) = b

castToInt :: PHPValue -> PHPValue
castToInt (PHPString _) = error "string to int behavior is not implemented"
castToInt a@(PHPInt _) = a
castToInt (PHPFloat a) = PHPInt $ floor a
castToInt (PHPBool a) | a == True  = PHPInt 1
                      | a == False = PHPInt 0
castToInt PHPNull = PHPInt 0

castToFloat :: PHPValue -> PHPValue
castToFloat (PHPString _) = error "undefined behavior for string to float"
castToFloat (PHPInt a) = PHPFloat $ fromInteger a
castToFloat a@(PHPFloat _) = a
castToFloat PHPNull = PHPFloat 0
castToFloat (PHPBool a) | a == True  = PHPFloat 1
                        | a == False = PHPFloat 0

castToString :: PHPValue -> PHPValue
castToString a@(PHPString _) = a
castToString (PHPInt a) = PHPString (show a)
castToString (PHPFloat a) = PHPString (show a)
castToString a@(PHPBool _) = castToString $ castToInt a
castToString PHPNull = PHPString ""

phpSum :: PHPValue -> PHPValue -> PHPValue
phpSum (PHPFloat a) (PHPFloat b) = PHPFloat (a + b)
phpSum (PHPInt a) (PHPInt b) = PHPInt (a + b)
phpSum a@(PHPFloat _) b = phpSum a (castToFloat b)
phpSum a b@(PHPFloat _) = phpSum (castToFloat a) b
phpSum a@(PHPInt _) b = phpSum a (castToInt b)
phpSum a b@(PHPInt _) = phpSum (castToInt a) b
phpSum a b = phpSum (castToInt a) (castToInt b)

phpSubtract :: PHPValue -> PHPValue -> PHPValue
phpSubtract a b = uncurry sub $ makeCompatible (a, b)
    where sub (PHPFloat a) (PHPFloat b) = PHPFloat (a - b)
          sub (PHPInt a) (PHPInt b) = PHPInt (a - b)

phpMultiply :: PHPValue -> PHPValue -> PHPValue
phpMultiply a b = uncurry mul $ makeCompatible (a, b)
    where mul (PHPFloat a) (PHPFloat b) = PHPFloat (a * b)<commit_msg>Implemented type conversion from string to int and float<commit_after>module Conversion where

import Tokenizer
import Text.Read
import Data.Maybe

castToBool :: PHPValue -> PHPValue
castToBool (PHPString a) | a == ""   = PHPBool False
                         | a == "0"  = PHPBool False
                         | otherwise = PHPBool True

castToBool (PHPInt a) | a == 0    = PHPBool False
                      | otherwise = PHPBool True

castToBool a@(PHPBool _) = a

castToBool (PHPFloat a) | a == 0    = PHPBool False
                        | otherwise = PHPBool True

castToBool PHPNull = PHPBool False

isTruthy :: PHPValue -> Bool
isTruthy = getBool . castToBool
    where getBool (PHPBool b) = b

castToInt :: PHPValue -> PHPValue
castToInt (PHPString a) = PHPInt $ read a :: PHPValue
castToInt a@(PHPInt _) = a
castToInt (PHPFloat a) = PHPInt $ floor a
castToInt (PHPBool a) | a == True  = PHPInt 1
                      | a == False = PHPInt 0
castToInt PHPNull = PHPInt 0

castToFloat :: PHPValue -> PHPValue
castToFloat (PHPString a) = PHPFloat $ read a :: PHPValue
castToFloat (PHPInt a) = PHPFloat $ fromInteger a
castToFloat a@(PHPFloat _) = a
castToFloat PHPNull = PHPFloat 0
castToFloat (PHPBool a) | a == True  = PHPFloat 1
                        | a == False = PHPFloat 0

castToString :: PHPValue -> PHPValue
castToString a@(PHPString _) = a
castToString (PHPInt a) = PHPString (show a)
castToString (PHPFloat a) = PHPString (show a)
castToString a@(PHPBool _) = castToString $ castToInt a
castToString PHPNull = PHPString ""

phpSum :: PHPValue -> PHPValue -> PHPValue
phpSum (PHPFloat a) (PHPFloat b) = PHPFloat (a + b)
phpSum (PHPInt a) (PHPInt b) = PHPInt (a + b)
phpSum a@(PHPFloat _) b = phpSum a (castToFloat b)
phpSum a b@(PHPFloat _) = phpSum (castToFloat a) b
phpSum a@(PHPInt _) b = phpSum a (castToInt b)
phpSum a b@(PHPInt _) = phpSum (castToInt a) b
phpSum a b = phpSum (castToInt a) (castToInt b)

phpSubtract :: PHPValue -> PHPValue -> PHPValue
phpSubtract a b = uncurry sub $ makeCompatible (a, b)
    where sub (PHPFloat a) (PHPFloat b) = PHPFloat (a - b)
          sub (PHPInt a) (PHPInt b) = PHPInt (a - b)

phpMultiply :: PHPValue -> PHPValue -> PHPValue
phpMultiply a b = uncurry mul $ makeCompatible (a, b)
    where mul (PHPFloat a) (PHPFloat b) = PHPFloat (a * b)
{'fraction_non_alphanumeric': 0.10173160173160173, 'fraction_numerical': 0.003896103896103896, 'mean_word_length': 3.0112847222222223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1298161', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1537, 'n_words': 538}