eoc/lib/Bril.hs

48 lines
1.2 KiB
Haskell

module Bril (
Function (..), Type (..), FunctionArg (..), Program (..), Int, Bool,
)where
import Data.Aeson (FromJSON, parseJSON, withObject, withText, (.:), decode, Value)
data Program = Program { programFns :: [Function] }
deriving (Show, Eq)
data Function = Function {
functionName :: String,
returnType :: Maybe Type,
functionArgs :: [FunctionArg]
} deriving (Show, Eq)
data Type = Int | Bool
deriving (Show, Eq)
data FunctionArg = FunctionArg {
argName :: String,
argType :: Type
} deriving (Show, Eq)
instance FromJSON Program where
parseJSON = withObject "Program" $ \v -> Program
<$> v .: "functions"
instance FromJSON Function where
parseJSON = withObject "Function" $ \v -> Function
<$> v .: "name"
<*> v .: "type"
<*> v .: "args"
instance FromJSON FunctionArg where
parseJSON = withObject "FunctionArg" $ \v -> FunctionArg
<$> v .: "name"
<*> v .: "type"
instance FromJSON Type where
parseJSON = withText "Type" $ \v ->
case v of
"int" -> return Int
"bool" -> return Bool
_ -> fail "wrong type"
parseBrilJSON text = (decode text :: Maybe Program)