Created
December 27, 2016 20:35
-
-
Save mthadley/64b50d65969fe10b77e6de6de199d103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Parser exposing (..) | |
import Combine exposing (..) | |
import Combine.Char exposing (char) | |
import Combine.Num exposing (int) | |
type Program | |
= Program Expr | |
type Expr | |
= NumOp Expr OpType Expr | |
| NumLiteral Int | |
type OpType | |
= Add | |
| Sub | |
| Div | |
program : Parser s Program | |
program = | |
Program <$> expr <* end | |
expr : Parser s Expr | |
expr = | |
let | |
helper = | |
choice | |
[ numOp | |
, numLiteral | |
] | |
in | |
whitespace *> helper <* whitespace | |
numOp : Parser s Expr | |
numOp = | |
lazy <| | |
\() -> | |
succeed NumOp | |
<*> numLiteral | |
<* whitespace | |
<*> opType | |
<* whitespace | |
<*> expr | |
opType : Parser s OpType | |
opType = | |
choice | |
[ Add <$ char '+' | |
, Sub <$ char '-' | |
, Div <$ char '/' | |
] | |
numLiteral : Parser s Expr | |
numLiteral = | |
NumLiteral <$> int |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment