Skip to content

Commit

Permalink
Separate i16, i32, i64
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Dec 9, 2024
1 parent 3e60c0a commit de81451
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
8 changes: 4 additions & 4 deletions eo-phi-normalizer/src/Language/EO/Phi/Dataize/Atoms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ knownAtomsList =
-- , ("Lorg_eolang_fs_file_open_process_file", _)
-- , ("Lorg_eolang_fs_file_size", _)
-- , ("Lorg_eolang_fs_file_touched_touch", _)
("Lorg_eolang_i16_as_i32", evaluateUnaryDataizationFunChain intToBytes bytesToInt wrapBytesInConstInt extractRho id)
, ("Lorg_eolang_i32_as_i64", evaluateUnaryDataizationFunChain intToBytes bytesToInt wrapBytesInConstInt extractRho id)
, ("Lorg_eolang_i64_as_number", evaluateUnaryDataizationFunChain floatToBytes bytesToInt wrapBytesInConstInt extractRho fromIntegral)
("Lorg_eolang_i16_as_i32", evaluateUnaryDataizationFunChain int16ToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i32_as_i64", evaluateUnaryDataizationFunChain int32ToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i64_as_number", evaluateUnaryDataizationFunChain floatToBytes bytesToInt64 wrapBytesInConstInt extractRho fromIntegral)
, ("Lorg_eolang_i64_div", evaluateIntIntMaybeIntFunChain (\x y -> if y == 0 then Nothing else Just (x `quot` y)))
, ("Lorg_eolang_i64_gt", evaluateIntIntBoolFunChain (>))
, ("Lorg_eolang_i64_plus", evaluateIntIntIntFunChain (+))
Expand All @@ -117,7 +117,7 @@ knownAtomsList =
, ("Lorg_eolang_math_real_ln", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho log)
, ("Lorg_eolang_math_real_pow", evaluateFloatFloatFloatFunChain (**))
, ("Lorg_eolang_math_real_sqrt", evaluateUnaryDataizationFunChain floatToBytes bytesToFloat wrapBytesInConstFloat extractRho sqrt)
, ("Lorg_eolang_number_as_i64", evaluateUnaryDataizationFunChain intToBytes bytesToFloat wrapBytesInConstInt extractRho round)
, ("Lorg_eolang_number_as_i64", evaluateUnaryDataizationFunChain int64ToBytes bytesToFloat wrapBytesInConstInt extractRho round)
, ("Lorg_eolang_number_div", evaluateFloatFloatFloatFunChain (/))
, ("Lorg_eolang_number_floor", evaluateUnaryDataizationFunChain intToBytes bytesToFloat wrapBytesInConstInt extractRho floor)
, ("Lorg_eolang_number_gt", evaluateBinaryDataizationFunChain boolToBytes bytesToFloat wrapBytesInBytes extractRho (extractLabel "x") (>))
Expand Down
106 changes: 106 additions & 0 deletions eo-phi-normalizer/src/Language/EO/Phi/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ module Language.EO.Phi.Syntax (

-- * Conversion to 'Bytes'
intToBytes,
int64ToBytes,
int32ToBytes,
int16ToBytes,
floatToBytes,
boolToBytes,
stringToBytes,

-- * Conversion from 'Bytes'
bytesToInt,
bytesToInt64,
bytesToInt32,
bytesToInt16,
bytesToFloat,
bytesToString,
bytesToBool,
Expand Down Expand Up @@ -70,6 +76,7 @@ module Language.EO.Phi.Syntax (
import Data.ByteString (ByteString)
import Data.ByteString qualified as ByteString.Strict
import Data.Char (isSpace, toUpper)
import Data.Int
import Data.List (intercalate)
import Data.Serialize qualified as Serialize
import Data.String (IsString (fromString))
Expand Down Expand Up @@ -344,6 +351,39 @@ sliceBytes (Bytes bytes) start len = Bytes $ normalizeBytes $ take (2 * len) (dr
intToBytes :: Int -> Bytes
intToBytes n = Bytes $ normalizeBytes $ foldMap (padLeft 2 . (`showHex` "")) $ ByteString.Strict.unpack $ Serialize.encode n

-- | Convert an 'Int64' into 'Bytes' representation.
--
-- >>> int64ToBytes 7
-- Bytes "00-00-00-00-00-00-00-07"
-- >>> int64ToBytes (3^33)
-- Bytes "00-13-BF-EF-A6-5A-BB-83"
-- >>> int64ToBytes (-1)
-- Bytes "FF-FF-FF-FF-FF-FF-FF-FF"
int64ToBytes :: Int64 -> Bytes
int64ToBytes n = Bytes $ normalizeBytes $ foldMap (padLeft 2 . (`showHex` "")) $ ByteString.Strict.unpack $ Serialize.encode n

-- | Convert an 'Int32' into 'Bytes' representation.
--
-- >>> int32ToBytes 7
-- Bytes "00-00-00-07"
-- >>> int32ToBytes (3^33)
-- Bytes "A6-5A-BB-83"
-- >>> int32ToBytes (-1)
-- Bytes "FF-FF-FF-FF"
int32ToBytes :: Int32 -> Bytes
int32ToBytes n = Bytes $ normalizeBytes $ foldMap (padLeft 2 . (`showHex` "")) $ ByteString.Strict.unpack $ Serialize.encode n

-- | Convert an 'Int16' into 'Bytes' representation.
--
-- >>> int16ToBytes 7
-- Bytes "00-07"
-- >>> int16ToBytes (3^33)
-- Bytes "BB-83"
-- >>> int16ToBytes (-1)
-- Bytes "FF-FF"
int16ToBytes :: Int16 -> Bytes
int16ToBytes n = Bytes $ normalizeBytes $ foldMap (padLeft 2 . (`showHex` "")) $ ByteString.Strict.unpack $ Serialize.encode n

-- | Parse 'Bytes' as 'Int'.
--
-- >>> bytesToInt "00-13-BF-EF-A6-5A-BB-83"
Expand All @@ -366,6 +406,72 @@ bytesToInt (Bytes (dropWhile (== '0') . filter (/= '-') -> bytes))
| null bytes = 0
| otherwise = fst $ head $ readHex bytes

-- | Parse 'Bytes' as 'Int64'.
--
-- >>> bytesToInt64 "00-13-BF-EF-A6-5A-BB-83"
-- 5559060566555523
-- >>> bytesToInt64 "AB-"
-- 171
--
-- May error on invalid 'Bytes':
--
-- >>> bytesToInt64 "s"
-- *** Exception: Prelude.head: empty list
-- ...
-- ...
-- ...
-- ...
-- ...
-- ...
bytesToInt64 :: Bytes -> Int64
bytesToInt64 (Bytes (dropWhile (== '0') . filter (/= '-') -> bytes))
| null bytes = 0
| otherwise = fst $ head $ readHex bytes

-- | Parse 'Bytes' as 'Int32'.
--
-- >>> bytesToInt32 "A6-5A-BB-83"
-- -1504003197
-- >>> bytesToInt32 "AB-"
-- 171
--
-- May error on invalid 'Bytes':
--
-- >>> bytesToInt32 "s"
-- *** Exception: Prelude.head: empty list
-- ...
-- ...
-- ...
-- ...
-- ...
-- ...
bytesToInt32 :: Bytes -> Int32
bytesToInt32 (Bytes (dropWhile (== '0') . filter (/= '-') -> bytes))
| null bytes = 0
| otherwise = fst $ head $ readHex bytes

-- | Parse 'Bytes' as 'Int16'.
--
-- >>> bytesToInt16 "BB-83"
-- -17533
-- >>> bytesToInt16 "AB-"
-- 171
--
-- May error on invalid 'Bytes':
--
-- >>> bytesToInt16 "s"
-- *** Exception: Prelude.head: empty list
-- ...
-- ...
-- ...
-- ...
-- ...
-- ...
bytesToInt16 :: Bytes -> Int16
bytesToInt16 (Bytes (dropWhile (== '0') . filter (/= '-') -> bytes))
| null bytes = 0
| otherwise = fst $ head $ readHex bytes

-- | Convert 'Bool' to 'Bytes'.
--
-- >>> boolToBytes False
Expand Down

0 comments on commit de81451

Please sign in to comment.