@@ -12,6 +12,9 @@ use crate::persistent_vector::{PersistentVector, ToPersistentVectorIter};
1212use crate :: symbol:: Symbol ;
1313use crate :: value:: { Evaluable , ToValue } ;
1414
15+ use crate :: error_message;
16+ use crate :: type_tag:: TypeTag ;
17+
1518//
1619// This module will hold the core functions and macros that Clojure will
1720// hook into; Functions / Macros like "+", "fn*", "let", "cond", etc
@@ -72,12 +75,12 @@ impl IFn for AddFn {
7275 args. into_iter ( ) . fold ( 0_i32 . to_value ( ) , |a, b| match a {
7376 Value :: I32 ( a_) => match b {
7477 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?
7679 "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
7780 b. type_tag( )
7881 ) ) ,
7982 } ,
80- _ => Value :: Condition ( format ! (
83+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
8184 "Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
8285 a. type_tag( )
8386 ) ) ,
@@ -105,11 +108,7 @@ impl IFn for EvalFn {
105108 fn invoke ( & self , args : Vec < & Value > ) -> Value {
106109 // @TODO generalize arity exceptions, and other exceptions
107110 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 ( ) )
113112 }
114113 let arg = args. get ( 0 ) . unwrap ( ) ;
115114 arg. eval ( Rc :: clone ( & self . enclosing_environment ) )
@@ -178,58 +177,39 @@ impl IFn for NthFn {
178177 fn invoke ( & self , args : Vec < & Value > ) -> Value {
179178 // @TODO generalize arity exceptions, and other exceptions
180179 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 ( ) )
185181 }
186182 // @TODO change iteration to work with Value references, or even change invoke to work on Rc<..>
187183 // as we do everything else; surely we don't want to clone just to read from a collection
188184 if let Some ( Value :: I32 ( ind) ) = args. get ( 1 ) {
189185 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 )
191187 }
192188 let ind = * ind as usize ;
193189
194190 match args. get ( 0 ) . unwrap ( ) {
195191 Value :: PersistentList ( Cons ( head, tail, count) ) => {
196192 let count = * count as usize ;
197193 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)
202195 } else if ind == 0 {
203196 head. to_value ( )
204197 } else {
205198 tail. iter ( ) . nth ( ind - 1 ) . unwrap ( ) . to_value ( )
206199 }
207200 }
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 ) ,
212202 Value :: PersistentVector ( PersistentVector { vals } ) => {
213203 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 ( ) )
219205 } else {
220206 vals. get ( ind) . unwrap ( ) . to_value ( )
221207 }
222208 }
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 ) ) ,
227210 }
228211 } 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 ) )
233213 }
234214 }
235215}
@@ -273,11 +253,7 @@ impl ToValue for PrintStringFn {
273253impl IFn for PrintStringFn {
274254 fn invoke ( & self , args : Vec < & Value > ) -> Value {
275255 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 ( ) )
281257 }
282258 println ! ( "{}" , args. get( 0 ) . unwrap( ) . to_string( ) ) ;
283259 Value :: Nil
0 commit comments