From 6e27b20d10306d53cd838ef375fe80571dfe91ff Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 24 Oct 2021 11:52:56 +0200 Subject: Add updated hash table with intern key-values --- src/bytecode/compiler.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/bytecode/compiler.h') diff --git a/src/bytecode/compiler.h b/src/bytecode/compiler.h index d404a5a..ec51942 100755 --- a/src/bytecode/compiler.h +++ b/src/bytecode/compiler.h @@ -33,11 +33,16 @@ has_next_token(const Visitor *visitor) { void emit_constant(Chunk *chunk, Token tok, Object obj) { - // TODO: Should we deduplicate constants? For example why store a number - // more than once instead of reusing the existing index? + size_t prev_size = array_size(chunk->constants); size_t num_idx = add_constant(chunk, obj); add_code(chunk, OP_CONSTANT, tok.line, tok.column); add_code(chunk, num_idx, tok.line, tok.column); + + // If the non value constant was already present we need to properly free + // the memory from the object given to this function. + if (prev_size == array_size(chunk->constants)) { + object_free(obj); + } } void @@ -180,6 +185,7 @@ parse_list(Chunk *chunk, Visitor *vs, Token start) { .line = start.line, .col = start.column, }); + return; } Token tok = next_token(vs); switch (tok.type) { -- cgit v1.2.1