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_of_field_value of
37 | Wrong_type_of_arg of
43 | Wrong_number_of_args of
49 | Invalid_operand_type of
55 | Different_operand_types of
68 let s = Printf.sprintf in
70 | Invalid_syntax pos ->
71 s "Invalid syntax in %s" (Pos.to_string pos)
72 | Unknown_id {id; pos} ->
73 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
74 | Unknown_type {ty_id; pos} ->
75 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
76 | Id_is_a_function {id; pos} ->
77 s "Identifier %S is a function, it cannot be used as a variable in %s"
78 (Sym.to_string id) (Pos.to_string pos)
79 | Id_not_a_function {id; pos} ->
80 s "Identifier %S is not a function, it cannot be called in %s"
81 (Sym.to_string id) (Pos.to_string pos)
82 | No_such_field_in_record {field; record; pos} ->
83 s "No field %S in record %S in %s"
84 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
85 | Exp_not_a_record {ty; pos} ->
86 s ( "The expression of type %S is not a record, it cannot be"
88 (Typ.to_string ty) (Pos.to_string pos)
89 | Exp_not_an_array {ty; pos} ->
90 s ( "The expression of type %S is not an array, it cannot be"
92 (Typ.to_string ty) (Pos.to_string pos)
93 | Wrong_type {expected; given; pos} ->
94 s "Type error: expected: %S, but given: %S, in %s"
95 (Typ.to_string expected)
98 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
99 s ( "Wrong type of expression in declaration of %S. "
100 ^^"Expected: %S, given: %S. In %s")
101 (Sym.to_string var_id)
102 (Typ.to_string expected)
103 (Typ.to_string given)
105 | Wrong_type_used_as_record {ty_id; ty; pos} ->
106 s ( "Identifier %S is bound to type %S, not a record. "
107 ^^"It cannot be used in %s")
108 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
109 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
110 s ( "Field %S is declared to be of type %S, but is bound to expression "
111 ^^"of type %S in %s")
112 (Sym.to_string field_id)
113 (Typ.to_string expected)
114 (Typ.to_string given)
116 | Wrong_type_of_arg {func; expected; given; pos} ->
117 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
120 (Typ.to_string expected)
121 (Typ.to_string given)
123 | Wrong_number_of_args {func; expected; given; pos} ->
124 s ( "Incorrect number of arguments to function %S, "
125 ^^"expected: %d, given: %d,"
127 (Sym.to_string func) expected given (Pos.to_string pos)
128 | Invalid_operand_type {oper; valid; given; pos} ->
129 s ( "Invalid operand type %S for operator %S, which expects only: %s"
131 (Typ.to_string given)
133 (String.concat ", " valid)
135 | Different_operand_types {oper; left; right; pos} ->
136 s "Operands of different types (%S %S %S) given in %s"
139 (Typ.to_string right)
142 let is_unknown_id t =
149 | Id_not_a_function _
150 | No_such_field_in_record _
154 | Wrong_type_of_expression_in_var_dec _
155 | Wrong_type_used_as_record _
156 | Wrong_type_of_field_value _
157 | Wrong_type_of_arg _
158 | Wrong_number_of_args _
159 | Invalid_operand_type _
160 | Different_operand_types _ ->