Safe Haskell | Safe-Infered |
---|
Creates RPN expressions from infix inputs.
Penny accepts only infix expressions, but RPN expressions are easier to process. This module converts infix expressions to RPN expressions for further processing.
Uses the shunting-yard algorithm, best described at http:www.chris-j.co.uk/parsing.php (be sure to use the "click to display" links).
- newtype Precedence = Precedence Int
- data Associativity
- data Token a
- = TokOperand a
- | TokUnaryPostfix (a -> a)
- | TokUnaryPrefix Precedence (a -> a)
- | TokBinary Precedence Associativity (a -> a -> a)
- | TokOpenParen
- | TokCloseParen
- infixToRPN :: Foldable l => l (Token a) -> Maybe (Seq (Token a))
Documentation
newtype Precedence Source
Precedence can be any integer; the greater the number, the higher the precedence.
TokOperand a | |
TokUnaryPostfix (a -> a) | |
TokUnaryPrefix Precedence (a -> a) | |
TokBinary Precedence Associativity (a -> a -> a) | |
TokOpenParen | |
TokCloseParen |
:: Foldable l | |
=> l (Token a) | Input tokens. These should be in the sequence from left to right in ordinary infix order. The easiest choice is a list, though you might want to use Data.Sequence if many appends will be needed to build the sequence. |
-> Maybe (Seq (Token a)) | The resulting RPN expression. The token type here is a token from Penny.Liberty.Expressions.RPN, which is a different type than the Token in this module. Fails only if there is a close parenthesis without a matching open parenthesis, or if there is an open parenthesis without a matching close parenthesis. Other nonsensical expressions will still parse to an RPN expression successfully, so the RPN parser has to catch these errors. |
Converts an infix expression to an RPN expression.