Commit f7c8b163 authored by Tom Almeida's avatar Tom Almeida 📚

Merge branch 'fix-zero' into 'master'

Fix lone zero handling

Closes #26

See merge request !23
parents 62378ccb bff14f6e
Pipeline #3742 passed with stage
in 2 minutes and 56 seconds
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.9.0"
......
......@@ -396,24 +396,27 @@ where
match input.peek() {
Some(&'0') => {
input.next();
match input.peek() {
Some(&'x') | Some(&'X') => {
match input.peek().cloned() {
Some('x') | Some('X') => {
input.next();
let digits = digits(input, 16);
let num = Integral::from_str_radix(&digits, 16)?;
return Ok(Value::hex(num));
}
Some(&_) => (),
None => return Ok(Value::dec(0)),
Some(c) if c.is_digit(16) || c == '.' => (),
_ => return Ok(Value::dec(0)),
}
}
Some(_) => (),
None => return Err(CalcError::UnexpectedEndOfInput),
}
let whole = digits(input, 10);
let mut whole = digits(input, 10);
if let Some(&'.') = input.peek() {
input.next();
let frac = digits(input, 10);
if whole.is_empty() && frac.is_empty() {
whole = "0".to_string();
}
let num = [whole, ".".into(), frac]
.concat()
.parse::<d128>()
......@@ -474,6 +477,19 @@ mod tests {
assert_eq!(tokenize(line), Ok(expected));
}
#[test]
fn zero() {
let line = "0 + 0. + 0";
let expected = vec![
Token::Number(Value::dec(0)),
Token::Plus,
Token::Number(Value::Float(d128::from(0))),
Token::Plus,
Token::Number(Value::dec(0)),
];
assert_eq!(tokenize(line), Ok(expected));
}
#[test]
fn dice() {
let line = "3d6";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment