Skip to content

Instantly share code, notes, and snippets.

@jg
Created October 11, 2016 20:17
Show Gist options
  • Save jg/0338169fe95998a0f4714c0b674277d8 to your computer and use it in GitHub Desktop.
Save jg/0338169fe95998a0f4714c0b674277d8 to your computer and use it in GitHub Desktop.
-- arithmetic operators:
data AExpr = Var String Info
| IntConst Integer Info
| Neg AExpr Info
| ABinary ABinOp AExpr AExpr Info
deriving (Show, Eq)
aExpression :: Parser AExpr
aExpression = buildExpressionParser aOperators aTerm
bExpression :: Parser BExpr
bExpression = buildExpressionParser bOperators bTerm
aOperators = [ [prefix "-" Neg]
, [binary "*" (ABinary Multiply) AssocLeft]
, [binary "/" (ABinary Divide) AssocLeft]
, [binary "+" (ABinary Add) AssocLeft]
, [binary "-" (ABinary Subtract) AssocLeft]
]
bOperators = [ [prefix "not" (Not)]
, [binary "and" (BBinary And) AssocLeft]
, [binary "or" (BBinary Or) AssocLeft]
]
binary name fun assoc = Infix (do{ reservedOp name; pos <- getPosition; return fun (getInfo pos)}) assoc
prefix name fun = Prefix (do{ reservedOp name; pos <- getPosition; return fun (getInfo pos)})
postfix name fun = Postfix (do{ reservedOp name; pos <- getPosition; return fun (getInfo pos)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment