diff --git a/rlox/src/bc.rs b/rlox/src/bc.rs index bdf7873..0e2f5db 100644 --- a/rlox/src/bc.rs +++ b/rlox/src/bc.rs @@ -44,6 +44,13 @@ impl Value { _ => None, } } + + pub fn as_obj(&self) -> Option { + match self { + &Value::Obj(val) => Some(val), + _ => None, + } + } } impl From for Value { diff --git a/rlox/src/lc.rs b/rlox/src/lc.rs index 066bcf9..2734f6e 100644 --- a/rlox/src/lc.rs +++ b/rlox/src/lc.rs @@ -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); + } + }