2018-02-11 22:34:46 -08:00
|
|
|
#ifndef LIBABACUS_CUSTOM_H
|
|
|
|
#define LIBABACUS_CUSTOM_H
|
|
|
|
|
2018-03-15 19:41:11 -07:00
|
|
|
#include "parsetype.h"
|
2018-05-11 11:38:10 -07:00
|
|
|
#include "ref_trie.h"
|
2018-05-17 14:53:48 -07:00
|
|
|
#include "tree.h"
|
2018-03-15 19:41:11 -07:00
|
|
|
|
2018-05-27 00:02:20 -07:00
|
|
|
struct libab_s;
|
|
|
|
|
2018-02-11 22:34:46 -08:00
|
|
|
/**
|
|
|
|
* A function pointer that is called
|
|
|
|
* to execute a certain type of function.
|
|
|
|
*/
|
2018-06-21 14:25:11 -07:00
|
|
|
typedef libab_result (*libab_function_ptr)(struct libab_s*, libab_ref*, libab_ref_vec*, libab_ref*);
|
2018-02-11 22:34:46 -08:00
|
|
|
|
2018-03-17 20:56:25 -07:00
|
|
|
/**
|
|
|
|
* The variant of the operator that
|
|
|
|
* has been registered with libabacus.
|
|
|
|
*/
|
|
|
|
enum libab_operator_variant_e {
|
|
|
|
OPERATOR_PREFIX,
|
|
|
|
OPERATOR_INFIX,
|
|
|
|
OPERATOR_POSTFIX
|
|
|
|
};
|
|
|
|
|
2018-04-20 00:25:31 -07:00
|
|
|
/**
|
|
|
|
* The variant of the implementation of a behavior.
|
|
|
|
*/
|
2018-04-21 14:09:01 -07:00
|
|
|
enum libab_behavior_variant_e { BIMPL_INTERNAL, BIMPL_TREE };
|
2018-04-20 00:25:31 -07:00
|
|
|
|
|
|
|
/**
|
2018-05-16 23:10:33 -07:00
|
|
|
* The common information
|
|
|
|
* that both operators and functions shared.
|
2018-04-20 00:25:31 -07:00
|
|
|
*/
|
2018-05-16 23:10:33 -07:00
|
|
|
struct libab_behavior_s {
|
2018-04-20 00:25:31 -07:00
|
|
|
/**
|
2018-05-17 15:33:38 -07:00
|
|
|
* The variant of this behavior.
|
2018-04-20 00:25:31 -07:00
|
|
|
*/
|
|
|
|
enum libab_behavior_variant_e variant;
|
|
|
|
union {
|
|
|
|
/**
|
|
|
|
* The internal function used for an internal implementation.
|
|
|
|
*/
|
|
|
|
libab_function_ptr internal;
|
|
|
|
/**
|
|
|
|
* The tree-based implementation.
|
|
|
|
*/
|
2018-05-09 15:05:40 -07:00
|
|
|
libab_tree* tree;
|
2018-04-20 00:25:31 -07:00
|
|
|
} data_u;
|
|
|
|
};
|
|
|
|
|
2018-02-11 22:34:46 -08:00
|
|
|
/**
|
|
|
|
* A struct that holds informatiion
|
|
|
|
* about an operator that has been
|
|
|
|
* registered with libabacus.
|
|
|
|
*/
|
|
|
|
struct libab_operator_s {
|
2018-03-17 17:38:13 -07:00
|
|
|
/**
|
|
|
|
* The type of the operator (infix, prefix, postfix).
|
|
|
|
* Corresponds to token types associated with
|
|
|
|
* each operator.
|
|
|
|
*/
|
2018-05-16 15:29:14 -07:00
|
|
|
enum libab_operator_variant_e variant;
|
2018-02-11 22:34:46 -08:00
|
|
|
/**
|
|
|
|
* The precedence of the operator.
|
|
|
|
*/
|
|
|
|
int precedence;
|
2018-02-17 16:00:39 -08:00
|
|
|
/**
|
|
|
|
* The associativity of the operator.
|
|
|
|
*/
|
|
|
|
int associativity;
|
2018-02-11 22:34:46 -08:00
|
|
|
/**
|
2018-06-01 15:24:55 -07:00
|
|
|
* The function called by this operator.
|
2018-02-11 22:34:46 -08:00
|
|
|
*/
|
2018-06-01 15:24:55 -07:00
|
|
|
const char* function;
|
2018-02-11 22:34:46 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A struct that holds information
|
|
|
|
* about an function that has been
|
|
|
|
* registered with libabacus.
|
|
|
|
*/
|
|
|
|
struct libab_function_s {
|
|
|
|
/**
|
2018-03-15 19:41:11 -07:00
|
|
|
* The behavior of this function.
|
2018-02-11 22:34:46 -08:00
|
|
|
*/
|
2018-03-15 19:41:11 -07:00
|
|
|
struct libab_behavior_s behavior;
|
2018-05-22 17:45:32 -07:00
|
|
|
/**
|
|
|
|
* The parameters given to this function
|
|
|
|
* if it was created via partial application.
|
|
|
|
*/
|
|
|
|
libab_ref_vec params;
|
2018-05-28 01:18:06 -07:00
|
|
|
/**
|
|
|
|
* The scope in which this function was declared.
|
|
|
|
*/
|
|
|
|
libab_ref scope;
|
2018-02-11 22:34:46 -08:00
|
|
|
};
|
|
|
|
|
2018-03-17 20:56:25 -07:00
|
|
|
typedef enum libab_operator_variant_e libab_operator_variant;
|
2018-04-20 00:25:31 -07:00
|
|
|
typedef enum libab_behavior_variant_e libab_behavior_variant;
|
2018-03-15 19:41:11 -07:00
|
|
|
typedef struct libab_behavior_s libab_behavior;
|
2018-02-11 22:34:46 -08:00
|
|
|
typedef struct libab_operator_s libab_operator;
|
|
|
|
typedef struct libab_function_s libab_function;
|
|
|
|
|
2018-05-09 15:05:40 -07:00
|
|
|
/**
|
|
|
|
* Initializes a behavior that uses an internal function.
|
|
|
|
* @param behavior the behavior to initialize.
|
|
|
|
* @param func the function that this behavior calls.
|
|
|
|
*/
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_behavior_init_internal(libab_behavior* behavior,
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_function_ptr func);
|
2018-05-09 15:05:40 -07:00
|
|
|
/**
|
|
|
|
* Initializes a behavior that uses a tree that has been
|
|
|
|
* parsed from the user.
|
|
|
|
* @param behavior the behavior to initialize.
|
|
|
|
* @param tree the tree that this behavior uses.
|
|
|
|
*/
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree);
|
2018-06-02 20:56:10 -07:00
|
|
|
/**
|
|
|
|
* Copies given behavior into a new one.
|
|
|
|
* @param behavior the behavior to copy.
|
|
|
|
* @param into the behavior to copy into.
|
|
|
|
*/
|
|
|
|
void libab_behavior_copy(libab_behavior* behavior, libab_behavior* into);
|
2018-04-17 11:22:48 -07:00
|
|
|
/**
|
|
|
|
* Frees the given behavior.
|
|
|
|
* @param behavior the behavior to free.
|
|
|
|
*/
|
|
|
|
void libab_behavior_free(libab_behavior* behavior);
|
2018-05-09 15:05:40 -07:00
|
|
|
/**
|
|
|
|
* Initializes an operator with the given info.
|
|
|
|
* @param op the operator to initialize.
|
|
|
|
* @param variant the variant of the operator (infix, prefix, etc)
|
|
|
|
* @param precedence the precedence of the operator.
|
2018-05-17 14:53:48 -07:00
|
|
|
* @param associativity the associativity (left = -1, right = 1) of the
|
|
|
|
* operator.
|
2018-06-01 15:24:55 -07:00
|
|
|
* @param function the function this operator represents.
|
2018-08-22 17:45:45 -07:00
|
|
|
* @result the result of the initialization.
|
2018-05-09 15:05:40 -07:00
|
|
|
*/
|
2018-08-22 17:45:45 -07:00
|
|
|
libab_result libab_operator_init(libab_operator* op, libab_operator_variant variant,
|
2018-06-01 15:24:55 -07:00
|
|
|
int precedence, int associativity, const char* function);
|
2018-04-17 11:22:48 -07:00
|
|
|
/**
|
|
|
|
* Frees the given operator.
|
|
|
|
* @param op the operator to free.
|
|
|
|
*/
|
|
|
|
void libab_operator_free(libab_operator* op);
|
2018-05-09 15:05:40 -07:00
|
|
|
/**
|
|
|
|
* Initializes a function with the given internal behavior.
|
|
|
|
* @param function the function to initialize.
|
|
|
|
* @param fun the function implementation.
|
2018-05-28 01:18:06 -07:00
|
|
|
* @param the parent scope in which this function was declared.
|
2018-05-09 15:05:40 -07:00
|
|
|
* @return the result of the initialization.
|
|
|
|
*/
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_result libab_function_init_internal(libab_function* function,
|
2018-05-28 01:18:06 -07:00
|
|
|
libab_function_ptr fun,
|
|
|
|
libab_ref* scope);
|
2018-05-09 15:05:40 -07:00
|
|
|
/**
|
|
|
|
* Initializes a function with the given tree behavior.
|
|
|
|
* @param function the function to initialize.
|
|
|
|
* @param tree the tree that represents the function's behavior.
|
2018-05-28 01:18:06 -07:00
|
|
|
* @param scope the scope in which this function was declared.
|
2018-05-09 15:05:40 -07:00
|
|
|
* @return the result of the initialization.
|
|
|
|
*/
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_result libab_function_init_tree(libab_function* function,
|
2018-05-28 01:18:06 -07:00
|
|
|
libab_tree* tree,
|
|
|
|
libab_ref* scope);
|
2018-06-02 20:56:10 -07:00
|
|
|
/**
|
|
|
|
* Initializes a function with the given behavior, regardless of type.
|
|
|
|
* @param function the function to initialize.
|
|
|
|
* @param behavior the behavior to initialize this function with.
|
|
|
|
* @param scope the scope this function is in.
|
|
|
|
* @return the result of the initialization.
|
|
|
|
*/
|
|
|
|
libab_result libab_function_init_behavior(libab_function* function,
|
|
|
|
libab_behavior* behavior,
|
|
|
|
libab_ref* scope);
|
2018-04-17 11:22:48 -07:00
|
|
|
/**
|
|
|
|
* Frees the given function.
|
|
|
|
* @param fun the function to free.
|
|
|
|
*/
|
|
|
|
void libab_function_free(libab_function* fun);
|
|
|
|
|
2018-02-11 22:34:46 -08:00
|
|
|
#endif
|