Parse "print" statement

pull/25/head
Ethan Reece 2023-09-30 05:04:39 -05:00
parent d40467bc00
commit e162d55439
Signed by: me
GPG Key ID: D3993665FF92E1C3
2 changed files with 13 additions and 6 deletions

View File

@ -1 +1 @@
5*3-2+-4-4
print 5*3-2+-4-4

View File

@ -25,12 +25,16 @@ symbol = L.symbol C.space
int :: Parser Int
int = lexeme $ L.signed (return ()) L.decimal
string :: Text -> Parser Text
string = C.string
term :: Parser Expr
term = Lit <$> int
table :: [[Operator Parser Expr]]
table =
[ [ binaryOp "*" Mul,
[ [methodOp "print" Print],
[ binaryOp "*" Mul,
binaryOp "/" Div
],
[ binaryOp "+" Add,
@ -41,11 +45,14 @@ table =
binaryOp :: Text -> (a -> a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
binaryOp name f = InfixL (f <$ symbol name)
prefixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
prefixOp name f = Prefix (f <$ symbol name)
-- prefixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
-- prefixOp name f = Prefix (f <$ symbol name)
postfixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
postfixOp name f = Postfix (f <$ symbol name)
methodOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
methodOp name f = Prefix (f <$ (string name <* C.space))
-- postfixOp :: Text -> (a -> a) -> Operator (ParsecT Void Text Data.Functor.Identity.Identity) a
-- postfixOp name f = Postfix (f <$ symbol name)
expr :: Parser Expr
expr = makeExprParser term table