forked from bonsai/harakit
		
	rpn(1): made EvaluationError struct contain exit code
This commit is contained in:
		
							parent
							
								
									8e0eaeab38
								
							
						
					
					
						commit
						d768a257a3
					
				
							
								
								
									
										20
									
								
								src/rpn.rs
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/rpn.rs
									
									
									
									
									
								
							| @ -112,19 +112,15 @@ impl Display for CalcType { | ||||
| } | ||||
| 
 | ||||
| #[derive(Debug, Clone)] | ||||
| struct EvaluationError { pub message: String } | ||||
| 
 | ||||
| impl Display for EvaluationError { | ||||
| 	fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||||
| 		write!(f, "{}", self.message) | ||||
| 	} | ||||
| struct EvaluationError { | ||||
| 	message: String, | ||||
| 	code: i32, | ||||
| } | ||||
| 
 | ||||
| // I’m no math nerd but I want the highest possible approximation of 0.9
 | ||||
| // repeating and it seems this can give it to me
 | ||||
| const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0; | ||||
| 
 | ||||
| 
 | ||||
| fn eval( | ||||
| 	input: &str, | ||||
| 	initial_stack: VecDeque<f64>, | ||||
| @ -150,7 +146,8 @@ fn eval( | ||||
| 			Val(v) => stack.push_back(v), | ||||
| 			Invalid(i) => { | ||||
| 				return Err(EvaluationError { | ||||
| 					message: format!("{}: Invalid token", i) | ||||
| 					message: format!("{}: Invalid token", i), | ||||
| 					code: EX_DATAERR, | ||||
| 				}) | ||||
| 			}, | ||||
| 			op => { | ||||
| @ -175,7 +172,8 @@ fn eval( | ||||
| 					}; | ||||
| 				} else { | ||||
| 					return Err(EvaluationError { | ||||
| 						message: format!("{}: Unexpected operation.", op) | ||||
| 						message: format!("{}: Unexpected operation.", op), | ||||
| 						code: EX_DATAERR, | ||||
| 					}) | ||||
| 				} | ||||
| 			}, | ||||
| @ -217,7 +215,7 @@ fn main() -> ExitCode { | ||||
| 				}, | ||||
| 				Err(err) => { | ||||
| 					eprintln!("{}: {}", argv[0], err.message); | ||||
| 					return ExitCode::from(EX_DATAERR as u8); | ||||
| 					return ExitCode::from(err.code as u8); | ||||
| 				}, | ||||
| 			}; | ||||
| 		} | ||||
| @ -242,7 +240,7 @@ fn main() -> ExitCode { | ||||
| 			}, | ||||
| 			Err(err) => { | ||||
| 				eprintln!("{}: {}", argv[0], err.message); | ||||
| 				return ExitCode::from(EX_DATAERR as u8); | ||||
| 				return ExitCode::from(err.code as u8); | ||||
| 			}, | ||||
| 		}; | ||||
| 	} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user