BCI-dev/interpreter/method_dispatcher/method_dispatcher.h

88 lines
2.3 KiB
C
Raw Normal View History

2018-09-26 11:18:37 +00:00
#ifndef bci_method_dispatcher_h__
#define bci_method_dispatcher_h__
#include <inttypes.h>
#include "../base.h"
2018-09-26 11:18:37 +00:00
typedef struct dispatch_tree_node_s
{
struct dispatch_tree_node_s * higher;
struct dispatch_tree_node_s * lower;
uint16_t word;
bci_core_method_t method;
} * dispatch_tree_node_t;
typedef struct dispatch_tree_s
{
dispatch_tree_node_t root;
} * dispatch_tree_t;
dispatch_tree_t dispatch_tree_t_new(void);
/*
* This deletes the tree and will invoke dispatch_tree_node_t_del on
* tree->root (which will delete all the other nodes recursively).
* If you want to keep nodes you have to explicityl remove the reference to
* them by setting them to NULL.
* */
void dispatch_tree_t_del(dispatch_tree_t tree);
dispatch_tree_node_t dispatch_tree_node_t_new(uint16_t word
, bci_core_method_t method);
/*
* This deletes the node an will invoke dispatch_tree_node_t_del on
* node->higher and node->lower. If you want to keep those nodes
* you have to explicitly clear the reference by setting them to NULL.
* */
void dispatch_tree_node_t_del(dispatch_tree_node_t node);
/*
* Insert a new key-value-pair into the tree.
*
* Return codes:
* 0: OK
* 1: out of memory
* 2: key already in tree
* */
char dispatch_tree_t_insert(dispatch_tree_t tree
, uint16_t word
, bci_core_method_t method);
bci_core_method_t dispatch_tree_t_dispatch(dispatch_tree_t tree
, uint16_t word);
typedef struct dispatch_tree_autoinserter_s
{
uint16_t mayor;
uint16_t minor;
dispatch_tree_t tree;
} * dispatch_tree_autoinserter_t;
dispatch_tree_autoinserter_t dispatch_tree_autoinserter_t_new(dispatch_tree_t tree);
void dispatch_tree_autoinserter_t_del(dispatch_tree_autoinserter_t autoinserter);
/*
* This function inserts a new method into the tree and automatically
* chooses the optimal word to prevent degeneration.
*
* NOTE:
* When inserting mutliple methods this function acts deterministic,
* IF AND ONLY IF the order of the methods is not changed.
*
* The assembler will bring an analogous function that allows one
* to determine the opcodes of the methods.
*
* NOTE:
* This algorithm will not work for more that 1023 registered opcodes.
* This limit is however considered acceptable.
*
* */
uint16_t dispatch_tree_autoinserter_t_insert(
dispatch_tree_autoinserter_t autoinserter
, bci_core_method_t method);
2018-09-26 11:18:37 +00:00
#endif