diff options
Diffstat (limited to 'src/Operators.hs')
-rw-r--r-- | src/Operators.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/Operators.hs b/src/Operators.hs new file mode 100644 index 0000000..e57f885 --- /dev/null +++ b/src/Operators.hs | |||
@@ -0,0 +1,22 @@ | |||
1 | module Operators ( | ||
2 | primitives | ||
3 | ) where | ||
4 | |||
5 | import Parser | ||
6 | |||
7 | primitives :: [(String, [Expr] -> Expr)] | ||
8 | primitives = | ||
9 | [ | ||
10 | ("+", arithmetic (+)) | ||
11 | , ("-", arithmetic (-)) | ||
12 | , ("*", arithmetic (*)) | ||
13 | , ("/", arithmetic div) | ||
14 | ] | ||
15 | |||
16 | arithmetic :: (Integer -> Integer -> Integer) -> [Expr] -> Expr | ||
17 | arithmetic op args = IntLiteral $ foldl1 op $ map unwrapNum args | ||
18 | |||
19 | unwrapNum :: Expr -> Integer | ||
20 | unwrapNum (IntLiteral n) = n | ||
21 | unwrapNum _ = undefined | ||
22 | |||