2018-02-10 17:53:07 -08:00
|
|
|
#ifndef LIBABACUS_TABLE_H
|
|
|
|
#define LIBABACUS_TABLE_H
|
|
|
|
|
2018-02-11 22:32:42 -08:00
|
|
|
#include "result.h"
|
2018-02-11 22:50:08 -08:00
|
|
|
#include "custom.h"
|
2018-03-24 20:47:34 -07:00
|
|
|
#include "trie.h"
|
2018-04-17 12:07:22 -07:00
|
|
|
#include "basetype.h"
|
2018-02-10 17:53:07 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A struct that represents a structure
|
|
|
|
* similar to a symbol table. This structure
|
|
|
|
* is used to keep track of definitions such
|
|
|
|
* as types, functions, and variables in an
|
|
|
|
* environment with scopes.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
struct libab_table_s {
|
2018-02-10 17:53:07 -08:00
|
|
|
/**
|
|
|
|
* The "parent" scope of this table.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
struct libab_table_s* parent;
|
2018-02-10 17:53:07 -08:00
|
|
|
/**
|
|
|
|
* The hash table used to store the data.
|
|
|
|
*/
|
2018-03-24 20:47:34 -07:00
|
|
|
libab_trie trie;
|
2018-02-10 17:53:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum that represents the type of a table
|
|
|
|
* entry.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
enum libab_table_entry_variant_e {
|
2018-02-10 17:53:07 -08:00
|
|
|
ENTRY_VALUE,
|
2018-04-17 12:07:22 -07:00
|
|
|
ENTRY_BASETYPE,
|
2018-02-17 14:00:37 -08:00
|
|
|
ENTRY_OP,
|
|
|
|
ENTRY_FUN
|
2018-02-10 17:53:07 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An entry in the table.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
struct libab_table_entry_s {
|
2018-02-10 17:53:07 -08:00
|
|
|
/**
|
|
|
|
* The type of this entry.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
enum libab_table_entry_variant_e variant;
|
2018-02-11 22:50:08 -08:00
|
|
|
/**
|
|
|
|
* Union that holds the various types of
|
|
|
|
* data that this entry could hold.
|
|
|
|
*/
|
|
|
|
union {
|
|
|
|
libab_operator op;
|
|
|
|
libab_function function;
|
2018-04-17 12:07:22 -07:00
|
|
|
libab_basetype* basetype;
|
2018-02-11 22:50:08 -08:00
|
|
|
} data_u;
|
2018-02-10 17:53:07 -08:00
|
|
|
};
|
|
|
|
|
2018-02-11 21:09:41 -08:00
|
|
|
typedef struct libab_table_s libab_table;
|
|
|
|
typedef enum libab_table_entry_variant_e libab_table_entry_variant;
|
|
|
|
typedef struct libab_table_entry_s libab_table_entry;
|
2018-02-10 17:53:07 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the given table.
|
|
|
|
* @param table the table to initialize.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
void libab_table_init(libab_table* table);
|
2018-03-17 17:39:44 -07:00
|
|
|
/**
|
|
|
|
* Searches for the given string in the table, comparing
|
|
|
|
* values to a given reference as an extra filtering step.
|
|
|
|
* @param table the table to search.
|
|
|
|
* @param string the string to search for.
|
|
|
|
* @param data the data to compare against potential values.
|
|
|
|
* @param compare the comparison function to use.
|
|
|
|
* @return the table entry, or NULL if an entry was not found.
|
|
|
|
*/
|
|
|
|
libab_table_entry* libab_table_search_filter(libab_table* table, const char* string, void* data, compare_func compare);
|
2018-02-10 17:57:24 -08:00
|
|
|
/**
|
|
|
|
* Searches for the given string in the table.
|
|
|
|
* @param table the table to search.
|
|
|
|
* @param string the string to search for.
|
|
|
|
* @return the table entry, or NULL if an entry was not found.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
libab_table_entry* libab_table_search(libab_table* table, const char* string);
|
2018-02-11 23:00:07 -08:00
|
|
|
/**
|
|
|
|
* Searches for the given string in the table, returning a value only
|
|
|
|
* if it is an operator.
|
|
|
|
* @param table the table to search.
|
|
|
|
* @param string the string to search for.
|
2018-03-17 17:39:44 -07:00
|
|
|
* @param type the type of operator to search for (infix, prefix, postfix)
|
2018-02-11 23:00:07 -08:00
|
|
|
* @return the found operator, or NULL if it was not found.
|
|
|
|
*/
|
2018-03-17 17:39:44 -07:00
|
|
|
libab_operator* libab_table_search_operator(libab_table* table, const char* string, int type);
|
2018-02-11 23:00:07 -08:00
|
|
|
/**
|
|
|
|
* Searches for the given string in the table, returning a value only
|
|
|
|
* if it is a function.
|
|
|
|
* @param table the table to search.
|
|
|
|
* @param string the string to search for.
|
|
|
|
* @return the found function, or NULL if it was not found.
|
|
|
|
*/
|
|
|
|
libab_function* libab_table_search_function(libab_table* table, const char* string);
|
2018-04-17 12:07:22 -07:00
|
|
|
/**
|
|
|
|
* Searches for the given basetype in the table, returning a value
|
|
|
|
* only if it's a basetype.
|
|
|
|
* @param table the table to search.
|
|
|
|
* @param string the string to search for.
|
|
|
|
* @return the found basetype, or NULL if it was not found.
|
|
|
|
*/
|
|
|
|
libab_basetype* libab_table_search_basetype(libab_table* table, const char* string);
|
2018-02-11 22:50:08 -08:00
|
|
|
/**
|
|
|
|
* 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);
|
2018-02-10 17:53:07 -08:00
|
|
|
/**
|
|
|
|
* Frees the resources allocated by the
|
|
|
|
* given table.
|
|
|
|
* @param table the table to free.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
void libab_table_free(libab_table* table);
|
2018-02-17 19:53:33 -08:00
|
|
|
/**
|
|
|
|
* Frees the given table entry.
|
|
|
|
* @param entry the entry to free.
|
|
|
|
*/
|
|
|
|
void libab_table_entry_free(libab_table_entry* entry);
|
2018-02-10 17:53:07 -08:00
|
|
|
|
|
|
|
#endif
|