@@ -11,40 +11,97 @@ define Value as Num | Bool
11
11
// Expressions
12
12
// ====================================================================
13
13
term Var(string)
14
- term Sub[Expr,Expr ]
15
- term Add[Expr,Expr ]
16
- term Mul[Expr,Expr ]
17
- term Div[Expr,Expr ]
14
+ term Sub[RExpr,RExpr ]
15
+ term Add[RExpr,RExpr ]
16
+ term Mul[RExpr,RExpr ]
17
+ term Div[RExpr,RExpr ]
18
18
19
19
define Expr as Value | Var | Sub | Add | Mull | Div
20
+ define RExpr as Expr | State[Env,Expr]
21
+
22
+ // R-Var
23
+ reduce State[Env{[Var v1, Value r], [Var,Value]...}, Var v2]:
24
+ => r, if v1 == v2
25
+
26
+ // R-Add
27
+ reduce State[Env e, Add[Num(int n1),Num(int n2)]]:
28
+ => Num(n1+n2)
29
+
30
+ reduce State[Env e, Add[Expr e1, Expr e2]]:
31
+ => State[e,Add[State[e,e1],e2]], if !(e1 is Value)
32
+
33
+ reduce State[Env e, Add[Num n, Expr e2]]:
34
+ => State[e,Add[n,State[e,e2]]], if !(e2 is Value)
35
+
36
+ // R-Sub
37
+ reduce State[Env e, Sub[Num(int n1),Num(int n2)]]:
38
+ => Num(n1-n2)
39
+
40
+ reduce State[Env e, Sub[Expr e1, Expr e2]]:
41
+ => State[e,Sub[State[e,e1],e2]], if !(e1 is Value)
42
+
43
+ reduce State[Env e, Sub[Num n, Expr e2]]:
44
+ => State[e,Sub[n,State[e,e2]]], if !(e2 is Value)
45
+
46
+ // R-Mul
47
+ reduce State[Env e, Mul[Num(int n1),Num(int n2)]]:
48
+ => Num(n1 * n2)
49
+
50
+ reduce State[Env e, Mul[Expr e1, Expr e2]]:
51
+ => State[e,Mul[State[e,e1],e2]], if !(e1 is Value)
52
+
53
+ reduce State[Env e, Mul[Num n, Expr e2]]:
54
+ => State[e,Mul[n,State[e,e2]]], if !(e2 is Value)
55
+
56
+ // R-Div
57
+ reduce State[Env e, Div[Num(int n1),Num(int n2)]]:
58
+ => Num(n1/n2), if n2 != 0
59
+
60
+ reduce State[Env e, Div[Expr e1, Expr e2]]:
61
+ => State[e,Div[State[e,e1],e2]], if !(e1 is Value)
62
+
63
+ reduce State[Env e, Div[Num n, Expr e2]]:
64
+ => State[e,Div[n,State[e,e2]]], if !(e2 is Value)
20
65
21
66
// ====================================================================
22
67
// Statements
23
68
// ====================================================================
24
- term Assign[Var,Expr]
25
- term Return(Expr)
69
+ term Assign[Var,RExpr]
70
+ term Return(RExpr)
71
+ term Skip
72
+
73
+ define Stmt as Skip | Assign | Return
74
+ define RStmt as Stmt | State[Env,Stmt]
75
+
76
+ // R-Return1,2
77
+ reduce State[Env e, Return(Expr r)]:
78
+ => State[e,r], if r is Value
79
+ => State[e,Return(State[e,r])]
80
+
81
+ // R-Assign1
82
+ reduce State[Env{[Var,Value]... ps}, Assign[Var v, Value n]]:
83
+ => let ws = { [p[0],p[1]] | p in ps, p[0] != v }
84
+ in State[Env(ws ++ {[v,n]}), Skip]
26
85
27
- define Stmt as Assign | Return
86
+ // R-Assign2
87
+ reduce State[Env e, Assign[Var v, Expr r]]:
88
+ => State[e,Assign[v,State[e,r]]], if !(r is Value)
28
89
29
90
// ====================================================================
30
- // Semantics
91
+ // Operational Semantics
31
92
// ====================================================================
32
93
term Env{[Var,Value]...}
33
94
term Program[Stmt...]
95
+ define Reducible as Program|Stmt|Expr
34
96
35
- term State[Env,Program ]
97
+ term State[Env,Reducible ]
36
98
37
- // R-Return1
38
- reduce State[Env, Program[Return(Value v),Stmt... rest]]:
39
- => v
40
-
41
- // R-Return2 ?
42
-
43
- // R-Assign1
44
- reduce State[Env{[Var,Value]... ps}, Program[Assign[Var v, Value n],Stmt... rest]]:
45
- => let ws = { [p[0],p[1]] | p in ps, p[0] != v }
46
- in State[Env(ws ++ {[v,n]}), rest]
99
+ // Block Reductions
100
+ reduce State[Env e, Program[Stmt s,Stmt... rest]]:
101
+ => State[e,rest], if s is Skip
102
+ => State[e, Program([State[e,s]] ++ rest)]
47
103
48
- // R-Assign2 ?
104
+ reduce State[Env, Program[State[Env,Value v], Stmt... rest]]:
105
+ => v
49
106
50
107
0 commit comments