diff options
author | Akshay <[email protected]> | 2021-05-13 16:20:17 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-05-13 16:20:17 +0100 |
commit | 09ee8cc84251d1758766dedff9e25497eebb88d8 (patch) | |
tree | dc837f785ad64d6c284235aa6c53fa533a3efbf1 /src/lisp/expr.rs | |
parent | 41c50e4c324b19183d1c36c185878d4fa500662a (diff) |
rework arity errors
Diffstat (limited to 'src/lisp/expr.rs')
-rw-r--r-- | src/lisp/expr.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 045991e..6f5b139 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs | |||
@@ -26,13 +26,17 @@ pub enum Arity { | |||
26 | } | 26 | } |
27 | 27 | ||
28 | impl Arity { | 28 | impl Arity { |
29 | pub fn check<T>(self, args: &[T]) -> bool { | 29 | pub fn check<T>(self, args: &[T]) -> Result<(), LispError> { |
30 | match self { | 30 | if !match self { |
31 | Arity::Exact(a) => args.len() == a, | 31 | Arity::Exact(a) => args.len() == a, |
32 | Arity::Atleast(a) => args.len() >= a, | 32 | Arity::Atleast(a) => args.len() >= a, |
33 | Arity::Atmost(a) => args.len() <= a, | 33 | Arity::Atmost(a) => args.len() <= a, |
34 | Arity::Range(low, high) => args.len() >= low && args.len() <= high, | 34 | Arity::Range(low, high) => args.len() >= low && args.len() <= high, |
35 | Arity::None => true, | 35 | Arity::None => true, |
36 | } { | ||
37 | Err(self.to_error()) | ||
38 | } else { | ||
39 | Ok(()) | ||
36 | } | 40 | } |
37 | } | 41 | } |
38 | pub fn to_error(self) -> LispError { | 42 | pub fn to_error(self) -> LispError { |
@@ -49,9 +53,7 @@ pub struct PrimitiveFunc { | |||
49 | 53 | ||
50 | impl PrimitiveFunc { | 54 | impl PrimitiveFunc { |
51 | pub fn call(&self, args: &[LispExpr], app: &mut AppState) -> Result<LispExpr, LispError> { | 55 | pub fn call(&self, args: &[LispExpr], app: &mut AppState) -> Result<LispExpr, LispError> { |
52 | if !self.arity.check(args) { | 56 | self.arity.check(args)?; |
53 | return Err(EvalError::ArgumentCount(self.arity).into()); | ||
54 | } | ||
55 | (self.closure)(args, app) | 57 | (self.closure)(args, app) |
56 | } | 58 | } |
57 | } | 59 | } |