aboutsummaryrefslogtreecommitdiffstats
path: root/src/Types.hs
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2022-09-23 15:02:46 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit51c3993f0a84b3de57bae480ce7bb14a1408f25e (patch)
tree692ecf7b67371f25a5d9d5dbeecc449484dd75e5 /src/Types.hs
parent89a6e5d5a4ab10a5dbc70e891499db851896ac7d (diff)
Implement parser
Diffstat (limited to 'src/Types.hs')
-rw-r--r--src/Types.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Types.hs b/src/Types.hs
index 94428b6..5779845 100644
--- a/src/Types.hs
+++ b/src/Types.hs
@@ -1,7 +1,11 @@
+{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module Types where
import Control.Monad.Except
import Control.Monad.Reader
+import qualified Data.Map as M
+import qualified Data.List as L
+import Utils
newtype LException = LException String
data Config = Config {
@@ -11,3 +15,43 @@ data Config = Config {
}
type LContext a = ReaderT Config (ExceptT LException IO) a
+
+data AST
+ = ASTNumber Double
+ | ASTSymbol String
+ | ASTBoolean Bool
+ | ASTString String
+ | ASTVector [AST]
+ | ASTFunctionCall [AST]
+ | ASTHashMap (M.Map AST AST)
+ | ASTFunction (AST -> AST)
+
+instance (Show AST) where
+ show (ASTNumber n) = show n
+ show (ASTSymbol s) = s
+ show (ASTBoolean b) = show b
+ show (ASTString s) = show s
+ show (ASTVector v) = "[" ++ L.intercalate " " (map show v) ++ "]"
+ show (ASTFunctionCall v) = "(" ++ L.intercalate " " (map show v) ++ ")"
+ show (ASTHashMap m) =
+ let flattenMap = M.assocs .> map (\(k, v) -> [k, v]) .> concat
+ in "{" ++ L.intercalate " " (map show $ flattenMap m) ++ "}"
+ show (ASTFunction _) = "<fn>"
+
+instance (Eq AST) where
+ ASTNumber a == ASTNumber b = a == b
+ ASTSymbol a == ASTSymbol b = a == b
+ ASTBoolean a == ASTBoolean b = a == b
+ ASTString a == ASTString b = a == b
+ ASTVector a == ASTVector b = a == b
+ ASTHashMap a == ASTHashMap b = a == b
+ _ == _ = False
+
+instance (Ord AST) where
+ ASTNumber a <= ASTNumber b = a <= b
+ ASTSymbol a <= ASTSymbol b = a <= b
+ ASTBoolean a <= ASTBoolean b = a <= b
+ ASTString a <= ASTString b = a <= b
+ ASTVector a <= ASTVector b = a <= b
+ ASTHashMap a <= ASTHashMap b = a <= b
+ _ <= _ = False