Skip to content

Instantly share code, notes, and snippets.

@hasufell
Created February 11, 2015 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasufell/54c68fc61e938df59c43 to your computer and use it in GitHub Desktop.
Save hasufell/54c68fc61e938df59c43 to your computer and use it in GitHub Desktop.
module Grammar where
data Expr a = Plus (Expr a) (Term a)
| Minus (Expr a) (Term a)
| ExprTerm (Term a)
data Term a = Mult (Term a) (Factor a)
| Div (Term a) (Factor a)
| TermFactor (Factor a)
data Factor a = Constant a
| FactorExpr (Expr a)
runExpr :: Expr Double -> Double
runExpr (Plus expr term) = runExpr expr + runTerm term
runExpr (Minus expr term) = runExpr expr - runTerm term
runExpr (ExprTerm term) = runTerm term
runTerm :: Term Double -> Double
runTerm (Mult term fac) = runTerm term * runFac fac
runTerm (Div term fac) = runTerm term / runFac fac
runTerm (TermFactor fac) = runFac fac
runFac :: Factor Double -> Double
runFac (Constant a) = a
runFac (FactorExpr expr) = runExpr expr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment