WIP type-checking -- check record expressions
[tiger.ml.git] / compiler / src / lib / tiger / tiger_error.ml
CommitLineData
c16dd441 1module Abs = Tiger_absyn
7c14a966 2module Pos = Tiger_position
c16dd441
SK
3module Sym = Tiger_symbol
4module Typ = Tiger_env_type
7c14a966 5
c16dd441
SK
6type 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}
523e2b06 10 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
c16dd441 11 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
523e2b06
SK
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}
161a300d 14 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
523e2b06
SK
15 | Wrong_type of
16 { expected : Typ.t
17 ; given : Typ.t
18 ; pos : Pos.t
19 }
c16dd441
SK
20 | Wrong_type_of_expression_in_var_dec of
21 { var_id : Sym.t
22 ; expected : Typ.t
23 ; given : Typ.t
24 ; pos : Pos.t
25 }
26 | Wrong_type_used_as_record of
27 { ty_id : Sym.t
28 ; ty : Typ.t
29 ; pos : Pos.t
30 }
31 | Wrong_type_of_field_value of
32 { field_id : Sym.t
33 ; expected : Typ.t
34 ; given : Typ.t
35 ; pos : Pos.t
36 }
37 | Wrong_type_of_arg of
38 { func : Sym.t
39 ; expected : Typ.t
40 ; given : Typ.t
41 ; pos : Pos.t
42 }
43 | Wrong_number_of_args of
44 { func : Sym.t
45 ; expected : int
46 ; given : int
47 ; pos : Pos.t
48 }
49 | Invalid_operand_type of
50 { oper : Abs.oper
51 ; valid : string list
52 ; given : Typ.t
53 ; pos : Pos.t
54 }
55 | Different_operand_types of
56 { oper : Abs.oper
57 ; left : Typ.t
58 ; right : Typ.t
59 ; pos : Pos.t
60 }
7c14a966 61
c16dd441
SK
62exception T of t
63
64let raise t =
65 raise (T t)
66
67let to_string =
68 let s = Printf.sprintf in
69 function
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)
523e2b06
SK
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)
c16dd441
SK
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)
523e2b06
SK
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} ->
161a300d
SK
86 s ( "The expression of type %S is not a record, it cannot be"
87 ^^"accessed in %s")
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"
523e2b06
SK
91 ^^"accessed in %s")
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)
96 (Typ.to_string given)
97 (Pos.to_string pos)
c16dd441
SK
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)
104 (Pos.to_string pos)
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)
115 (Pos.to_string pos)
116 | Wrong_type_of_arg {func; expected; given; pos} ->
117 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
118 ^^" in %s")
119 (Sym.to_string func)
120 (Typ.to_string expected)
121 (Typ.to_string given)
122 (Pos.to_string pos)
123 | Wrong_number_of_args {func; expected; given; pos} ->
124 s ( "Incorrect number of arguments to function %S, "
125 ^^"expected: %d, given: %d,"
126 ^^" in %s")
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"
130 ^^". In %s")
131 (Typ.to_string given)
132 (Abs.op_show oper)
133 (String.concat ", " valid)
134 (Pos.to_string pos)
135 | Different_operand_types {oper; left; right; pos} ->
136 s "Operands of different types (%S %S %S) given in %s"
137 (Typ.to_string left)
138 (Abs.op_show oper)
139 (Typ.to_string right)
140 (Pos.to_string pos)
5da420a8
SK
141
142let is_unknown_id t =
143 match t with
144 | Unknown_id _ ->
145 true
146 | Invalid_syntax _
147 | Unknown_type _
523e2b06 148 | Id_is_a_function _
5da420a8 149 | Id_not_a_function _
523e2b06
SK
150 | No_such_field_in_record _
151 | Exp_not_a_record _
161a300d 152 | Exp_not_an_array _
523e2b06 153 | Wrong_type _
5da420a8
SK
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 _ ->
161 false
This page took 0.039375 seconds and 4 git commands to generate.