Add a unit type.

This commit is contained in:
Danila Fedorin 2018-06-02 15:37:04 -07:00
parent b7cc17d394
commit 969e457fba
2 changed files with 48 additions and 0 deletions

View File

@ -51,6 +51,10 @@ struct libab_s {
* The function list type instance.
*/
libab_ref type_function_list;
/**
* The unit type instance.
*/
libab_ref type_unit;
/**
* Internal; the number basetype. This cannot be a static
@ -152,6 +156,12 @@ libab_basetype* libab_get_basetype_function(libab* ab);
* @return the function list basetype.
*/
libab_basetype* libab_get_basetype_function_list(libab* ab);
/**
* Finds and returns the built-in libabacus unit type.
* @param ab the ab instance for which to return a type.
* @return the unit basetype.
*/
libab_basetype* libab_get_basetype_unit(libab* ab);
/**
* Get the type of a number in this libabacus instance.
@ -165,6 +175,12 @@ void libab_get_type_num(libab* ab, libab_ref* into);
* @param into the ference to store the type into.
*/
void libab_get_type_function_list(libab* ab, libab_ref* into);
/**
* Get the type of the unit in this libabacus instance.
* @param ab the instance to get the type for.
* @param into the reference to store the type into.
*/
void libab_get_type_unit(libab* ab, libab_ref* into);
/**
* Executes the given string of code.

View File

@ -23,6 +23,12 @@ static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}};
static libab_basetype _basetype_function = {_free_function,
_basetype_function_params, 1};
void _free_unit(void* unit) {
}
static libab_basetype _basetype_unit = { _free_unit, NULL, 0 };
libab_result _prepare_types(libab* ab, void (*free_function)(void*));
libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
@ -34,11 +40,15 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
libab_ref_null(&null_ref);
libab_ref_null(&ab->type_num);
libab_ref_null(&ab->type_function_list);
libab_ref_null(&ab->type_unit);
ab->impl.parse_num = parse_function;
result = libab_create_table(&ab->table, &null_ref);
if (result == LIBAB_SUCCESS) {
libab_ref_free(&ab->type_num);
libab_ref_free(&ab->type_function_list);
libab_ref_free(&ab->type_unit);
result = _prepare_types(ab, free_function);
}
@ -58,6 +68,7 @@ libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
libab_ref_free(&ab->table);
libab_ref_free(&ab->type_num);
libab_ref_free(&ab->type_function_list);
libab_ref_free(&ab->type_unit);
if (parser_initialized) {
libab_parser_free(&ab->parser);
@ -277,6 +288,7 @@ libab_result _prepare_types(libab* ab, void (*free_function)(void*)) {
libab_ref_null(&ab->type_num);
libab_ref_null(&ab->type_function_list);
libab_ref_null(&ab->type_unit);
if (result == LIBAB_SUCCESS) {
libab_ref_free(&ab->type_num);
@ -290,6 +302,11 @@ libab_result _prepare_types(libab* ab, void (*free_function)(void*)) {
&ab->type_function_list, 0);
}
if(result == LIBAB_SUCCESS) {
libab_ref_free(&ab->type_unit);
result = libab_instantiate_basetype(&_basetype_unit, &ab->type_unit, 0);
}
if (result == LIBAB_SUCCESS) {
result = libab_register_basetype(ab, "num", &ab->basetype_num);
}
@ -303,11 +320,17 @@ libab_result _prepare_types(libab* ab, void (*free_function)(void*)) {
&_basetype_function_list);
}
if (result == LIBAB_SUCCESS) {
result = libab_register_basetype(ab, "unit", &_basetype_unit);
}
if (result != LIBAB_SUCCESS) {
libab_ref_free(&ab->type_num);
libab_ref_free(&ab->type_function_list);
libab_ref_free(&ab->type_unit);
libab_ref_null(&ab->type_num);
libab_ref_null(&ab->type_function_list);
libab_ref_null(&ab->type_unit);
}
return result;
@ -340,6 +363,10 @@ libab_basetype* libab_get_basetype_function_list(libab* ab) {
return &_basetype_function_list;
}
libab_basetype* libab_get_basetype_unit(libab* ab) {
return &_basetype_unit;
}
void libab_get_type_num(libab* ab, libab_ref* into) {
libab_ref_copy(&ab->type_num, into);
}
@ -348,6 +375,10 @@ void libab_get_type_function_list(libab* ab, libab_ref* into) {
libab_ref_copy(&ab->type_function_list, into);
}
void libab_get_type_unit(libab* ab, libab_ref* into) {
libab_ref_copy(&ab->type_unit, into);
}
libab_result libab_run(libab* ab, const char* string, libab_ref* value) {
libab_result result = LIBAB_SUCCESS;
ll tokens;
@ -406,6 +437,7 @@ libab_result libab_free(libab* ab) {
libab_ref_free(&ab->table);
libab_ref_free(&ab->type_num);
libab_ref_free(&ab->type_function_list);
libab_ref_free(&ab->type_unit);
libab_parser_free(&ab->parser);
libab_interpreter_free(&ab->intr);
return libab_lexer_free(&ab->lexer);