diff --git a/Main.hs b/Main.hs index 2fbdd98..107c76b 100644 --- a/Main.hs +++ b/Main.hs @@ -1,46 +1,7 @@ module Main where -import Data.Aeson (FromJSON, parseJSON, withObject, withText, (.:), decode) - -data Program = Program { programFns :: [Function] } - deriving (Show) - -data Function = Function { - functionName :: String, - returnType :: Maybe Type, - functionArgs :: [FunctionArg] - } deriving (Show) - -data Type = Int | Bool - deriving (Show) - -data FunctionArg = FunctionArg { - argName :: String, - argType :: Type - } deriving (Show) - -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" +import Bril main :: IO () main = do - putStrLn $ show (decode "int" :: Maybe Type) + putStrLn $ "Hello World!" diff --git a/eoc.cabal b/eoc.cabal index 4beb66d..19ab010 100644 --- a/eoc.cabal +++ b/eoc.cabal @@ -1,4 +1,4 @@ -cabal-version: >=1.10 +cabal-version: 2.0 -- Initial package description 'eoc.cabal' generated by 'cabal init'. For -- further documentation, see http://haskell.org/cabal/users-guide/ @@ -16,13 +16,35 @@ maintainer: enrico.lumetti@gmail.com build-type: Simple extra-source-files: README.md +library eoc-lib + exposed-modules: Eoc, Bril + hs-source-dirs: lib + build-depends: + base >=4.13 && <4.18, + aeson >= 2.2 + default-language: Haskell2010 + default-extensions: DeriveGeneric, OverloadedStrings + executable eoc main-is: Main.hs -- other-modules: -- other-extensions: build-depends: - base >=4.13 && <4.14, - aeson >= 2.2 + base >=4.13 && <4.18, + eoc-lib -- hs-source-dirs: default-language: Haskell2010 default-extensions: DeriveGeneric, OverloadedStrings + +test-suite tests + type: exitcode-stdio-1.0 + main-is: Main.hs + build-depends: + base >=4.13 && <4.18, + HUnit >=1.6, + eoc-lib, + aeson >= 2.2 + hs-source-dirs: tests + default-language: Haskell2010 + default-extensions: DeriveGeneric, OverloadedStrings + diff --git a/lib/Bril.hs b/lib/Bril.hs new file mode 100644 index 0000000..ecdb43a --- /dev/null +++ b/lib/Bril.hs @@ -0,0 +1,47 @@ +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) diff --git a/lib/Eoc.hs b/lib/Eoc.hs new file mode 100644 index 0000000..8a511eb --- /dev/null +++ b/lib/Eoc.hs @@ -0,0 +1,2 @@ +module Eoc where + diff --git a/tests/Main.hs b/tests/Main.hs new file mode 100644 index 0000000..bdddf87 --- /dev/null +++ b/tests/Main.hs @@ -0,0 +1,20 @@ +module Main where + +import qualified Bril as B +import Data.Aeson (decode) +import Data.Maybe + +import Test.HUnit +import qualified System.Exit as Exit + +test1 :: Test +test1 = TestCase + (assertEqual "?" (decode "\"int\"" :: Maybe B.Type) (Just B.Int)) + +tests :: Test +tests = TestList [TestLabel "test1" test1] + +main :: IO () +main = do + result <- runTestTT tests + if failures result > 0 then Exit.exitFailure else Exit.exitSuccess