feat: Add high-level parse function to Parser
This commit is contained in:
parent
9d1a80bf2b
commit
929285b5ef
2 changed files with 9 additions and 2 deletions
|
|
@ -1,8 +1,9 @@
|
|||
module Main where
|
||||
|
||||
import qualified Lexer
|
||||
import qualified Parser
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
source <- readFile "arithmetic.txt"
|
||||
putStr $ show (Lexer.lex source)
|
||||
putStr $ show (Parser.parse $ Lexer.lex source)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import qualified Lexer as L
|
|||
import Control.Applicative as Applicative
|
||||
import Data.Void as Void
|
||||
|
||||
type AST = Expr
|
||||
type CST = Expr
|
||||
|
||||
data Expr = Add Term Expr
|
||||
| Sub Term Expr
|
||||
|
|
@ -46,6 +46,12 @@ instance Alternative Parser where
|
|||
result -> result
|
||||
|
||||
|
||||
parse :: Tokens -> Maybe CST
|
||||
parse tokens = case runParser parseExpression tokens of
|
||||
Just ([], cst) -> Just cst
|
||||
_ -> Nothing
|
||||
|
||||
|
||||
parseAdd :: Parser Expr
|
||||
parseAdd = liftA2 Add (parseTerm <* expect L.Plus) parseExpression
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue