Skip to content

Instantly share code, notes, and snippets.

@mthadley
Created December 27, 2016 20:35
Show Gist options
  • Save mthadley/64b50d65969fe10b77e6de6de199d103 to your computer and use it in GitHub Desktop.
Save mthadley/64b50d65969fe10b77e6de6de199d103 to your computer and use it in GitHub Desktop.
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