Store function / operator types when they are registered.
This commit is contained in:
parent
429c0bde3e
commit
ab2311def9
|
@ -1,12 +1,28 @@
|
||||||
#ifndef LIBABACUS_CUSTOM_H
|
#ifndef LIBABACUS_CUSTOM_H
|
||||||
#define LIBABACUS_CUSTOM_H
|
#define LIBABACUS_CUSTOM_H
|
||||||
|
|
||||||
|
#include "parsetype.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A function pointer that is called
|
* A function pointer that is called
|
||||||
* to execute a certain type of function.
|
* to execute a certain type of function.
|
||||||
*/
|
*/
|
||||||
typedef void(*libab_function_ptr)();
|
typedef void(*libab_function_ptr)();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The common information
|
||||||
|
* that both operators and functions shared.
|
||||||
|
*/
|
||||||
|
struct libab_behavior_s {
|
||||||
|
/**
|
||||||
|
* The function that handles the parameters.
|
||||||
|
*/
|
||||||
|
libab_function_ptr function;
|
||||||
|
/**
|
||||||
|
* The type of the function.
|
||||||
|
*/
|
||||||
|
libab_parsetype* type;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* A struct that holds informatiion
|
* A struct that holds informatiion
|
||||||
* about an operator that has been
|
* about an operator that has been
|
||||||
|
@ -22,9 +38,9 @@ struct libab_operator_s {
|
||||||
*/
|
*/
|
||||||
int associativity;
|
int associativity;
|
||||||
/**
|
/**
|
||||||
* The functionality of the operator.
|
* The behavior of this operator.
|
||||||
*/
|
*/
|
||||||
libab_function_ptr function;
|
struct libab_behavior_s behavior;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,11 +50,12 @@ struct libab_operator_s {
|
||||||
*/
|
*/
|
||||||
struct libab_function_s {
|
struct libab_function_s {
|
||||||
/**
|
/**
|
||||||
* The functionality of the function.
|
* The behavior of this function.
|
||||||
*/
|
*/
|
||||||
libab_function_ptr function;
|
struct libab_behavior_s behavior;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct libab_behavior_s libab_behavior;
|
||||||
typedef struct libab_operator_s libab_operator;
|
typedef struct libab_operator_s libab_operator;
|
||||||
typedef struct libab_function_s libab_function;
|
typedef struct libab_function_s libab_function;
|
||||||
|
|
||||||
|
|
|
@ -46,37 +46,41 @@ libab_result libab_init(libab* ab);
|
||||||
* @param op the operator string to register.
|
* @param op the operator string to register.
|
||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param associativity the associativity of the operator.
|
* @param associativity the associativity of the operator.
|
||||||
|
* @param type the type of this operator.
|
||||||
* @param func the function that describes the functionality of the operator.
|
* @param func the function that describes the functionality of the operator.
|
||||||
* @return the result of the initialization.
|
* @return the result of the initialization.
|
||||||
*/
|
*/
|
||||||
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_function_ptr func);
|
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, const char* type, libab_function_ptr func);
|
||||||
/**
|
/**
|
||||||
* Registers an operation with libabacus that appears
|
* Registers an operation with libabacus that appears
|
||||||
* before its operand.
|
* before its operand.
|
||||||
* @param ab the libabacus instance to register the operator with.
|
* @param ab the libabacus instance to register the operator with.
|
||||||
* @param op the operator string to register.
|
* @param op the operator string to register.
|
||||||
|
* @param type the type of this operator.
|
||||||
* @param func the function that describes the functionality of the operator.
|
* @param func the function that describes the functionality of the operator.
|
||||||
* @return the result of the registration.
|
* @return the result of the registration.
|
||||||
*/
|
*/
|
||||||
libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_function_ptr func);
|
libab_result libab_register_operator_prefix(libab* ab, const char* op, const char* type, libab_function_ptr func);
|
||||||
/**
|
/**
|
||||||
* Registers an operation with libabacus that appears
|
* Registers an operation with libabacus that appears
|
||||||
* after its operand.
|
* after its operand.
|
||||||
* @param ab the libabacus instance to register the operator with.
|
* @param ab the libabacus instance to register the operator with.
|
||||||
* @param op the operator string to register.
|
* @param op the operator string to register.
|
||||||
|
* @param type the type of this operator.
|
||||||
* @param func the function that describes the functionality of the operator.
|
* @param func the function that describes the functionality of the operator.
|
||||||
* @return the result of the registration.
|
* @return the result of the registration.
|
||||||
*/
|
*/
|
||||||
libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_function_ptr func);
|
libab_result libab_register_operator_postfix(libab* ab, const char* op, const char* type, libab_function_ptr func);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a function with libabacus.
|
* Registers a function with libabacus.
|
||||||
* @param ab the libabacus instance used to keep state.
|
* @param ab the libabacus instance used to keep state.
|
||||||
* @param name the name of the function.
|
* @param name the name of the function.
|
||||||
|
* @param type the type of this operator.
|
||||||
* @param func the function that describes the functionality of the function.
|
* @param func the function that describes the functionality of the function.
|
||||||
* @return the result of the initialization.
|
* @return the result of the initialization.
|
||||||
*/
|
*/
|
||||||
libab_result libab_register_function(libab* ab, const char* name, libab_function_ptr func);
|
libab_result libab_register_function(libab* ab, const char* name, const char* type, libab_function_ptr func);
|
||||||
/**
|
/**
|
||||||
* Releases all the resources allocated by libabacus.
|
* Releases all the resources allocated by libabacus.
|
||||||
* @param ab the libabacus instance to release.
|
* @param ab the libabacus instance to release.
|
||||||
|
|
|
@ -17,19 +17,41 @@ void _sanitize(char* to, const char* from, size_t buffer_size) {
|
||||||
to[index] = '\0';
|
to[index] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result _register_operator(libab* ab, const char* op, int token_type, int precedence, int associativity, libab_function_ptr func) {
|
libab_result _initialize_behavior(libab* ab, libab_behavior* behavior,
|
||||||
|
const char* type, libab_function_ptr func) {
|
||||||
|
libab_result result = LIBAB_SUCCESS;
|
||||||
|
ll tokens;
|
||||||
|
|
||||||
|
ll_init(&tokens);
|
||||||
|
|
||||||
|
result = libab_lexer_lex(&ab->lexer, type, &tokens);
|
||||||
|
if(result == LIBAB_SUCCESS) {
|
||||||
|
result = libab_parser_parse_type(&ab->parser, &tokens, type, &behavior->type);
|
||||||
|
}
|
||||||
|
|
||||||
|
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);
|
||||||
|
ll_free(&tokens);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
libab_result _register_operator(libab* ab, const char* op, int token_type, int precedence, int associativity, const char* type, libab_function_ptr func) {
|
||||||
char op_buffer[8];
|
char op_buffer[8];
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_table_entry* new_entry;
|
libab_table_entry* new_entry;
|
||||||
if((new_entry = malloc(sizeof(*new_entry)))) {
|
if((new_entry = malloc(sizeof(*new_entry)))) {
|
||||||
new_entry->variant = ENTRY_OP;
|
new_entry->variant = ENTRY_OP;
|
||||||
new_entry->data_u.op.function = func;
|
new_entry->data_u.op.behavior.type = NULL;
|
||||||
new_entry->data_u.op.precedence = precedence;
|
new_entry->data_u.op.precedence = precedence;
|
||||||
new_entry->data_u.op.associativity = associativity;
|
new_entry->data_u.op.associativity = associativity;
|
||||||
} else {
|
} else {
|
||||||
result = LIBAB_MALLOC;
|
result = LIBAB_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(result == LIBAB_SUCCESS) {
|
||||||
|
result = _initialize_behavior(ab, &(new_entry->data_u.op.behavior), type, func);
|
||||||
|
}
|
||||||
|
|
||||||
if(result == LIBAB_SUCCESS) {
|
if(result == LIBAB_SUCCESS) {
|
||||||
_sanitize(op_buffer, op, 8);
|
_sanitize(op_buffer, op, 8);
|
||||||
result = libab_convert_lex_result(eval_config_add(&ab->lexer.config, op_buffer, token_type));
|
result = libab_convert_lex_result(eval_config_add(&ab->lexer.config, op_buffer, token_type));
|
||||||
|
@ -40,6 +62,8 @@ libab_result _register_operator(libab* ab, const char* op, int token_type, int p
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result != LIBAB_SUCCESS) {
|
if(result != LIBAB_SUCCESS) {
|
||||||
|
if(new_entry && new_entry->data_u.op.behavior.type)
|
||||||
|
libab_parsetype_free_recursive(new_entry->data_u.op.behavior.type);
|
||||||
eval_config_remove(&ab->lexer.config, op, token_type);
|
eval_config_remove(&ab->lexer.config, op, token_type);
|
||||||
free(new_entry);
|
free(new_entry);
|
||||||
}
|
}
|
||||||
|
@ -47,33 +71,40 @@ libab_result _register_operator(libab* ab, const char* op, int token_type, int p
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_function_ptr func) {
|
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, const char* type, libab_function_ptr func) {
|
||||||
return _register_operator(ab, op, TOKEN_OP_INFIX, precedence, associativity, func);
|
return _register_operator(ab, op, TOKEN_OP_INFIX, precedence, associativity, type, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_function_ptr func) {
|
libab_result libab_register_operator_prefix(libab* ab, const char* op, const char* type, libab_function_ptr func) {
|
||||||
return _register_operator(ab, op, TOKEN_OP_PREFIX, 0, 0, func);
|
return _register_operator(ab, op, TOKEN_OP_PREFIX, 0, 0, type, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_function_ptr func) {
|
libab_result libab_register_operator_postfix(libab* ab, const char* op, const char* type, libab_function_ptr func) {
|
||||||
return _register_operator(ab, op, TOKEN_OP_POSTFIX, 0, 0, func);
|
return _register_operator(ab, op, TOKEN_OP_POSTFIX, 0, 0, type, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
libab_result libab_register_function(libab* ab, const char* name, libab_function_ptr func) {
|
libab_result libab_register_function(libab* ab, const char* name, const char* type, libab_function_ptr func) {
|
||||||
libab_result result = LIBAB_SUCCESS;
|
libab_result result = LIBAB_SUCCESS;
|
||||||
libab_table_entry* new_entry;
|
libab_table_entry* new_entry;
|
||||||
if((new_entry = malloc(sizeof(*new_entry)))) {
|
if((new_entry = malloc(sizeof(*new_entry)))) {
|
||||||
new_entry->variant = ENTRY_FUN;
|
new_entry->variant = ENTRY_FUN;
|
||||||
new_entry->data_u.function.function = func;
|
new_entry->data_u.function.behavior.type = NULL;
|
||||||
} else {
|
} else {
|
||||||
result = LIBAB_MALLOC;
|
result = LIBAB_MALLOC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(result == LIBAB_SUCCESS) {
|
||||||
|
result = _initialize_behavior(ab,
|
||||||
|
&(new_entry->data_u.function.behavior), type, func);
|
||||||
|
}
|
||||||
|
|
||||||
if(result == LIBAB_SUCCESS) {
|
if(result == LIBAB_SUCCESS) {
|
||||||
result = libab_table_put(&ab->table, name, new_entry);
|
result = libab_table_put(&ab->table, name, new_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result != LIBAB_SUCCESS) {
|
if(result != LIBAB_SUCCESS) {
|
||||||
|
if(new_entry && new_entry->data_u.function.behavior.type)
|
||||||
|
libab_parsetype_free_recursive(new_entry->data_u.function.behavior.type);
|
||||||
free(new_entry);
|
free(new_entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,5 +43,9 @@ void libab_table_free(libab_table* table) {
|
||||||
ht_free(&table->table);
|
ht_free(&table->table);
|
||||||
}
|
}
|
||||||
void libab_table_entry_free(libab_table_entry* entry) {
|
void libab_table_entry_free(libab_table_entry* entry) {
|
||||||
|
if(entry->variant == ENTRY_OP) {
|
||||||
|
libab_parsetype_free_recursive(entry->data_u.op.behavior.type);
|
||||||
|
} else if(entry->variant == ENTRY_FUN) {
|
||||||
|
libab_parsetype_free_recursive(entry->data_u.function.behavior.type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user