summaryrefslogtreecommitdiff
path: root/fp2/week45/camil/RefactorX.icl
blob: 9cbb6d77405c9a4d075ed9deffcb0f6993fe6275 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
implementation module RefactorX

import StdEnv

Start = map eval [E1,E2,E3,E4,E5]

E1 = OP (LET "x" (OP (NR 42) MIN (NR 3)) (OP (VAR "x") DIV (NR 0)))
	PLUS
	(LET "y" (NR 6) (OP (VAR "y") MUL (VAR "y")))
E2 = LET "x" (NR 42) (OP (VAR "x") PLUS (LET "x" (NR 58) (VAR "x")))
E3 = LET "x" (NR 1) (LET "y" (NR 2) (LET "x" (NR 3) (NR 4)))
E4 = LET "x" (NR 1) (OP (VAR "x") PLUS (VAR "y"))
E5 = OP (LET "x" (NR 1) (VAR "x")) MUL (VAR "x")

::	Expr							= NR   Int
									| VAR  Name
									| OP   Expr Operator Expr
									| LET  Name     Expr Expr
::	Name							:== String
::	Operator						= PLUS | MIN | MUL | DIV
::	Val								= Result Int | Undef

(<+) infixl 9 :: String a -> String | toString a
(<+) str a = str +++ toString a

instance toString Operator where
	toString PLUS = "+"
	toString MIN = "-"
	toString MUL = "*"
	toString DIV = "/"

//  expressies afdrukken:
instance toString Expr where
	toString (NR n) = toString n
	toString (VAR s) = s
	toString (LET s e1 e2) = "let " <+ s <+ " = " <+ e1 <+ " in " <+ e2
	toString (OP e1 o e2) = bracket e1 <+ o <+ bracket e2
	where
		bracket :: Expr -> String
		bracket (OP e1 o e2) = "(" <+ e1 <+ o <+ e2 <+ ")"
		bracket (LET s e1 e2) = "(" <+ (LET s e1 e2) <+ ")"
		bracket x = toString x

//	vrije variabelen:
free								:: Expr -> [Name]
free (NR _) = []
free (VAR s) = [s]
free (LET s _ e2) = [n \\ n <- free e2 | n <> s]
free (OP e1 _ e2) = removeDup ((free e1) ++ (free e2))

//	verwijder deelexpressies met ongebruikte let-variabelen:
remove_unused_lets					:: Expr -> Expr
remove_unused_lets (LET s e1 e2)
| isMember s (free e2) = (LET s (remove_unused_lets e1) (remove_unused_lets e2))
| otherwise = remove_unused_lets e2
remove_unused_lets (OP e1 o e2) = OP (remove_unused_lets e1) o (remove_unused_lets e2)
remove_unused_lets x = x

//	evaluator met tabel van naam-waarde paren:
eval								:: Expr -> Val
eval e = fst (eval` e [])
where
	eval` :: Expr [(Name, Val)] -> (Val, [(Name, Val)])
	eval` (NR n) vs = (Result n, [])
	eval` (VAR s) vs = (find s vs, [])
	where
		find :: Name [(Name, Val)] -> Val
		find _ [] = Undef
		find s [(t,v):vs]
		| s == t = v
		| otherwise = find s vs
	eval` (LET s e1 e2) vs = eval` e2 [(s,fst (eval` e1 vs)):vs]
	eval` (OP e1 o e2) vs = (op o (fst (eval` e1 vs)) (fst (eval` e2 vs)), [])
	where
		op :: Operator Val Val -> Val
		op _ Undef _ = Undef
		op _ _ Undef = Undef
		op PLUS (Result x) (Result y) = Result (x + y)
		op MIN (Result x) (Result y) = Result (x - y)
		op MUL (Result x) (Result y) = Result (x * y)
		op DIV _ (Result 0) = Undef
		op DIV (Result x) (Result y) = Result (x / y)