about summary refs log tree commit diff
path: root/src/Typecheck.hs
blob: d8b8128ea356826d8c0442a647d14624eb74cba0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module Typecheck
(inferExpr,
 checkExpr)
  where
import Syntax
import Control.Monad
type Environment = [(Idnt,Typ)]

assertInt TInt = Just ()
assertInt _ = Nothing
assertRec (Record _) = Just ()
assertRec _ = Nothing
assertVar (Variant _ _) = Just ()
assertVar _ = Nothing
assertPtr (TPtr _) = Just ()
assertPtr _ = Nothing

inferExpr :: Expr -> Environment -> Maybe Typ
inferExpr (Var name) env = lookup name env
inferExpr (IntLit _) _ = Just TInt
inferExpr (Record _) _ = undefined
inferExpr (Variant _ _) _ = undefined
inferExpr (Add e1 e2) env = do
  checkExpr e1 env TInt
  checkExpr e1 env TInt
  return TInt
inferExpr (Mult e1 e2) env = do
  checkExpr e1 env TInt
  checkExpr e1 env TInt
  return TInt
inferExpr (Neg e) env = do
  checkExpr e env TInt
  return TInt
inferExpr (Deref e1) env = do
  t <- inferExpr e1 env
  assertPtr t
  return t

checkExpr :: Expr -> Environment -> Typ -> Maybe ()
checkExpr e env t = do
  t' <- inferExpr e env
  guard (t==t')
  return ()