WIP type-checking
[tiger.ml.git] / compiler / src / lib / tiger / tiger_error.ml
1 module Abs = Tiger_absyn
2 module Pos = Tiger_position
3 module Sym = Tiger_symbol
4 module Typ = Tiger_env_type
5
6 type t =
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 | Wrong_type of
15 { expected : Typ.t
16 ; given : Typ.t
17 ; pos : Pos.t
18 }
19 | Wrong_type_of_expression_in_var_dec of
20 { var_id : Sym.t
21 ; expected : Typ.t
22 ; given : Typ.t
23 ; pos : Pos.t
24 }
25 | Wrong_type_used_as_record of
26 { ty_id : Sym.t
27 ; ty : Typ.t
28 ; pos : Pos.t
29 }
30 | Wrong_type_of_field_value of
31 { field_id : Sym.t
32 ; expected : Typ.t
33 ; given : Typ.t
34 ; pos : Pos.t
35 }
36 | Wrong_type_of_arg of
37 { func : Sym.t
38 ; expected : Typ.t
39 ; given : Typ.t
40 ; pos : Pos.t
41 }
42 | Wrong_number_of_args of
43 { func : Sym.t
44 ; expected : int
45 ; given : int
46 ; pos : Pos.t
47 }
48 | Invalid_operand_type of
49 { oper : Abs.oper
50 ; valid : string list
51 ; given : Typ.t
52 ; pos : Pos.t
53 }
54 | Different_operand_types of
55 { oper : Abs.oper
56 ; left : Typ.t
57 ; right : Typ.t
58 ; pos : Pos.t
59 }
60
61 exception T of t
62
63 let raise t =
64 raise (T t)
65
66 let to_string =
67 let s = Printf.sprintf in
68 function
69 | Invalid_syntax pos ->
70 s "Invalid syntax in %s" (Pos.to_string pos)
71 | Unknown_id {id; pos} ->
72 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
73 | Unknown_type {ty_id; pos} ->
74 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
75 | Id_is_a_function {id; pos} ->
76 s "Identifier %S is a function, it cannot be used as a variable in %s"
77 (Sym.to_string id) (Pos.to_string pos)
78 | Id_not_a_function {id; pos} ->
79 s "Identifier %S is not a function, it cannot be called in %s"
80 (Sym.to_string id) (Pos.to_string pos)
81 | No_such_field_in_record {field; record; pos} ->
82 s "No field %S in record %S in %s"
83 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
84 | Exp_not_a_record {ty; pos} ->
85 s ( "This expression has type %S, it is not a record, it cannot be"
86 ^^"accessed in %s")
87 (Typ.to_string ty) (Pos.to_string pos)
88 | Wrong_type {expected; given; pos} ->
89 s "Type error: expected: %S, but given: %S, in %s"
90 (Typ.to_string expected)
91 (Typ.to_string given)
92 (Pos.to_string pos)
93 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
94 s ( "Wrong type of expression in declaration of %S. "
95 ^^"Expected: %S, given: %S. In %s")
96 (Sym.to_string var_id)
97 (Typ.to_string expected)
98 (Typ.to_string given)
99 (Pos.to_string pos)
100 | Wrong_type_used_as_record {ty_id; ty; pos} ->
101 s ( "Identifier %S is bound to type %S, not a record. "
102 ^^"It cannot be used in %s")
103 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
104 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
105 s ( "Field %S is declared to be of type %S, but is bound to expression "
106 ^^"of type %S in %s")
107 (Sym.to_string field_id)
108 (Typ.to_string expected)
109 (Typ.to_string given)
110 (Pos.to_string pos)
111 | Wrong_type_of_arg {func; expected; given; pos} ->
112 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
113 ^^" in %s")
114 (Sym.to_string func)
115 (Typ.to_string expected)
116 (Typ.to_string given)
117 (Pos.to_string pos)
118 | Wrong_number_of_args {func; expected; given; pos} ->
119 s ( "Incorrect number of arguments to function %S, "
120 ^^"expected: %d, given: %d,"
121 ^^" in %s")
122 (Sym.to_string func) expected given (Pos.to_string pos)
123 | Invalid_operand_type {oper; valid; given; pos} ->
124 s ( "Invalid operand type %S for operator %S, which expects only: %s"
125 ^^". In %s")
126 (Typ.to_string given)
127 (Abs.op_show oper)
128 (String.concat ", " valid)
129 (Pos.to_string pos)
130 | Different_operand_types {oper; left; right; pos} ->
131 s "Operands of different types (%S %S %S) given in %s"
132 (Typ.to_string left)
133 (Abs.op_show oper)
134 (Typ.to_string right)
135 (Pos.to_string pos)
136
137 let is_unknown_id t =
138 match t with
139 | Unknown_id _ ->
140 true
141 | Invalid_syntax _
142 | Unknown_type _
143 | Id_is_a_function _
144 | Id_not_a_function _
145 | No_such_field_in_record _
146 | Exp_not_a_record _
147 | Wrong_type _
148 | Wrong_type_of_expression_in_var_dec _
149 | Wrong_type_used_as_record _
150 | Wrong_type_of_field_value _
151 | Wrong_type_of_arg _
152 | Wrong_number_of_args _
153 | Invalid_operand_type _
154 | Different_operand_types _ ->
155 false
This page took 0.050136 seconds and 4 git commands to generate.