aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2022-09-23 15:14:37 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commitc02d29e8e119d12748c90b3965f6b98633efbec2 (patch)
tree8b72350fa9ad7454c70f1fe3626f27a478b04eee
parent51c3993f0a84b3de57bae480ce7bb14a1408f25e (diff)
Add integer type
-rw-r--r--src/Lib.hs12
-rw-r--r--src/Types.hs12
2 files changed, 16 insertions, 8 deletions
diff --git a/src/Lib.hs b/src/Lib.hs
index 13e4234..8c2fc06 100644
--- a/src/Lib.hs
+++ b/src/Lib.hs
@@ -69,14 +69,18 @@ asPairs _ = throwError $ LException "Odd number of elements to pair up"
parseToken :: String -> AST
parseToken token
- | isNumber token = ASTNumber (read token)
+ | isInteger token = ASTInteger (read token)
+ | isDouble token = ASTDouble (read token)
| isString token = ASTString $ removeQuotes token
| isBoolean token = ASTBoolean $ asBoolean token
| otherwise = ASTSymbol token
where
- numberRegex = "^-?[[:digit:]]+(\\.[[:digit:]]+)?$"
- isNumber :: String -> Bool
- isNumber t = t =~ numberRegex
+ integerRegex = "^-?[[:digit:]]+$"
+ isInteger :: String -> Bool
+ isInteger t = t =~ integerRegex
+ doubleRegex = "^-?[[:digit:]]+(\\.[[:digit:]]+)?$"
+ isDouble :: String -> Bool
+ isDouble t = t =~ doubleRegex
isString t = "\"" `L.isPrefixOf` t
removeQuotes s = drop 1 s $> take (length s - 2)
isBoolean t = t `elem` ["true", "false"]
diff --git a/src/Types.hs b/src/Types.hs
index 5779845..be9002a 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -17,7 +17,8 @@ data Config = Config {
type LContext a = ReaderT Config (ExceptT LException IO) a
data AST
- = ASTNumber Double
+ = ASTInteger Int
+ | ASTDouble Double
| ASTSymbol String
| ASTBoolean Bool
| ASTString String
@@ -27,7 +28,8 @@ data AST
| ASTFunction (AST -> AST)
instance (Show AST) where
- show (ASTNumber n) = show n
+ show (ASTInteger n) = show n
+ show (ASTDouble n) = show n
show (ASTSymbol s) = s
show (ASTBoolean b) = show b
show (ASTString s) = show s
@@ -39,7 +41,8 @@ instance (Show AST) where
show (ASTFunction _) = "<fn>"
instance (Eq AST) where
- ASTNumber a == ASTNumber b = a == b
+ ASTInteger a == ASTInteger b = a == b
+ ASTDouble a == ASTDouble b = a == b
ASTSymbol a == ASTSymbol b = a == b
ASTBoolean a == ASTBoolean b = a == b
ASTString a == ASTString b = a == b
@@ -48,7 +51,8 @@ instance (Eq AST) where
_ == _ = False
instance (Ord AST) where
- ASTNumber a <= ASTNumber b = a <= b
+ ASTInteger a <= ASTInteger b = a <= b
+ ASTDouble a <= ASTDouble b = a <= b
ASTSymbol a <= ASTSymbol b = a <= b
ASTBoolean a <= ASTBoolean b = a <= b
ASTString a <= ASTString b = a <= b