aboutsummaryrefslogtreecommitdiff
path: root/src/Operators.hs
blob: e57f885aa4970fb8f39ce2c4defe62c03bcddb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Operators (
    primitives
    ) where

import           Parser

primitives :: [(String, [Expr] -> Expr)]
primitives =
    [
    ("+", arithmetic (+))
    , ("-", arithmetic (-))
    , ("*", arithmetic (*))
    , ("/", arithmetic div)
    ]

arithmetic :: (Integer -> Integer -> Integer) -> [Expr] -> Expr
arithmetic op args = IntLiteral $ foldl1 op $ map unwrapNum args

unwrapNum :: Expr -> Integer
unwrapNum (IntLiteral n) = n
unwrapNum _              = undefined