@@ -10,12 +10,12 @@ use crate::persistent_list::{
1010} ;
1111use crate :: persistent_list_map:: IPersistentListMap ;
1212use crate :: persistent_vector:: { PersistentVector , ToPersistentVectorIter } ;
13- use crate :: repl:: Repl ;
1413use crate :: symbol:: Symbol ;
1514use crate :: type_tag:: TypeTag ;
1615use crate :: value:: { Evaluable , ToValue } ;
1716
1817use itertools:: Itertools ;
18+ use crate :: error_message;
1919
2020use crate :: util:: IsEven ;
2121
@@ -70,12 +70,12 @@ impl IFn for AddFn {
7070 args. into_iter ( ) . fold ( 0_i32 . to_value ( ) , |a, b| match a {
7171 Value :: I32 ( a_) => match * b {
7272 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?
7474 "Type mismatch; Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
7575 b. type_tag( )
7676 ) ) ,
7777 } ,
78- _ => Value :: Condition ( format ! (
78+ _ => Value :: Condition ( format ! ( // TODO: what error message should be returned regarding using typetags?
7979 "Type mismatch: Expecting: (i32 | i64 | f32 | f64), Found: {}" ,
8080 a. type_tag( )
8181 ) ) ,
@@ -103,11 +103,7 @@ impl IFn for EvalFn {
103103 fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
104104 // @TODO generalize arity exceptions, and other exceptions
105105 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 ( ) )
111107 }
112108 let arg = args. get ( 0 ) . unwrap ( ) ;
113109 arg. eval ( Rc :: clone ( & self . enclosing_environment ) )
@@ -176,58 +172,39 @@ impl IFn for NthFn {
176172 fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
177173 // @TODO generalize arity exceptions, and other exceptions
178174 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 ( ) )
183176 }
184177 // @TODO change iteration to work with Value references, or even change invoke to work on Rc<..>
185178 // as we do everything else; surely we don't want to clone just to read from a collection
186179 if let Value :: I32 ( ind) = * * args. get ( 1 ) . unwrap ( ) {
187180 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 )
189182 }
190183 let ind = ind as usize ;
191184
192185 match & * * args. get ( 0 ) . unwrap ( ) {
193186 Value :: PersistentList ( Cons ( head, tail, count) ) => {
194187 let count = * count as usize ;
195188 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)
200190 } else if ind == 0 {
201191 head. to_value ( )
202192 } else {
203193 tail. iter ( ) . nth ( ind - 1 ) . unwrap ( ) . to_value ( )
204194 }
205195 }
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 ) ,
210197 Value :: PersistentVector ( PersistentVector { vals } ) => {
211198 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 ( ) )
217200 } else {
218201 vals. get ( ind) . unwrap ( ) . to_value ( )
219202 }
220203 }
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 ( ) ) ,
225205 }
226206 } 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 ( ) )
231208 }
232209 }
233210}
@@ -271,11 +248,7 @@ impl ToValue for PrintStringFn {
271248impl IFn for PrintStringFn {
272249 fn invoke ( & self , args : Vec < Rc < Value > > ) -> Value {
273250 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 ( ) )
279252 }
280253 println ! ( "{}" , args. get( 0 ) . unwrap( ) . to_string( ) ) ;
281254 Value :: Nil
0 commit comments