From c68fe7634341e97c3246b627a20fa9487586ffcb Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 21 Aug 2019 06:08:00 +0200 Subject: hash map contains --- include/program.h | 5 +++++ include/std/hash_map.h | 10 ++++++---- src/parser.c | 2 +- src/program.c | 4 ++++ src/std/hash_map.c | 7 ++++++- tests/bytecode.amal | 4 +++- 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/include/program.h b/include/program.h index 543b38d..075bfca 100644 --- a/include/program.h +++ b/include/program.h @@ -30,6 +30,7 @@ #define AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS_SIZE -18 #define AMAL_PROGRAM_INSTRUCTION_INVALID_EXTERN_FUNC_INDEX -19 #define AMAL_PROGRAM_NO_SUCH_EXTERNAL_FUNCTION -20 +#define AMAL_PROGRAM_EXTERN_FUNC_ALREADY_EXISTS -21 #define AMAL_PROGRAM_MAGIC_NUMBER (u32)0xdec05eba #define AMAL_PROGRAM_MAJOR_VERSION 1 @@ -66,6 +67,10 @@ typedef struct { CHECK_RESULT int amal_program_init(amal_program *self); void amal_program_deinit(amal_program *self); +/* + returns @AMAL_PROGRAM_EXTERN_FUNC_ALREADY_EXISTS if a function with the name @name already exists + in the program +*/ CHECK_RESULT int amal_program_add_extern_func(amal_program *self, BufferView name, void *func_ptr, int args_byte_size); CHECK_RESULT int amal_program_append_bytecode(amal_program *self, Bytecode *bytecode); diff --git a/include/std/hash_map.h b/include/std/hash_map.h index d789db4..c9e5b51 100644 --- a/include/std/hash_map.h +++ b/include/std/hash_map.h @@ -25,15 +25,17 @@ struct HashMap { CHECK_RESULT int hash_map_init(HashMap *self, ArenaAllocator *allocator, usize value_type_size, HashMapCompare key_compare_func, HashMapHash key_hash_func); /* -Not thread-safe. -Expected @value size to be @self->value_type_size. + Not thread-safe. + Expected @value size to be @self->value_type_size. */ CHECK_RESULT int hash_map_insert(HashMap *self, BufferView key, void *value); /* -Thread-safe unless @hash_map_insert is used in another thread at the same time. -Expected @value size to be @self->value_type_size. + Thread-safe unless @hash_map_insert is used in another thread at the same time. + Expected @value size to be @self->value_type_size. + If @value is NULL, then the value is not copied and the functions works the same as @hash_map_contains */ CHECK_RESULT bool hash_map_get(HashMap *self, BufferView key, void *value); +CHECK_RESULT bool hash_map_contains(HashMap *self, BufferView key); int hash_map_compare_string(const void *a, const void *b); diff --git a/src/parser.c b/src/parser.c index cdf7c5b..03b835e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -521,7 +521,7 @@ static void parser_parse_else_if_statement_loop(Parser *self, IfStatement *if_st if(!next_else_if) break; else_if_stmt->next_else_if_stmt = next_else_if; - /* else statement, which because they have no conditions; can't be followed by another else statement */ + /* else statement that has no condition can't be followed by another else statement */ if(!else_if_stmt->condition) break; else_if_stmt = next_else_if; diff --git a/src/program.c b/src/program.c index 9ec1b41..17aee03 100644 --- a/src/program.c +++ b/src/program.c @@ -91,6 +91,10 @@ int amal_program_add_extern_func(amal_program *self, BufferView name, void *func ProgramExternFunc extern_func; extern_func.func = func_ptr; extern_func.args_byte_size = args_byte_size; + + if(hash_map_contains(&self->extern_funcs_map, name)) + return AMAL_PROGRAM_EXTERN_FUNC_ALREADY_EXISTS; + return hash_map_insert(&self->extern_funcs_map, name, &extern_func); } diff --git a/src/std/hash_map.c b/src/std/hash_map.c index 2b29f2e..48e9e38 100644 --- a/src/std/hash_map.c +++ b/src/std/hash_map.c @@ -192,7 +192,8 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { BufferView bucket_key; bucket_key = bucket_node_get_key(bucket_node); if(hash == bucket_node_get_hash(bucket_node) && self->compare_func(&key, &bucket_key) == 0) { - am_memcpy(value, bucket_node_get_value(bucket_node), self->value_type_size); + if(value) + am_memcpy(value, bucket_node_get_value(bucket_node), self->value_type_size); return bool_true; } } @@ -200,6 +201,10 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { return bool_false; } +bool hash_map_contains(HashMap *self, BufferView key) { + return hash_map_get(self, key, NULL); +} + #define MIN(a, b) ((a) < (b) ? (a) : (b)) int hash_map_compare_string(const void *a, const void *b) { diff --git a/tests/bytecode.amal b/tests/bytecode.amal index 096f921..0aad6de 100644 --- a/tests/bytecode.amal +++ b/tests/bytecode.amal @@ -4,10 +4,12 @@ extern const print_extern_num: fn(num: i32); const main = fn { var value = 23; value = 34; - const value2: i64 = 23; + const value2: i64 = 54; const value3 = 2 + 5 - 1 * 10 / 2; const str_value = "hello, world"; print(); + //if value2 == 54 + // const result = print_num(1337); const result = print_num(1337); } -- cgit v1.2.3