Implement two entry types and a function to store entries in table.

This commit is contained in:
Danila Fedorin 2018-02-11 22:50:08 -08:00
parent 3de3f1ec00
commit 72be209f0f
2 changed files with 22 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include "ht.h" #include "ht.h"
#include "result.h" #include "result.h"
#include "custom.h"
/** /**
* A struct that represents a structure * A struct that represents a structure
@ -29,6 +30,7 @@ struct libab_table_s {
enum libab_table_entry_variant_e { enum libab_table_entry_variant_e {
ENTRY_VALUE, ENTRY_VALUE,
ENTRY_TYPE, ENTRY_TYPE,
ENTRY_OPERATOR,
ENTRY_FUNCTION ENTRY_FUNCTION
}; };
@ -40,6 +42,14 @@ struct libab_table_entry_s {
* The type of this entry. * The type of this entry.
*/ */
enum libab_table_entry_variant_e variant; enum libab_table_entry_variant_e variant;
/**
* Union that holds the various types of
* data that this entry could hold.
*/
union {
libab_operator op;
libab_function function;
} data_u;
}; };
typedef struct libab_table_s libab_table; typedef struct libab_table_s libab_table;
@ -58,6 +68,14 @@ void libab_table_init(libab_table* table);
* @return the table entry, or NULL if an entry was not found. * @return the table entry, or NULL if an entry was not found.
*/ */
libab_table_entry* libab_table_search(libab_table* table, const char* string); libab_table_entry* libab_table_search(libab_table* table, const char* string);
/**
* Stores the given entry in the table under the given key.
* @param table the table to store the entry into.
* @param string the string to use as the key.
* @param entry the new entry to put into the table.
* @return the result of the insertion, which could be LIBAB_MALLOC.
*/
libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry);
/** /**
* Frees the resources allocated by the * Frees the resources allocated by the
* given table. * given table.

View File

@ -1,5 +1,6 @@
#include "table.h" #include "table.h"
#include <stdlib.h> #include <stdlib.h>
#include "util.h"
void table_init(libab_table* table) { void table_init(libab_table* table) {
ht_init(&table->table); ht_init(&table->table);
@ -13,6 +14,9 @@ libab_table_entry* table_search(libab_table* table, const char* string) {
} while(table && to_return == NULL); } while(table && to_return == NULL);
return to_return; return to_return;
} }
libab_result libab_table_put(libab_table* table, const char* string, libab_table_entry* entry) {
return libab_convert_ds_result(ht_put(&table->table, string, entry));
}
void table_free(libab_table* table) { void table_free(libab_table* table) {
ht_free(&table->table); ht_free(&table->table);
} }