Properly print negative decimals

This commit is contained in:
ConnorSkees 2020-02-16 15:20:38 -05:00
parent 24dc99affe
commit 3512873363
3 changed files with 32 additions and 3 deletions

View File

@ -40,7 +40,7 @@ impl<'a> Iterator for Lexer<'a> {
self.buf.next();
self.pos.next_char();
match self.buf.peek().unwrap() {
'0'..='9' => match self.lex_num() {
'0'..='9' | '.' => match self.lex_num() {
TokenKind::Number(n) => {
let mut s = String::from("-");
s.push_str(&n);

View File

@ -93,13 +93,16 @@ from_integer!(u8);
impl Display for Number {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.val.to_integer())?;
if self.val.is_negative() {
f.write_char('-')?;
}
write!(f, "{}", self.val.to_integer().abs())?;
let mut frac = self.val.fract();
if frac != BigRational::from_integer(BigInt::from(0)) {
f.write_char('.')?;
for _ in 0..(PRECISION - 1) {
frac *= BigRational::from_integer(BigInt::from(10));
write!(f, "{}", frac.to_integer())?;
write!(f, "{}", frac.to_integer().abs())?;
frac = frac.fract();
if frac == BigRational::from_integer(BigInt::from(0)) {
break;

View File

@ -307,3 +307,29 @@ test!(
"a {\n color: 1 + foo();\n}\n",
"a {\n color: 1foo();\n}\n"
);
test!(
positive_integer,
"a {\n color: 1;\n}\n"
);
test!(
negative_integer,
"a {\n color: -1;\n}\n"
);
test!(
positive_float_no_leading_zero,
"a {\n color: .1;\n}\n",
"a {\n color: 0.1;\n}\n"
);
test!(
negative_float_no_leading_zero,
"a {\n color: -.1;\n}\n",
"a {\n color: -0.1;\n}\n"
);
test!(
positive_float_leading_zero,
"a {\n color: 0.1;\n}\n"
);
test!(
negative_float_leading_zero,
"a {\n color: -0.1;\n}\n"
);