-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpret.ml
75 lines (64 loc) · 1.77 KB
/
interpret.ml
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
open Error
type value =
| Vbase of const (* Les types de base du language *)
| Vclass of class_expr (* Les classes utilisateurs *)
let get_int = function
| Vbase (Cint i) -> i
| _ -> raise Interpretation_error
let get_bool = function
| Vbase (Cbool b) -> b
| _ -> raise Interpretation_error
let get_string = function
| Vbase (Cstring s) -> s
| _ -> raise Interpretation_error
let unop e = function
| Unot -> let b = get_bool (interpret_expr e) in Vbase (Cbool not b)
| Uminus -> let i = get_int (interpret_expr e) in Vbase (Cint -i)
and binop e1 e2 = function
| Add | Sub | Mult | Div | Mod as op ->
let i1 = get_int (interpret_expr e1)
and i2 = get_int (interpret_expr e2)
and f = function
| Add -> (+) | Sub -> (-) | Mult -> ( * ) | Div -> (/) | Mod -> (mod)
in
f op i1 i2
| Lt | Le | Gt | Ge as op ->
let i1 = interpret_expr e1
and i2 = interpret_expr e2
and f = function
| Lt -> (<)
| Le -> (<=)
| Gt -> (>)
| Ge -> (>=)
in
f op i1 i2
| Eq | Neq as op ->
let v1 = interpret_expr e1
and v2 = interpret_expr e2
and f = function
| Eq -> (=)
| Neq -> (<>)
in
f op v1 v2
| And ->
let b = get_bool (interpret_expr e1) in
if not b
then Vbase (Cbool false)
else
let b = get_bool (interpret_expr e2) in
if not b
then Vbase (Cbool false)
else Vbase (Cbool true)
| Or ->
let b = get_bool (interpret_expr e1) in
if b
then Vbase (Cbool true)
else
let b = get_bool (interpret_expr e2) in
if b
then Vbase (Cbool true)
else Vbase (Cbool false)
and interpret_expr env = function
| Const c -> Vconst c
| Unop (op, e) -> unop e op
| Binop (op, e1, e2) -> binop e1 e2 op