libabacus/include/table.h

212 lines
7.0 KiB
C
Raw Permalink Normal View History

#ifndef LIBABACUS_TABLE_H
#define LIBABACUS_TABLE_H
2018-04-21 14:09:01 -07:00
#include "basetype.h"
#include "custom.h"
2018-05-17 14:53:48 -07:00
#include "refcount.h"
2018-04-21 14:09:01 -07:00
#include "result.h"
2018-03-24 20:47:34 -07:00
#include "trie.h"
/**
* 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.
*/
struct libab_table_s {
/**
* The "parent" scope of this table.
*/
2018-04-24 11:06:21 -07:00
libab_ref parent;
/**
* The hash table used to store the data.
*/
2018-03-24 20:47:34 -07:00
libab_trie trie;
};
/**
* Enum that represents the type of a table
* entry.
*/
2018-05-27 23:10:59 -07:00
enum libab_table_entry_variant_e {
ENTRY_VALUE,
ENTRY_BASETYPE,
ENTRY_OP,
ENTRY_TYPE_PARAM
};
/**
* An entry in the table.
*/
struct libab_table_entry_s {
/**
* The type of this entry.
*/
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_basetype* basetype;
libab_ref value;
2018-05-27 23:10:59 -07:00
libab_ref type_param;
} data_u;
};
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;
/**
* Initializes the given table.
* @param table the table to initialize.
*/
void libab_table_init(libab_table* table);
/**
* 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.
*/
2018-04-21 14:09:01 -07:00
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.
*/
libab_table_entry* libab_table_search(libab_table* table, const char* string);
/**
* 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.
* @param type the type of operator to search for (infix, prefix, postfix)
* @return the found operator, or NULL if it was not found.
*/
2018-04-21 14:09:01 -07:00
libab_operator* libab_table_search_operator(libab_table* table,
const char* string, int type);
2018-08-10 19:47:11 -07:00
/**
* Searches for a given string in the table,
* returning the entry that holds it only if it is an operator.
* @param table the table to search.
* @param string the string to search for.
* @param type the type of operator to search for (infix, prefix, postfix)
* @return the entry, or NULL if it was not found.
*/
libab_table_entry* libab_table_search_entry_operator(libab_table* table,
const char* string,
int type);
/**
* 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.
*/
2018-04-21 14:09:01 -07:00
libab_basetype* libab_table_search_basetype(libab_table* table,
const char* string);
2018-08-10 19:47:11 -07:00
/**
* Searches for the given basetype in the table, returning a table
* entry only if it holds a basetype.
* @param table to table to search.
* @param string the string to search for.
* @return the entry holding the basetype, or NULL if it was not found.
*/
libab_table_entry* libab_table_search_entry_basetype(libab_table* table,
const char* string);
/**
* Searches for the given value in the table.
* @param table the table to search.
* @param string the table entry key.
* @param ref the reference to store the result into.
*/
2018-05-17 14:53:48 -07:00
void libab_table_search_value(libab_table* table, const char* string,
libab_ref* ref);
2018-05-27 23:10:59 -07:00
/**
2018-08-10 19:47:11 -07:00
* Searches for a value with the given name in the table,
* and returns an entry that holds it.
* @param table the table to search.
* @param string the tabe entry key.
* @return the table entry holding the value, or NULL if it was not found.
*/
libab_table_entry* libab_table_search_entry_value(libab_table* table,
const char* string);
/**
* Searches for the given type parameter in the table.
2018-05-27 23:10:59 -07:00
* @param table the table to search in.
2018-08-10 19:47:11 -07:00
* @param string the key to search for.
2018-05-27 23:10:59 -07:00
* @param ref the reference to store the type into.
*/
void libab_table_search_type_param(libab_table* table, const char* string,
libab_ref* ref);
2018-08-10 19:47:11 -07:00
/**
* Searches for the given type parameter in the table.
* @param table the table to search in.
* @param string the key to search for.
* @return the table entry holding the type parameter, or NULL.
*/
libab_table_entry* libab_table_search_entry_type_param(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.
*/
2018-04-21 14:09:01 -07:00
libab_result libab_table_put(libab_table* table, const char* string,
libab_table_entry* entry);
2018-04-24 11:06:21 -07:00
/**
* Sets the parent of the given table.
* @param table the table whose parent to set.
* @param parent a valid reference to a parent table.
*/
void libab_table_set_parent(libab_table* table, libab_ref* parent);
2018-05-31 19:29:14 -07:00
/**
* Clears the table.
* @param table the table to clear.
*/
void libab_table_clear(libab_table* table);
/**
* Frees the resources allocated by the
* given table.
* @param table the table to free.
*/
void libab_table_free(libab_table* table);
/**
* Comparison function used to search the table for a prefix operator.
*/
int libab_table_compare_op_prefix(const void* left, const void* right);
/**
* Comparison function used to search the table for a infix operator.
*/
int libab_table_compare_op_infix(const void* left, const void* right);
/**
* Comparison function used to search the table for a postfix operator.
*/
int libab_table_compare_op_postfix(const void* left, const void* right);
/**
* Comparison function used to search the table for a value.
*/
int libab_table_compare_value(const void* left, const void* right);
/**
* Comparison function used to search the table for a basetype.
*/
int libab_table_compare_basetype(const void* left, const void* right);
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);
#endif