Split into lib

This commit is contained in:
Samae 2023-12-02 17:54:37 +02:00
parent c01a1f9ca4
commit af7ebce206
3 changed files with 81 additions and 65 deletions

66
Main.hs
View file

@ -2,70 +2,10 @@
module Main where module Main where
import Control.Applicative ((<|>),empty) import Data.Attoparsec.Text (parseOnly)
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 qualified Data.Text.IO as T import qualified Data.Text.IO as T
insuranceNetworkParser :: Parser InsuranceNetworkAST import INAST
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
main :: IO () main :: IO ()
main = do main = do
@ -74,5 +14,3 @@ main = do
T.putStrLn inputNetwork T.putStrLn inputNetwork
T.putStrLn "Parsed AST:" T.putStrLn "Parsed AST:"
print (parseOnly insuranceNetworkParser inputNetwork) print (parseOnly insuranceNetworkParser inputNetwork)

View file

@ -1,7 +1,20 @@
name: in23
ghc-options: -Wall -threaded
dependencies: dependencies:
- base == 4.* - base == 4.*
- attoparsec - attoparsec
- text - text
executables: executables:
interview-in: in23:
main: Main.hs main: Main.hs
dependencies:
- in23
library:
source-dirs: src
exposed-modules:
- INAST

65
src/INAST.hs Normal file
View 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