summaryrefslogtreecommitdiff
path: root/lec05/Calc.hs
blob: cd6f4087539d6132d9c332a5372541dec99e0b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module Calc where

import ExprT
import Parser

-- Exercise 1

eval :: ExprT -> Integer
eval (Lit n) = n
eval (Add l r) = eval l + eval r
eval (Mul l r) = eval l * eval r

-- Exercise 2

evalStr :: String -> Maybe Integer
evalStr s = case parseExp Lit Add Mul s of
                 Nothing -> Nothing
                 Just t -> Just (eval t)