Parse "print" statement
parent
d40467bc00
commit
e162d55439
|
@ -1 +1 @@
|
||||||
5*3-2+-4-4
|
print 5*3-2+-4-4
|
|
@ -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
|
||||||
|
|
Reference in New Issue