Implement associativity storage for operators.

This commit is contained in:
Danila Fedorin 2018-02-17 16:00:39 -08:00
parent bf3c90bed2
commit b1aa2a4bf5
3 changed files with 11 additions and 6 deletions

View File

@ -17,6 +17,10 @@ struct libab_operator_s {
* The precedence of the operator. * The precedence of the operator.
*/ */
int precedence; int precedence;
/**
* The associativity of the operator.
*/
int associativity;
/** /**
* The functionality of the operator. * The functionality of the operator.
*/ */

View File

@ -45,10 +45,11 @@ libab_result libab_init(libab* ab);
* @param ab the libabacus instance to reigster the operator with. * @param ab the libabacus instance to reigster the operator with.
* @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 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, libab_function_ptr func); libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_function_ptr func);
/** /**
* Registers an operation with libabacus that appears * Registers an operation with libabacus that appears
* before its operand. * before its operand.

View File

@ -8,7 +8,7 @@ libab_result libab_init(libab* ab) {
return libab_lexer_init(&ab->lexer); return libab_lexer_init(&ab->lexer);
} }
libab_result _register_operator(libab* ab, const char* op, int token_type, int precedence, libab_function_ptr func) { libab_result _register_operator(libab* ab, const char* op, int token_type, int precedence, int associativity, 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)))) {
@ -35,16 +35,16 @@ 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, libab_function_ptr func) { libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_function_ptr func) {
return _register_operator(ab, op, TOKEN_OP_INFIX, precedence, func); return _register_operator(ab, op, TOKEN_OP_INFIX, precedence, associativity, 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, libab_function_ptr func) {
return _register_operator(ab, op, TOKEN_OP_PREFIX, 0, func); return _register_operator(ab, op, TOKEN_OP_PREFIX, 0, 0, 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, libab_function_ptr func) {
return _register_operator(ab, op, TOKEN_OP_POSTFIX, 0, func); return _register_operator(ab, op, TOKEN_OP_POSTFIX, 0, 0, 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, libab_function_ptr func) {