diff --git a/crates/oxide_compiler/src/hash.rs b/crates/oxide_compiler/src/hash.rs index 92f158c..96e7909 100644 --- a/crates/oxide_compiler/src/hash.rs +++ b/crates/oxide_compiler/src/hash.rs @@ -23,6 +23,11 @@ fn hash_statement(stmt: &Statement, h: &mut rustc_hash::FxHasher) { Statement::VariableDeclaration(decl) => { 1u8.hash(h); (decl.declarations.len() as u32).hash(h); + for d in &decl.declarations { + if let Some(init) = &d.init { + hash_expression(init, h); + } + } } Statement::ReturnStatement(ret) => { 2u8.hash(h); @@ -108,6 +113,21 @@ fn hash_expression(expr: &Expression, h: &mut rustc_hash::FxHasher) { hash_expression(&cond.consequent, h); hash_expression(&cond.alternate, h); } + Expression::Identifier(_) => { + 5u8.hash(h); + } + Expression::NumericLiteral(num) => { + 6u8.hash(h); + num.value.to_bits().hash(h); + } + Expression::StringLiteral(s) => { + 7u8.hash(h); + s.value.hash(h); + } + Expression::BooleanLiteral(b) => { + 8u8.hash(h); + b.value.hash(h); + } _ => { std::mem::discriminant(expr).hash(h); } diff --git a/crates/oxide_compiler/tests/compiler_tests.rs b/crates/oxide_compiler/tests/compiler_tests.rs index 83ce800..d49281c 100644 --- a/crates/oxide_compiler/tests/compiler_tests.rs +++ b/crates/oxide_compiler/tests/compiler_tests.rs @@ -103,10 +103,31 @@ fn structural_hash_hit() { #[test] fn structural_hash_same_shape() { let hash_a = parse_to_hash("var x = 1 + 2; var y = x;"); - let hash_b = parse_to_hash("var a = 3 + 4; var b = a;"); + let hash_b = parse_to_hash("var a = 1 + 2; var b = a;"); assert_eq!(hash_a, hash_b); } +#[test] +fn structural_hash_different_init_values() { + let hash_a = parse_to_hash("var x = 1 + 2;"); + let hash_b = parse_to_hash("var y = 3 + 4;"); + assert_ne!(hash_a, hash_b); +} + +#[test] +fn structural_hash_different_string_literals() { + let hash_a = parse_to_hash(r#"var x = "hello";"#); + let hash_b = parse_to_hash(r#"var x = "world";"#); + assert_ne!(hash_a, hash_b); +} + +#[test] +fn structural_hash_different_booleans() { + let hash_a = parse_to_hash("var x = true;"); + let hash_b = parse_to_hash("var x = false;"); + assert_ne!(hash_a, hash_b); +} + #[test] fn structural_hash_different_shape() { let hash_a = parse_to_hash("1 + 2"); diff --git a/crates/oxide_kernel/src/code_forge.rs b/crates/oxide_kernel/src/code_forge.rs index 9d7bdf2..61b4599 100644 --- a/crates/oxide_kernel/src/code_forge.rs +++ b/crates/oxide_kernel/src/code_forge.rs @@ -61,7 +61,7 @@ mod tests { let a1 = Allocator::default(); let a2 = Allocator::default(); let p1 = oxide_parser::parse(&a1, "var x = 1; var y = 2;").expect("parse failed"); - let p2 = oxide_parser::parse(&a2, "var a = 3; var b = 4;").expect("parse failed"); + let p2 = oxide_parser::parse(&a2, "var a = 1; var b = 2;").expect("parse failed"); assert_eq!(structural_hash(&p1), structural_hash(&p2)); }