From 31519e8a586791d60e6e56e558c2a3bf973cc1f9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 26 Jan 2020 08:02:17 +0100 Subject: Implement plus operator and indexing of strings --- include/program.h | 2 ++ include/std_gc/hash_map.h | 33 +++++++++++++++++++++++++++++++-- include/std_gc/list.h | 2 +- include/value.h | 12 +++++++++++- 4 files changed, 45 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/program.h b/include/program.h index bbd3f37..52b4fe4 100644 --- a/include/program.h +++ b/include/program.h @@ -12,6 +12,7 @@ typedef enum { TSL_STACK_VALUE_TYPE_STRING, TSL_STACK_VALUE_TYPE_NULL, TSL_STACK_VALUE_TYPE_FUNCTION, + TSL_STACK_VALUE_TYPE_VARIABLE_NAME, TSL_STACK_VALUE_TYPE_VARIABLE, TSL_STACK_VALUE_TYPE_LIST, TSL_STACK_VALUE_TYPE_MAP, @@ -25,6 +26,7 @@ typedef struct { double number; TslBool boolean; TslStringView str; + TslValue variable; } data; TslStackValueType type; } TslStackValue; diff --git a/include/std_gc/hash_map.h b/include/std_gc/hash_map.h index e1a016d..fb6221b 100644 --- a/include/std_gc/hash_map.h +++ b/include/std_gc/hash_map.h @@ -2,15 +2,30 @@ #define TSL_HASH_MAP_H #include "../forward_decl.h" +#include "../value.h" #include #include +typedef struct TslHashMapNode TslHashMapNode; +struct TslHashMapNode { + uint64_t hash; + TslValue key; + TslValue value; + TslHashMapNode *next; +}; + /* TODO: Optimize small hash map by using the members of the struct instead of allocating on heap */ -typedef struct { +struct TslHashMap { void *buckets_data; /* value=TslHashMapNode */ size_t buckets_size; size_t buckets_capacity; -} TslHashMap; +}; + +typedef struct { + TslHashMap *hash_map; + size_t index; + TslHashMapNode *node; +} TslHashMapIterator; void tsl_hash_map_init(TslHashMap *self); @@ -27,4 +42,18 @@ TslValue* tsl_hash_map_get(TslHashMap *self, const TslValue *key); */ TslValue* tsl_hash_map_get_or_create(TslHashMap *self, const TslValue *key); +/* + How to use the iterator: + + TslHashMapIterator iterator; + tsl_hash_map_create_iterator(hash_map, &iterator); + while(tsl_hash_map_iterator_next(&iterator)) { + TslValue *key = &iterator.node->key; + TslValue *value = &iterator.node->value; + } +*/ + +void tsl_hash_map_create_iterator(TslHashMap *self, TslHashMapIterator *iterator); +int tsl_hash_map_iterator_next(TslHashMapIterator *self); + #endif /* TSL_HASH_MAP_H */ diff --git a/include/std_gc/list.h b/include/std_gc/list.h index 469f585..a97f311 100644 --- a/include/std_gc/list.h +++ b/include/std_gc/list.h @@ -16,6 +16,6 @@ int tsl_list_set_capacity_hint(TslList *self, size_t capacity); TslValue* tsl_list_begin(TslList *self); TslValue* tsl_list_end(TslList *self); /* Returns NULL if index is out of bounds of the list */ -TslValue* tsl_list_get(TslList *self, double index); +TslValue* tsl_list_get(const TslList *self, double index); #endif /* TSL_LIST_H */ diff --git a/include/value.h b/include/value.h index 7dd6d28..b90b446 100644 --- a/include/value.h +++ b/include/value.h @@ -6,7 +6,6 @@ #include #include "string_view.h" #include "std_gc/list.h" -#include "std_gc/hash_map.h" typedef enum { TSL_TYPE_NULL, @@ -36,10 +35,16 @@ typedef struct { /* This is an index to the function */ typedef int TslFunction; +typedef struct TslHashMap TslHashMap; + struct TslValue { union { TslNumber number; TslString *string; + /* + TODO: Make this an int that refers to the index in the bytecode which contains the string view. + Then the size of this union will be 8 bytes instead of 16 bytes on 64-bit systems. + */ TslStringView string_ref; TslBool boolean; TslBool null; @@ -58,4 +63,9 @@ int tsl_value_equals(const TslValue *lhs, const TslValue *rhs); /* This should be called before setting/modifying the data of the value */ void tsl_value_clear(TslValue *self); +int tsl_value_add(const TslValue *lhs_value, const TslValue *rhs_value, TslValue *result); + +/* Returns NULL if index is out of bounds of the list */ +TslValue* tsl_string_ref_get(const TslStringView *self, double index); + #endif /* TSL_VALUE_H */ -- cgit v1.2.3