1
- type binop =
2
- | Badd | Bsub | Bmul | Bdiv | Bmod
3
- | Bgt | Blt | Bge | Ble | Bequal | Bnotequal
4
- | Band | Bor
5
-
6
- type unop =
7
- | Uplus | Uminus | Unot | Uincre | Udecre
8
-
9
- type postfix =
10
- | Pincre | Pdecre
11
-
12
- type expression =
13
- | Int of int
14
- | Var of string
15
- | Bool of bool
16
- | Null
17
- | String of string
18
- | Assign of string * expression
19
- | Binop of binop * expression * expression
20
- | Unop of unop * expression
21
- | Postop of expression * postfix
22
- | Semi of expression
23
- | IfThen of expression * expression
24
- | IfThenElse of expression * expression * expression
25
-
26
-
27
- exception Unbound_variable of string
28
-
29
- let get_op_u = function
30
- | Uplus -> fun x -> x
31
- | Uminus -> fun x -> - x
32
- (* | Unot -> fun x -> not x *)
33
- | Uincre -> fun x -> x + 1
34
- | Udecre -> fun x -> x - 1
35
-
36
- let get_op_b op x y =
37
- match op with
38
- | Badd -> x + y
39
- | Bsub -> x - y
40
- | Bmul -> x * y
41
- | Bdiv -> x / y
42
- | Bmod -> x mod y
43
-
44
- let string_of_op_u = function
45
- | Uplus -> " +"
46
- | Uminus -> " -"
47
- | Unot -> " not"
48
- | Uincre -> " ++"
49
- | Udecre -> " --"
50
-
51
- let string_of_op_p = function
52
- | Pincre -> " ++"
53
- | Pdecre -> " --"
54
-
55
- let string_of_op_b = function
56
- | Badd -> " +"
57
- | Bsub -> " -"
58
- | Bmul -> " *"
59
- | Bdiv -> " /"
60
- | Bmod -> " %"
61
- | Bgt -> " > "
62
- | Blt -> " < "
63
- | Bge -> " >= "
64
- | Ble -> " <= "
65
- | Bequal -> " = "
66
- | Bnotequal -> " != "
67
- | Band -> " && "
68
- | Bor -> " || "
69
-
70
- let rec string_of_expr exp =
71
- match exp with
72
- | Int c -> " Int(" ^ string_of_int c^ " )"
73
- | Var v -> " Var(" ^ v^ " )"
74
- | Bool b -> string_of_bool b
75
- | Null -> " null"
76
- | String s -> " String(" ^ s^ " )"
77
- | Binop (op , e1 , e2 ) -> " (" ^ (string_of_expr e1)^ (string_of_op_b op) ^ (string_of_expr e2)^ " )"
78
- | Unop (op , e ) -> " (" ^ (string_of_op_u op) ^ (string_of_expr e)^ " )"
79
- | Postop (e , op ) -> " (" ^ (string_of_expr e) ^ (string_of_op_p op)^ " )"
80
- | Assign (s ,e ) -> " Assign(" ^ s^ " =" ^ (string_of_expr e)^ " )"
81
- | Semi (e1 ) -> (string_of_expr e1)^ " ;\n "
82
- | IfThen (e1 ,e2 ) -> " If(" ^ (string_of_expr e1)^ " ) {\n " ^ (string_of_expr e2 )^ " })\n "
83
- | IfThenElse (e1 ,e2 ,e3 ) -> " If(" ^ (string_of_expr e1)^ " ) {\n " ^ (string_of_expr e2 )^ " } Else {\n " ^ (string_of_expr e3)^ " })\n "
84
-
1
+ type binop =
2
+ | Badd | Bsub | Bmul | Bdiv | Bmod
3
+ | Bgt | Blt | Bge | Ble | Bequal | Bnotequal
4
+ | Band | Bor
5
+
6
+ type unop =
7
+ | Uplus | Uminus | Unot | Uincre | Udecre
8
+
9
+ type postfix =
10
+ | Pincre | Pdecre
11
+
12
+ type expression =
13
+ | Int of int
14
+ | Var of string
15
+ | Bool of bool
16
+ | Null
17
+ | String of string
18
+ | Assign of string * expression
19
+ | Binop of binop * expression * expression
20
+ | Unop of unop * expression
21
+ | Postop of expression * postfix
22
+ | Semi of expression
23
+ | IfThen of expression * expression
24
+ | IfThenElse of expression * expression * expression
25
+
26
+ type attr =
27
+ | Attr of string
28
+ | AttrWithAssign of string
29
+
30
+ type attr_or_method =
31
+ | Attribute of attr
32
+
33
+ type class_ =
34
+ | Class_ of string * attr_or_method list
35
+
36
+ type class_or_expr =
37
+ | Class of class_
38
+ | Expr of expression
39
+
40
+ exception Unbound_variable of string
41
+
42
+ let get_op_u = function
43
+ | Uplus -> fun x -> x
44
+ | Uminus -> fun x -> - x
45
+ (* | Unot -> fun x -> not x *)
46
+ | Uincre -> fun x -> x + 1
47
+ | Udecre -> fun x -> x - 1
48
+
49
+ let get_op_b op x y =
50
+ match op with
51
+ | Badd -> x + y
52
+ | Bsub -> x - y
53
+ | Bmul -> x * y
54
+ | Bdiv -> x / y
55
+ | Bmod -> x mod y
56
+
57
+ let string_of_op_u = function
58
+ | Uplus -> " +"
59
+ | Uminus -> " -"
60
+ | Unot -> " not"
61
+ | Uincre -> " ++"
62
+ | Udecre -> " --"
63
+
64
+ let string_of_op_p = function
65
+ | Pincre -> " ++"
66
+ | Pdecre -> " --"
67
+
68
+ let string_of_op_b = function
69
+ | Badd -> " +"
70
+ | Bsub -> " -"
71
+ | Bmul -> " *"
72
+ | Bdiv -> " /"
73
+ | Bmod -> " %"
74
+ | Bgt -> " > "
75
+ | Blt -> " < "
76
+ | Bge -> " >= "
77
+ | Ble -> " <= "
78
+ | Bequal -> " = "
79
+ | Bnotequal -> " != "
80
+ | Band -> " && "
81
+ | Bor -> " || "
82
+
83
+ let rec string_of_expr exp =
84
+ match exp with
85
+ | Int c -> " Int(" ^ string_of_int c^ " )"
86
+ | Var v -> " Var(" ^ v^ " )"
87
+ | Bool b -> string_of_bool b
88
+ | Null -> " null"
89
+ | String s -> " String(" ^ s^ " )"
90
+ | Binop (op , e1 , e2 ) -> " (" ^ (string_of_expr e1)^ (string_of_op_b op) ^ (string_of_expr e2)^ " )"
91
+ | Unop (op , e ) -> " (" ^ (string_of_op_u op) ^ (string_of_expr e)^ " )"
92
+ | Postop (e , op ) -> " (" ^ (string_of_expr e) ^ (string_of_op_p op)^ " )"
93
+ | Assign (s ,e ) -> " Assign(" ^ s^ " =" ^ (string_of_expr e)^ " )"
94
+ | Semi (e1 ) -> (string_of_expr e1)^ " ;\n "
95
+ | IfThen (e1 ,e2 ) -> " If(" ^ (string_of_expr e1)^ " ) {\n " ^ (string_of_expr e2 )^ " })\n "
96
+ | IfThenElse (e1 ,e2 ,e3 ) -> " If(" ^ (string_of_expr e1)^ " ) {\n " ^ (string_of_expr e2 )^ " } Else {\n " ^ (string_of_expr e3)^ " })\n "
97
+
98
+
99
+
100
+ let string_of_attr = function
101
+ | Attr a -> " Attr" ^ a
102
+ | AttrWithAssign a -> " AttrWithAssign" ^ a
103
+
104
+ let string_of_attr_or_method = function
105
+ | Attribute a -> string_of_attr a
106
+
107
+ let rec string_of_attrs_or_methods = function
108
+ | [] -> " "
109
+ | a ::l -> (string_of_attr_or_method a) ^ " " ^ (string_of_attrs_or_methods l)
110
+
111
+ let string_of_class = function
112
+ | Class_ (id ,am ) -> " Class(" ^ id^ " {" ^ (string_of_attrs_or_methods am) ^ " })"
113
+
114
+ let string_of_class_or_expr = function
115
+ | Class c -> string_of_class c
116
+ | Expr e -> string_of_expr e
0 commit comments