[rlox] Fix string interning

This commit is contained in:
ctsk
2024-08-27 15:30:04 +02:00
parent 0d7980ddc7
commit 78ef9fdc07
2 changed files with 21 additions and 0 deletions

View File

@@ -44,6 +44,13 @@ impl Value {
_ => None,
}
}
pub fn as_obj(&self) -> Option<Object> {
match self {
&Value::Obj(val) => Some(val),
_ => None,
}
}
}
impl From<f64> for Value {

View File

@@ -351,6 +351,7 @@ impl<'src> Parser<'src> {
None => {
let object = unsafe { allocate_string(without_quotes) }.unwrap();
chunk.add_constant(object.get_object().into(), 0);
self.intern_table.insert(without_quotes, chunk.constants.len() as u8 - 1);
chunk.allocations.push_front(object);
},
};
@@ -595,4 +596,17 @@ mod tests {
test_parse_expression(source, &expected);
}
#[test]
fn string_interning() {
let source = "\"ho\" + \"ho\" + \"ho\"";
let scanner = Scanner::new(source);
let mut parser = Parser::new(scanner);
let mut chunk = Chunk::new();
parser.expression(&mut chunk);
assert_eq!(chunk.allocations.len(), 1);
assert_eq!(chunk.constants.len(), 1);
}
}