2018-04-17 11:22:48 -07:00
|
|
|
#include "custom.h"
|
|
|
|
|
2018-05-16 15:26:03 -07:00
|
|
|
void libab_behavior_init_internal(libab_behavior* behavior,
|
2018-05-09 15:05:40 -07:00
|
|
|
libab_function_ptr func) {
|
2018-05-16 23:10:33 -07:00
|
|
|
behavior->variant = BIMPL_INTERNAL;
|
|
|
|
behavior->data_u.internal = func;
|
2018-05-09 15:05:40 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_behavior_init_tree(libab_behavior* behavior, libab_tree* tree) {
|
2018-05-16 23:10:33 -07:00
|
|
|
behavior->variant = BIMPL_TREE;
|
|
|
|
behavior->data_u.tree = tree;
|
2018-05-09 15:05:40 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 11:22:48 -07:00
|
|
|
void libab_behavior_free(libab_behavior* behavior) {
|
2018-05-16 23:10:33 -07:00
|
|
|
if (behavior->variant == BIMPL_TREE) {
|
|
|
|
libab_tree_free_recursive(behavior->data_u.tree);
|
2018-04-20 00:25:31 -07:00
|
|
|
}
|
2018-04-17 11:22:48 -07:00
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
void libab_operator_init(libab_operator* op, libab_operator_variant variant,
|
2018-05-09 15:05:40 -07:00
|
|
|
int precedence, int associativity, libab_ref* type,
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_function_ptr func) {
|
2018-05-16 15:29:14 -07:00
|
|
|
op->variant = variant;
|
2018-05-09 15:05:40 -07:00
|
|
|
op->precedence = precedence;
|
|
|
|
op->associativity = associativity;
|
2018-05-16 15:29:14 -07:00
|
|
|
libab_ref_copy(type, &op->type);
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_behavior_init_internal(&op->behavior, func);
|
2018-05-09 15:05:40 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 11:22:48 -07:00
|
|
|
void libab_operator_free(libab_operator* op) {
|
2018-05-16 15:29:14 -07:00
|
|
|
libab_ref_free(&op->type);
|
2018-04-17 11:22:48 -07:00
|
|
|
libab_behavior_free(&op->behavior);
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:45:32 -07:00
|
|
|
libab_result _function_init(libab_function* function) {
|
|
|
|
return libab_ref_vec_init(&function->params);
|
|
|
|
}
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_result libab_function_init_internal(libab_function* function,
|
2018-05-09 15:05:40 -07:00
|
|
|
libab_function_ptr fun) {
|
|
|
|
libab_result result = _function_init(function);
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_behavior_init_internal(&function->behavior, fun);
|
2018-05-09 15:05:40 -07:00
|
|
|
return result;
|
|
|
|
}
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_result libab_function_init_tree(libab_function* function,
|
2018-05-09 15:05:40 -07:00
|
|
|
libab_tree* tree) {
|
|
|
|
libab_result result = _function_init(function);
|
2018-05-16 15:26:03 -07:00
|
|
|
libab_behavior_init_tree(&function->behavior, tree);
|
2018-05-09 15:05:40 -07:00
|
|
|
return result;
|
|
|
|
}
|
2018-04-17 11:22:48 -07:00
|
|
|
void libab_function_free(libab_function* fun) {
|
|
|
|
libab_behavior_free(&fun->behavior);
|
2018-05-22 17:45:32 -07:00
|
|
|
libab_ref_vec_free(&fun->params);
|
2018-04-17 11:22:48 -07:00
|
|
|
}
|