-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.ml
58 lines (53 loc) · 2.38 KB
/
ast.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
type arith =
| AEVar of string
| AENum of int
| AENegate of arith
| AEPlus of arith * arith
| AEMinus of arith * arith
| AEMult of arith * arith
| AEDiv of arith * arith
let rec string_of_arith a =
match a with
| AEVar x -> Printf.sprintf "%s" x
| AENum n -> Printf.sprintf "%s" (string_of_int n)
| AENegate a -> Printf.sprintf "-%s" (string_of_arith a)
| AEPlus (a1, a2) -> Printf.sprintf "(%s + %s)" (string_of_arith a1) (string_of_arith a2)
| AEMinus (a1, a2) -> Printf.sprintf "(%s - %s)" (string_of_arith a1) (string_of_arith a2)
| AEMult (a1, a2) -> Printf.sprintf "(%s * %s)" (string_of_arith a1) (string_of_arith a2)
| AEDiv (a1, a2) -> Printf.sprintf "(%s / %s)" (string_of_arith a1) (string_of_arith a2)
type boolean =
| BETrue
| BEFalse
| BENot of boolean
| BEAnd of boolean * boolean
| BEOr of boolean * boolean
| BELT of arith * arith
| BEGT of arith * arith
| BEEq of arith * arith
let rec string_of_boolean b =
match b with
| BETrue -> Printf.sprintf "true"
| BEFalse -> Printf.sprintf "false"
| BENot b -> Printf.sprintf "not %s" (string_of_boolean b)
| BEAnd (b1, b2) -> Printf.sprintf "(%s and %s)" (string_of_boolean b1) (string_of_boolean b2)
| BEOr (b1, b2) -> Printf.sprintf "(%s or %s)" (string_of_boolean b1) (string_of_boolean b2)
| BELT (a1, a2) -> Printf.sprintf "(%s < %s)" (string_of_arith a1) (string_of_arith a2)
| BEGT (a1, a2) -> Printf.sprintf "(%s > %s)" (string_of_arith a1) (string_of_arith a2)
| BEEq (a1, a2) -> Printf.sprintf "(%s = %s)" (string_of_arith a1) (string_of_arith a2)
type stmt =
| SAssign of string * arith
| SInput of string
| SAssert of boolean
| SSkip
| SSeq of stmt * stmt
| SIf of boolean * stmt * stmt
| SWhile of boolean * stmt
let rec string_of_stmt s =
match s with
| SAssign (x, a) -> Printf.sprintf "%s := %s" x (string_of_arith a)
| SInput x -> Printf.sprintf "input(%s)" x
| SAssert b -> Printf.sprintf "assert %s" (string_of_boolean b)
| SSkip -> Printf.sprintf "skip"
| SSeq (s1, s2) -> Printf.sprintf "%s; %s" (string_of_stmt s1) (string_of_stmt s2)
| SIf (b, s1, s2) -> Printf.sprintf "if %s then %s else %s" (string_of_boolean b) (string_of_stmt s1) (string_of_stmt s2)
| SWhile (b, s) -> Printf.sprintf "while %s do %s" (string_of_boolean b) (string_of_stmt s)