This commit is contained in:
Felix Dilke 2026-01-28 16:45:01 +00:00
parent c0a6a447cf
commit 3c7c587045

View File

@ -7,13 +7,12 @@ import Text.Megaparsec.Char
import qualified Text.Megaparsec.Char.Lexer as L import qualified Text.Megaparsec.Char.Lexer as L
import Data.Void import Data.Void
import Data.Text (Text) import Data.Text (Text)
-- import qualified Data.Text as T
-- Type aliases for convenience -- Type aliases for convenience
type Parser = Parsec Void Text type Parser = Parsec Void Text
-- ┌───────────────────────────────┐ -- ┌───────────────────────────────┐
-- │ AST Definition -- │ AST Definition
-- └───────────────────────────────┘ -- └───────────────────────────────┘
data Expr data Expr
= ENumber Integer -- 42, -7, etc. = ENumber Integer -- 42, -7, etc.
@ -24,7 +23,7 @@ data Expr
deriving (Show, Eq) deriving (Show, Eq)
-- ┌───────────────────────────────┐ -- ┌───────────────────────────────┐
-- │ Basic lexemes -- │ Basic lexemes
-- └───────────────────────────────┘ -- └───────────────────────────────┘
-- Skip spaces & newlines -- Skip spaces & newlines
@ -52,7 +51,7 @@ signedInteger :: Parser Integer
signedInteger = L.signed sc integer signedInteger = L.signed sc integer
-- ┌───────────────────────────────┐ -- ┌───────────────────────────────┐
-- │ Expression levels -- │ Expression levels
-- └───────────────────────────────┘ -- └───────────────────────────────┘
-- Lowest precedence: numbers & parenthesized expressions -- Lowest precedence: numbers & parenthesized expressions
@ -78,7 +77,7 @@ expr = factor `chainl1` addOrSub
<|> ESub <$ symbol "-" <|> ESub <$ symbol "-"
-- ┌───────────────────────────────┐ -- ┌───────────────────────────────┐
-- │ Main parser -- │ Main parser
-- └───────────────────────────────┘ -- └───────────────────────────────┘
parseExpr :: Text -> Either (ParseErrorBundle Text Void) Expr parseExpr :: Text -> Either (ParseErrorBundle Text Void) Expr
@ -86,7 +85,7 @@ parseExpr =
parse (sc *> expr <* eof) "<input>" parse (sc *> expr <* eof) "<input>"
-- ┌───────────────────────────────┐ -- ┌───────────────────────────────┐
-- │ Helpful chainl1 alias -- │ Helpful chainl1 alias
-- └───────────────────────────────┘ -- └───────────────────────────────┘
chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a chainl1 :: Parser a -> Parser (a -> a -> a) -> Parser a
chainl1 p op = do chainl1 p op = do