@@ -10,12 +10,12 @@ use crate::persistent_list::{
10
10
} ;
11
11
use crate :: persistent_list_map:: IPersistentListMap ;
12
12
use crate :: persistent_vector:: { PersistentVector , ToPersistentVectorIter } ;
13
- use crate :: repl:: Repl ;
14
13
use crate :: symbol:: Symbol ;
15
14
use crate :: type_tag:: TypeTag ;
16
15
use crate :: value:: { Evaluable , ToValue } ;
17
16
18
17
use itertools:: Itertools ;
18
+ use crate :: error_message;
19
19
20
20
use crate :: util:: IsEven ;
21
21
@@ -70,12 +70,12 @@ impl IFn for AddFn {
70
70
args. into_iter ( ) . fold ( 0_i32 . to_value ( ) , |a, b| match a {
71
71
Value :: I32 ( a_) => match * b {
72
72
Value :: I32 ( b_) => Value :: I32 ( a_ + b_) ,
73
- _ => Value :: Condition ( format ! (
73
+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
74
74
"Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
75
75
b. type_tag( )
76
76
) ) ,
77
77
} ,
78
- _ => Value :: Condition ( format ! (
78
+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
79
79
"Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
80
80
a. type_tag( )
81
81
) ) ,
@@ -103,11 +103,7 @@ impl IFn for EvalFn {
103
103
fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
104
104
// @TODO generalize arity exceptions, and other exceptions
105
105
if args. len ( ) != 1 {
106
- return Value :: Condition ( format ! (
107
- "Wrong number of arguments given to function (Given: {}, Expected: {})" ,
108
- args. len( ) ,
109
- args. len( )
110
- ) ) ;
106
+ return error_message:: wrong_arg_count ( 1 , args. len ( ) )
111
107
}
112
108
let arg = args. get ( 0 ) . unwrap ( ) ;
113
109
arg. eval ( Rc :: clone ( & self . enclosing_environment ) )
@@ -176,58 +172,39 @@ impl IFn for NthFn {
176
172
fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
177
173
// @TODO generalize arity exceptions, and other exceptions
178
174
if args. len ( ) != 2 {
179
- return Value :: Condition ( format ! (
180
- "Wrong number of arguments (Given: {}, Expected: 1-2)" ,
181
- args. len( )
182
- ) ) ;
175
+ return error_message:: wrong_varg_count ( & [ 2 , 3 ] , args. len ( ) )
183
176
}
184
177
// @TODO change iteration to work with Value references, or even change invoke to work on Rc<..>
185
178
// as we do everything else; surely we don't want to clone just to read from a collection
186
179
if let Value :: I32 ( ind) = * * args. get ( 1 ) . unwrap ( ) {
187
180
if ind < 0 {
188
- return Value :: Condition ( format ! ( "Index cannot be negative; Index ({})" , ind ) ) ;
181
+ return error_message :: index_cannot_be_negative ( ind as usize )
189
182
}
190
183
let ind = ind as usize ;
191
184
192
185
match & * * args. get ( 0 ) . unwrap ( ) {
193
186
Value :: PersistentList ( Cons ( head, tail, count) ) => {
194
187
let count = * count as usize ;
195
188
if ind >= count {
196
- Value :: Condition ( format ! (
197
- "Index out of bounds: Index ({}), Length: ({})" ,
198
- ind, count
199
- ) )
189
+ error_message:: index_out_of_bounds ( ind, count)
200
190
} else if ind == 0 {
201
191
head. to_value ( )
202
192
} else {
203
193
tail. iter ( ) . nth ( ind - 1 ) . unwrap ( ) . to_value ( )
204
194
}
205
195
}
206
- Value :: PersistentList ( Empty ) => Value :: Condition ( format ! (
207
- "Index out of bounds: Index ({}), Length: ({})" ,
208
- ind, 0
209
- ) ) ,
196
+ Value :: PersistentList ( Empty ) => error_message:: index_out_of_bounds ( ind, 0 ) ,
210
197
Value :: PersistentVector ( PersistentVector { vals } ) => {
211
198
if ind >= vals. len ( ) {
212
- Value :: Condition ( format ! (
213
- "Index out of bounds: Index ({}), Length: ({})" ,
214
- ind,
215
- vals. len( )
216
- ) )
199
+ error_message:: index_out_of_bounds ( ind, vals. len ( ) )
217
200
} else {
218
201
vals. get ( ind) . unwrap ( ) . to_value ( )
219
202
}
220
203
}
221
- _ => Value :: Condition ( format ! (
222
- "Type mismatch; Expected instance of clojure.lang.ISeq, Recieved type {}" ,
223
- args. get( 0 ) . unwrap( ) . type_tag( )
224
- ) ) ,
204
+ _ => error_message:: type_mismatch ( TypeTag :: ISeq , & * * args. get ( 0 ) . unwrap ( ) ) ,
225
205
}
226
206
} else {
227
- Value :: Condition ( format ! (
228
- "Type mismatch; Expected instance of clojure.lang.Integer, Recieved type {}" ,
229
- args. get( 1 ) . unwrap( ) . type_tag( )
230
- ) )
207
+ error_message:: type_mismatch ( TypeTag :: Integer , & * * args. get ( 1 ) . unwrap ( ) )
231
208
}
232
209
}
233
210
}
@@ -271,11 +248,7 @@ impl ToValue for PrintStringFn {
271
248
impl IFn for PrintStringFn {
272
249
fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
273
250
if args. len ( ) != 1 {
274
- return Value :: Condition ( format ! (
275
- "Wrong number of arguments given to function (Given: {}, Expected: {})" ,
276
- args. len( ) ,
277
- args. len( )
278
- ) ) ;
251
+ return error_message:: wrong_arg_count ( 1 , args. len ( ) )
279
252
}
280
253
println ! ( "{}" , args. get( 0 ) . unwrap( ) . to_string( ) ) ;
281
254
Value :: Nil
0 commit comments