@@ -12,6 +12,9 @@ use crate::persistent_vector::{PersistentVector, ToPersistentVectorIter};
12
12
use crate :: symbol:: Symbol ;
13
13
use crate :: value:: { Evaluable , ToValue } ;
14
14
15
+ use crate :: error_message;
16
+ use crate :: type_tag:: TypeTag ;
17
+
15
18
//
16
19
// This module will hold the core functions and macros that Clojure will
17
20
// hook into; Functions / Macros like "+", "fn*", "let", "cond", etc
@@ -72,12 +75,12 @@ impl IFn for AddFn {
72
75
args. into_iter ( ) . fold ( 0_i32 . to_value ( ) , |a, b| match a {
73
76
Value :: I32 ( a_) => match b {
74
77
Value :: I32 ( b_) => Value :: I32 ( a_ + b_) ,
75
- _ => Value :: Condition ( format ! (
78
+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
76
79
"Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
77
80
b. type_tag( )
78
81
) ) ,
79
82
} ,
80
- _ => Value :: Condition ( format ! (
83
+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
81
84
"Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
82
85
a. type_tag( )
83
86
) ) ,
@@ -105,11 +108,7 @@ impl IFn for EvalFn {
105
108
fn invoke ( & self , args : Vec < & Value > ) -> Value {
106
109
// @TODO generalize arity exceptions, and other exceptions
107
110
if args. len ( ) != 1 {
108
- return Value :: Condition ( format ! (
109
- "Wrong number of arguments given to function (Given: {}, Expected: {})" ,
110
- args. len( ) ,
111
- args. len( )
112
- ) ) ;
111
+ return error_message:: wrong_arg_count ( 1 , args. len ( ) )
113
112
}
114
113
let arg = args. get ( 0 ) . unwrap ( ) ;
115
114
arg. eval ( Rc :: clone ( & self . enclosing_environment ) )
@@ -178,58 +177,39 @@ impl IFn for NthFn {
178
177
fn invoke ( & self , args : Vec < & Value > ) -> Value {
179
178
// @TODO generalize arity exceptions, and other exceptions
180
179
if args. len ( ) != 2 {
181
- return Value :: Condition ( format ! (
182
- "Wrong number of arguments (Given: {}, Expected: 1-2)" ,
183
- args. len( )
184
- ) ) ;
180
+ return error_message:: wrong_arg_count ( 2 , args. len ( ) )
185
181
}
186
182
// @TODO change iteration to work with Value references, or even change invoke to work on Rc<..>
187
183
// as we do everything else; surely we don't want to clone just to read from a collection
188
184
if let Some ( Value :: I32 ( ind) ) = args. get ( 1 ) {
189
185
if * ind < 0 {
190
- return Value :: Condition ( format ! ( "Index cannot be negative; Index ({})" , ind ) ) ;
186
+ return error_message :: index_cannot_be_negative ( * ind as usize )
191
187
}
192
188
let ind = * ind as usize ;
193
189
194
190
match args. get ( 0 ) . unwrap ( ) {
195
191
Value :: PersistentList ( Cons ( head, tail, count) ) => {
196
192
let count = * count as usize ;
197
193
if ind >= count {
198
- Value :: Condition ( format ! (
199
- "Index out of bounds: Index ({}), Length: ({})" ,
200
- ind, count
201
- ) )
194
+ error_message:: index_out_of_bounds ( ind, count)
202
195
} else if ind == 0 {
203
196
head. to_value ( )
204
197
} else {
205
198
tail. iter ( ) . nth ( ind - 1 ) . unwrap ( ) . to_value ( )
206
199
}
207
200
}
208
- Value :: PersistentList ( Empty ) => Value :: Condition ( format ! (
209
- "Index out of bounds: Index ({}), Length: ({})" ,
210
- ind, 0
211
- ) ) ,
201
+ Value :: PersistentList ( Empty ) => error_message:: index_out_of_bounds ( ind, 0 ) ,
212
202
Value :: PersistentVector ( PersistentVector { vals } ) => {
213
203
if ind >= vals. len ( ) {
214
- Value :: Condition ( format ! (
215
- "Index out of bounds: Index ({}), Length: ({})" ,
216
- ind,
217
- vals. len( )
218
- ) )
204
+ error_message:: index_out_of_bounds ( ind, vals. len ( ) )
219
205
} else {
220
206
vals. get ( ind) . unwrap ( ) . to_value ( )
221
207
}
222
208
}
223
- _ => Value :: Condition ( format ! (
224
- "Type mismatch; Expected instance of clojure.lang.ISeq, Recieved type {}" ,
225
- args. get( 0 ) . unwrap( ) . type_tag( )
226
- ) ) ,
209
+ _ => error_message:: type_mismatch ( TypeTag :: ISeq , args. get ( 0 ) ) ,
227
210
}
228
211
} else {
229
- Value :: Condition ( format ! (
230
- "Type mismatch; Expected instance of clojure.lang.Integer, Recieved type {}" ,
231
- args. get( 1 ) . unwrap( ) . type_tag( )
232
- ) )
212
+ error_message:: type_mismatch ( TypeTag :: Integer , args. get ( 0 ) )
233
213
}
234
214
}
235
215
}
@@ -273,11 +253,7 @@ impl ToValue for PrintStringFn {
273
253
impl IFn for PrintStringFn {
274
254
fn invoke ( & self , args : Vec < & Value > ) -> Value {
275
255
if args. len ( ) != 1 {
276
- return Value :: Condition ( format ! (
277
- "Wrong number of arguments given to function (Given: {}, Expected: {})" ,
278
- args. len( ) ,
279
- args. len( )
280
- ) ) ;
256
+ return error_message:: wrong_arg_count ( 1 , args. len ( ) )
281
257
}
282
258
println ! ( "{}" , args. get( 0 ) . unwrap( ) . to_string( ) ) ;
283
259
Value :: Nil
0 commit comments