From af7ebce2064e304afa54c16f3d619e74bb81d144 Mon Sep 17 00:00:00 2001 From: Samae Date: Sat, 2 Dec 2023 17:54:37 +0200 Subject: [PATCH] Split into lib --- Main.hs | 66 ++-------------------------------------------------- package.yaml | 15 +++++++++++- src/INAST.hs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 65 deletions(-) create mode 100644 src/INAST.hs diff --git a/Main.hs b/Main.hs index b6d1dab..c48ae35 100644 --- a/Main.hs +++ b/Main.hs @@ -2,70 +2,10 @@ module Main where -import Control.Applicative ((<|>),empty) -import Data.Attoparsec.Text (Parser, parseOnly, decimal, choice, option, sepBy, endOfLine, endOfInput, many1) -import qualified Data.Maybe as M -import qualified Data.Text as T +import Data.Attoparsec.Text (parseOnly) import qualified Data.Text.IO as T -insuranceNetworkParser :: Parser InsuranceNetworkAST -insuranceNetworkParser = many1 line - where - -- ::= ":" - -- ::= "" | "if" - line :: Parser INASTLine - line = do - id <- insurerId - ": " - pc <- percent - oe <- option [] (" if " *> exprs) - endOfLine - pure $ INASTLine id pc oe - -- ::= | "and" - exprs :: Parser [ INASTExpr ] - exprs = expr `sepBy` " and " - -- ::= | | | - expr :: Parser INASTExpr - expr = choice [ INASTExprLt <$> lt - , INASTExprGt <$> gt - , INASTExprEq <$> eq - , INASTExprInsurerId <$> insurerId ] - -- ::= "<" - lt :: Parser INASTLt - lt = "<" *> integer - -- ::= ">" - gt :: Parser INASTGt - gt = ">" *> integer - -- ::= "=" - eq :: Parser INASTEq - eq = "=" *> integer - -- ::= "I" - insurerId :: Parser INASTInsurerId - insurerId = "I" *> integer - -- ::= "%" - percent :: Parser INASTPercent - percent = integer <* "%" - -- ::= | - -- Overapproximation of the grammar - integer :: Parser Int - integer = decimal - -type InsuranceNetworkAST = [INASTLine] - -data INASTLine = INASTLine INASTInsurerId INASTPercent [INASTExpr] - deriving (Show) - -data INASTExpr = INASTExprLt INASTLt - | INASTExprGt INASTGt - | INASTExprEq INASTEq - | INASTExprInsurerId INASTInsurerId - deriving (Show) - -type INASTLt = Int -type INASTGt = Int -type INASTEq = Int -type INASTInsurerId = Int -type INASTPercent = Int +import INAST main :: IO () main = do @@ -74,5 +14,3 @@ main = do T.putStrLn inputNetwork T.putStrLn "Parsed AST:" print (parseOnly insuranceNetworkParser inputNetwork) - - diff --git a/package.yaml b/package.yaml index 8978128..67a78af 100644 --- a/package.yaml +++ b/package.yaml @@ -1,7 +1,20 @@ +name: in23 + +ghc-options: -Wall -threaded + dependencies: - base == 4.* - attoparsec - text + executables: - interview-in: + in23: main: Main.hs + dependencies: + - in23 + +library: + source-dirs: src + exposed-modules: + - INAST + diff --git a/src/INAST.hs b/src/INAST.hs new file mode 100644 index 0000000..816a12c --- /dev/null +++ b/src/INAST.hs @@ -0,0 +1,65 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# OPTIONS_GHC -Wno-unused-do-bind #-} + +module INAST where + +import Data.Attoparsec.Text (Parser, decimal, choice, option, sepBy, endOfLine, many1) + +insuranceNetworkParser :: Parser InsuranceNetworkAST +insuranceNetworkParser = many1 line + where + -- ::= ":" + -- ::= "" | "if" + line :: Parser INASTLine + line = do + id' <- insurerId + ": " + pc <- percent + oe <- option [] (" if " *> exprs) + endOfLine + pure $ INASTLine id' pc oe + -- ::= | "and" + exprs :: Parser [ INASTExpr ] + exprs = expr `sepBy` " and " + -- ::= | | | + expr :: Parser INASTExpr + expr = choice [ INASTExprLt <$> lt + , INASTExprGt <$> gt + , INASTExprEq <$> eq + , INASTExprInsurerId <$> insurerId ] + -- ::= "<" + lt :: Parser INASTLt + lt = "<" *> integer + -- ::= ">" + gt :: Parser INASTGt + gt = ">" *> integer + -- ::= "=" + eq :: Parser INASTEq + eq = "=" *> integer + -- ::= "I" + insurerId :: Parser INASTInsurerId + insurerId = "I" *> integer + -- ::= "%" + percent :: Parser INASTPercent + percent = integer <* "%" + -- ::= | + -- Overapproximation of the grammar + integer :: Parser Int + integer = decimal + +type InsuranceNetworkAST = [INASTLine] + +data INASTLine = INASTLine INASTInsurerId INASTPercent [INASTExpr] + deriving (Show) + +data INASTExpr = INASTExprLt INASTLt + | INASTExprGt INASTGt + | INASTExprEq INASTEq + | INASTExprInsurerId INASTInsurerId + deriving (Show) + +type INASTLt = Int +type INASTGt = Int +type INASTEq = Int +type INASTInsurerId = Int +type INASTPercent = Int