Skip to content

Commit 06b8629

Browse files
committed
Merge branch 'error-messages' [#33]
2 parents 99394f5 + af02f45 commit 06b8629

File tree

4 files changed

+55
-40
lines changed

4 files changed

+55
-40
lines changed

src/error_message.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::type_tag::TypeTag;
2+
use crate::value::Value;
3+
4+
pub fn type_mismatch(expected: TypeTag, got: &Value) -> Value {
5+
Value::Condition(format!(
6+
"Type mismatch; Expected instance of {}, Recieved type {}",
7+
expected,
8+
got
9+
))
10+
}
11+
12+
pub fn wrong_arg_count(expected: usize, got: usize) -> Value {
13+
Value::Condition(format!(
14+
"Wrong number of arguments given to function (Given: {}, Expected: {})",
15+
got,
16+
expected
17+
))
18+
}
19+
20+
pub fn wrong_varg_count(expected: &[usize], got: usize) -> Value {
21+
Value::Condition(format!(
22+
"Wrong number of arguments given to function (Given: {}, Expected: {:?})",
23+
got,
24+
expected
25+
))
26+
}
27+
28+
pub fn index_out_of_bounds(ind: usize, count: usize) -> Value {
29+
Value::Condition(format!(
30+
"Index out of bounds: Index ({}), Length: ({})",
31+
ind, count
32+
))
33+
}
34+
35+
pub fn index_cannot_be_negative(ind: usize) -> Value {
36+
Value::Condition(format!("Index cannot be negative; Index ({})", ind))
37+
}

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod keyword;
1818
mod type_tag;
1919
mod util;
2020
mod value;
21+
mod error_message;
2122

2223
fn main() {
2324
//

src/rust_core.rs

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use crate::persistent_list::{
1010
};
1111
use crate::persistent_list_map::IPersistentListMap;
1212
use crate::persistent_vector::{PersistentVector, ToPersistentVectorIter};
13-
use crate::repl::Repl;
1413
use crate::symbol::Symbol;
1514
use crate::type_tag::TypeTag;
1615
use crate::value::{Evaluable, ToValue};
1716

1817
use itertools::Itertools;
18+
use crate::error_message;
1919

2020
use 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 {
271248
impl 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

src/type_tag.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub enum TypeTag {
1313
// Experimental; may make no sense at runtime, as we will likely be unable to take the value of a macro
1414
Macro,
1515
String,
16-
Nil,
16+
Integer,
17+
ISeq,
18+
Nil
1719
}
1820
use TypeTag::*;
1921
impl fmt::Display for TypeTag {
@@ -30,6 +32,8 @@ impl fmt::Display for TypeTag {
3032
PersistentListMap => std::string::String::from("clojure.lang.PersistentListMap"),
3133
Macro => std::string::String::from("clojure.lang.Macro"),
3234
TypeTag::String => std::string::String::from("rust.std.string.String"),
35+
TypeTag::Integer => std::string::String::from("clojure.lang.Integer"),
36+
ISeq => std::string::String::from("clojure.lang.ISeq"),
3337
Nil => std::string::String::from("clojure.lang.Nil"),
3438
};
3539
write!(f, "{}", str)

0 commit comments

Comments
 (0)