Fixed bug where temperature literals were not being calculated correctly with negative numbers

This commit is contained in:
2025-12-15 23:13:40 -07:00
parent 941e81a3e5
commit 477c2b1aef
5 changed files with 214 additions and 95 deletions

View File

@@ -175,3 +175,52 @@ fn test_ternary_expression_assignment() -> Result<()> {
Ok(())
}
#[test]
fn test_negative_literals() -> Result<()> {
let compiled = compile!(
debug
r#"
let item = -10c - 20c;
"#
);
assert_eq!(
compiled,
indoc! {
"
j main
main:
move r8 243.15
"
}
);
Ok(())
}
#[test]
fn test_mismatched_temperature_literals() -> Result<()> {
let compiled = compile!(
debug
r#"
let item = -10c - 100k;
let item2 = item + 500c;
"#
);
assert_eq!(
compiled,
indoc! {
"
j main
main:
move r8 163.15
add r1 r8 773.15
move r9 r1
"
}
);
Ok(())
}

View File

@@ -15,7 +15,7 @@ use parser::{
use rust_decimal::Decimal;
use std::{borrow::Cow, collections::HashMap};
use thiserror::Error;
use tokenizer::token::Number;
use tokenizer::token::{Number, Unit};
fn extract_literal<'a>(
literal: Literal<'a>,
@@ -811,7 +811,7 @@ impl<'a> Compiler<'a> {
..
})),
..
}) => Literal::Number(Number::Integer(crc_hash_signed(&str_to_hash))),
}) => Literal::Number(Number::Integer(crc_hash_signed(&str_to_hash), Unit::None)),
LiteralOr::Or(Spanned { span, .. }) => {
return Err(Error::Unknown(
"hash only supports string literals in this context.".into(),
@@ -2022,6 +2022,7 @@ impl<'a> Compiler<'a> {
let loc = VariableLocation::Constant(Literal::Number(Number::Integer(
crc_hash_signed(&str_lit),
Unit::None,
)));
Ok(Some(CompileLocation {