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