Split into lib
This commit is contained in:
parent
c01a1f9ca4
commit
af7ebce206
3 changed files with 81 additions and 65 deletions
66
Main.hs
66
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
|
||||
-- <line> ::= <insurer-id> ":" <percent> <optional-exprs>
|
||||
-- <optional-exprs> ::= "" | "if" <exprs>
|
||||
line :: Parser INASTLine
|
||||
line = do
|
||||
id <- insurerId
|
||||
": "
|
||||
pc <- percent
|
||||
oe <- option [] (" if " *> exprs)
|
||||
endOfLine
|
||||
pure $ INASTLine id pc oe
|
||||
-- <exprs> ::= <expr> | <expr> "and" <exprs>
|
||||
exprs :: Parser [ INASTExpr ]
|
||||
exprs = expr `sepBy` " and "
|
||||
-- <expr> ::= <lesser-than> | <greater-than> | <equals> | <insurer-id>
|
||||
expr :: Parser INASTExpr
|
||||
expr = choice [ INASTExprLt <$> lt
|
||||
, INASTExprGt <$> gt
|
||||
, INASTExprEq <$> eq
|
||||
, INASTExprInsurerId <$> insurerId ]
|
||||
-- <lesser-than> ::= "<" <integer>
|
||||
lt :: Parser INASTLt
|
||||
lt = "<" *> integer
|
||||
-- <greater-than> ::= ">" <integer>
|
||||
gt :: Parser INASTGt
|
||||
gt = ">" *> integer
|
||||
-- <equals> ::= "=" <integer>
|
||||
eq :: Parser INASTEq
|
||||
eq = "=" *> integer
|
||||
-- <insurer-id> ::= "I" <integer>
|
||||
insurerId :: Parser INASTInsurerId
|
||||
insurerId = "I" *> integer
|
||||
-- <percent> ::= <integer> "%"
|
||||
percent :: Parser INASTPercent
|
||||
percent = integer <* "%"
|
||||
-- <integer> ::= <digit> | <non-zero-digit> <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)
|
||||
|
||||
|
||||
|
|
15
package.yaml
15
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
|
||||
|
||||
|
|
65
src/INAST.hs
Normal file
65
src/INAST.hs
Normal file
|
@ -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
|
||||
-- <line> ::= <insurer-id> ":" <percent> <optional-exprs>
|
||||
-- <optional-exprs> ::= "" | "if" <exprs>
|
||||
line :: Parser INASTLine
|
||||
line = do
|
||||
id' <- insurerId
|
||||
": "
|
||||
pc <- percent
|
||||
oe <- option [] (" if " *> exprs)
|
||||
endOfLine
|
||||
pure $ INASTLine id' pc oe
|
||||
-- <exprs> ::= <expr> | <expr> "and" <exprs>
|
||||
exprs :: Parser [ INASTExpr ]
|
||||
exprs = expr `sepBy` " and "
|
||||
-- <expr> ::= <lesser-than> | <greater-than> | <equals> | <insurer-id>
|
||||
expr :: Parser INASTExpr
|
||||
expr = choice [ INASTExprLt <$> lt
|
||||
, INASTExprGt <$> gt
|
||||
, INASTExprEq <$> eq
|
||||
, INASTExprInsurerId <$> insurerId ]
|
||||
-- <lesser-than> ::= "<" <integer>
|
||||
lt :: Parser INASTLt
|
||||
lt = "<" *> integer
|
||||
-- <greater-than> ::= ">" <integer>
|
||||
gt :: Parser INASTGt
|
||||
gt = ">" *> integer
|
||||
-- <equals> ::= "=" <integer>
|
||||
eq :: Parser INASTEq
|
||||
eq = "=" *> integer
|
||||
-- <insurer-id> ::= "I" <integer>
|
||||
insurerId :: Parser INASTInsurerId
|
||||
insurerId = "I" *> integer
|
||||
-- <percent> ::= <integer> "%"
|
||||
percent :: Parser INASTPercent
|
||||
percent = integer <* "%"
|
||||
-- <integer> ::= <digit> | <non-zero-digit> <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
|
Loading…
Reference in a new issue