1 module Abs = Tiger_absyn
2 module Pos = Tiger_position
3 module Sym = Tiger_symbol
4 module Typ = Tiger_env_type
7 | Invalid_syntax of Pos.t
8 | Unknown_id of {id : Sym.t; pos : Pos.t}
9 | Unknown_type of {ty_id : Sym.t; pos : Pos.t}
10 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
11 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
12 | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t}
13 | Exp_not_a_record of {ty : Typ.t; pos : Pos.t}
14 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
20 | Wrong_type_of_expression_in_var_dec of
26 | Wrong_type_used_as_record of
31 | Wrong_type_used_as_array of
36 | Wrong_type_of_field_value of
42 | Wrong_type_of_arg of
48 | Wrong_number_of_args of
54 | Invalid_operand_type of
60 | Different_operand_types of
73 let s = Printf.sprintf in
75 | Invalid_syntax pos ->
76 s "Invalid syntax in %s" (Pos.to_string pos)
77 | Unknown_id {id; pos} ->
78 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
79 | Unknown_type {ty_id; pos} ->
80 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
81 | Id_is_a_function {id; pos} ->
82 s "Identifier %S is a function, it cannot be used as a variable in %s"
83 (Sym.to_string id) (Pos.to_string pos)
84 | Id_not_a_function {id; pos} ->
85 s "Identifier %S is not a function, it cannot be called in %s"
86 (Sym.to_string id) (Pos.to_string pos)
87 | No_such_field_in_record {field; record; pos} ->
88 s "No field %S in record %S in %s"
89 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
90 | Exp_not_a_record {ty; pos} ->
91 s ( "The expression of type %S is not a record, it cannot be"
93 (Typ.to_string ty) (Pos.to_string pos)
94 | Exp_not_an_array {ty; pos} ->
95 s ( "The expression of type %S is not an array, it cannot be"
97 (Typ.to_string ty) (Pos.to_string pos)
98 | Wrong_type {expected; given; pos} ->
99 s "Type error: expected: %S, but given: %S, in %s"
100 (Typ.to_string expected)
101 (Typ.to_string given)
103 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
104 s ( "Wrong type of expression in declaration of %S. "
105 ^^"Expected: %S, given: %S. In %s")
106 (Sym.to_string var_id)
107 (Typ.to_string expected)
108 (Typ.to_string given)
110 | Wrong_type_used_as_array {ty_id; ty; pos} ->
111 s ( "Identifier %S is bound to type %S, not an array. "
112 ^^"It cannot be used in %s")
113 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
114 | Wrong_type_used_as_record {ty_id; ty; pos} ->
115 s ( "Identifier %S is bound to type %S, not a record. "
116 ^^"It cannot be used in %s")
117 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
118 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
119 s ( "Field %S is declared to be of type %S, but is bound to expression "
120 ^^"of type %S in %s")
121 (Sym.to_string field_id)
122 (Typ.to_string expected)
123 (Typ.to_string given)
125 | Wrong_type_of_arg {func; expected; given; pos} ->
126 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
129 (Typ.to_string expected)
130 (Typ.to_string given)
132 | Wrong_number_of_args {func; expected; given; pos} ->
133 s ( "Incorrect number of arguments to function %S, "
134 ^^"expected: %d, given: %d,"
136 (Sym.to_string func) expected given (Pos.to_string pos)
137 | Invalid_operand_type {oper; valid; given; pos} ->
138 s ( "Invalid operand type %S for operator %S, which expects only: %s"
140 (Typ.to_string given)
142 (String.concat ", " valid)
144 | Different_operand_types {oper; left; right; pos} ->
145 s "Operands of different types (%S %S %S) given in %s"
148 (Typ.to_string right)
151 let is_unknown_id t =
158 | Id_not_a_function _
159 | No_such_field_in_record _
163 | Wrong_type_of_expression_in_var_dec _
164 | Wrong_type_used_as_array _
165 | Wrong_type_used_as_record _
166 | Wrong_type_of_field_value _
167 | Wrong_type_of_arg _
168 | Wrong_number_of_args _
169 | Invalid_operand_type _
170 | Different_operand_types _ ->
173 let is_unknown_type t =
180 | Id_not_a_function _
181 | No_such_field_in_record _
185 | Wrong_type_of_expression_in_var_dec _
186 | Wrong_type_used_as_array _
187 | Wrong_type_used_as_record _
188 | Wrong_type_of_field_value _
189 | Wrong_type_of_arg _
190 | Wrong_number_of_args _
191 | Invalid_operand_type _
192 | Different_operand_types _ ->
195 let is_wrong_type t =
203 | Id_not_a_function _
204 | No_such_field_in_record _
207 | Wrong_type_of_expression_in_var_dec _
208 | Wrong_type_used_as_array _
209 | Wrong_type_used_as_record _
210 | Wrong_type_of_field_value _
211 | Wrong_type_of_arg _
212 | Wrong_number_of_args _
213 | Invalid_operand_type _
214 | Different_operand_types _ ->