Add function to compile a type from its string.

This commit is contained in:
Danila Fedorin 2018-04-20 14:54:23 -07:00
parent 8214aa8344
commit 7692d4541f
2 changed files with 26 additions and 4 deletions

View File

@ -50,7 +50,7 @@ libab_result libab_init(libab* ab);
* @param func the function that describes the functionality of the operator.
* @return the result of the initialization.
*/
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, const char* type, libab_function_ptr func);
libab_result libab_register_operator_infix(libab* ab, const char* op, int precedence, int associativity, libab_ref* type, libab_function_ptr func);
/**
* Registers an operation with libabacus that appears
* before its operand.
@ -60,7 +60,7 @@ libab_result libab_register_operator_infix(libab* ab, const char* op, int preced
* @param func the function that describes the functionality of the operator.
* @return the result of the registration.
*/
libab_result libab_register_operator_prefix(libab* ab, const char* op, const char* type, libab_function_ptr func);
libab_result libab_register_operator_prefix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func);
/**
* Registers an operation with libabacus that appears
* after its operand.
@ -70,7 +70,7 @@ libab_result libab_register_operator_prefix(libab* ab, const char* op, const cha
* @param func the function that describes the functionality of the operator.
* @return the result of the registration.
*/
libab_result libab_register_operator_postfix(libab* ab, const char* op, const char* type, libab_function_ptr func);
libab_result libab_register_operator_postfix(libab* ab, const char* op, libab_ref* type, libab_function_ptr func);
/**
* Registers a function with libabacus.
@ -80,7 +80,7 @@ libab_result libab_register_operator_postfix(libab* ab, const char* op, const ch
* @param func the function that describes the functionality of the function.
* @return the result of the registration.
*/
libab_result libab_register_function(libab* ab, const char* name, const char* type, libab_function_ptr func);
libab_result libab_register_function(libab* ab, const char* name, libab_ref* type, libab_function_ptr func);
/**
* Registers a base type with abacus.
* @param ab the libabacus instance used to keep state.
@ -89,6 +89,15 @@ libab_result libab_register_function(libab* ab, const char* name, const char* ty
* @return the result of the registration.
*/
libab_result libab_register_basetype(libab* ab, const char* name, libab_basetype* basetype);
/**
* Constructs and resolves a parse type, similarly to how it's done in the
* parser.
* @param ab the libab instance to use for constructing the type.
* @param into the reference to populate with the given type.
* @param type the type to parse.
* @return the result of the operation.
*/
libab_result libab_create_type(libab* ab, libab_ref* into, const char* type);
/**
* Releases all the resources allocated by libabacus.
* @param ab the libabacus instance to release.

View File

@ -149,6 +149,19 @@ libab_result libab_register_basetype(libab* ab, const char* name, libab_basetype
return result;
}
libab_result libab_create_type(libab* ab, libab_ref* into, const char* type) {
libab_result result;
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, into);
}
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);
ll_free(&tokens);
return result;
}
libab_result libab_free(libab* ab) {
libab_table_free(&ab->table);
libab_parser_free(&ab->parser);