2018-02-11 21:52:28 -08:00
|
|
|
#include "libabacus.h"
|
2018-05-26 21:55:30 -07:00
|
|
|
#include "debug.h"
|
2018-04-20 14:54:58 -07:00
|
|
|
#include "lexer.h"
|
2018-04-21 14:09:01 -07:00
|
|
|
#include "reserved.h"
|
|
|
|
#include "util.h"
|
2018-05-14 21:32:36 -07:00
|
|
|
#include "value.h"
|
2018-04-21 14:09:01 -07:00
|
|
|
#include <stdlib.h>
|
2018-06-15 20:38:19 -07:00
|
|
|
#include "free_functions.h"
|
2018-02-11 21:52:28 -08:00
|
|
|
|
2018-06-17 19:43:18 -07:00
|
|
|
static libab_basetype _basetype_function_list = {libab_free_function_list, NULL, 0};
|
2018-05-14 21:32:36 -07:00
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}};
|
2018-05-14 21:32:36 -07:00
|
|
|
|
2018-06-17 19:43:18 -07:00
|
|
|
static libab_basetype _basetype_function = {libab_free_function,
|
2018-05-17 14:53:48 -07:00
|
|
|
_basetype_function_params, 1};
|
2018-05-14 21:32:36 -07:00
|
|
|
|
2018-06-17 19:43:18 -07:00
|
|
|
static libab_basetype _basetype_unit = { libab_free_unit, NULL, 0 };
|
2018-06-02 15:37:04 -07:00
|
|
|
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_result _prepare_types(libab* ab, void (*free_function)(void*));
|
|
|
|
|
|
|
|
libab_result libab_init(libab* ab, void* (*parse_function)(const char*),
|
|
|
|
void (*free_function)(void*)) {
|
|
|
|
int parser_initialized = 0;
|
|
|
|
int lexer_initialized = 0;
|
2018-06-02 15:50:42 -07:00
|
|
|
int interpreter_initialized = 0;
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref null_ref;
|
2018-03-16 23:09:11 -07:00
|
|
|
libab_result result;
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref_null(&null_ref);
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_ref_null(&ab->type_num);
|
|
|
|
libab_ref_null(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_null(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
|
|
|
|
ab->impl.parse_num = parse_function;
|
2018-04-24 11:32:57 -07:00
|
|
|
result = libab_create_table(&ab->table, &null_ref);
|
|
|
|
|
2018-05-01 21:48:15 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_free(&ab->type_num);
|
|
|
|
libab_ref_free(&ab->type_function_list);
|
|
|
|
libab_ref_free(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
result = _prepare_types(ab, free_function);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-01 21:48:15 -07:00
|
|
|
parser_initialized = 1;
|
2018-05-11 20:36:27 -07:00
|
|
|
libab_parser_init(&ab->parser, ab);
|
2018-06-02 15:50:42 -07:00
|
|
|
result = libab_interpreter_init(&ab->intr, ab);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
interpreter_initialized = 1;
|
2018-04-24 11:32:57 -07:00
|
|
|
result = libab_lexer_init(&ab->lexer);
|
|
|
|
}
|
2018-03-16 23:09:11 -07:00
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-01 21:48:15 -07:00
|
|
|
lexer_initialized = 1;
|
2018-03-16 23:09:11 -07:00
|
|
|
result = libab_register_reserved_operators(&ab->lexer);
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref_free(&ab->table);
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_ref_free(&ab->type_num);
|
|
|
|
libab_ref_free(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_free(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (parser_initialized) {
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_parser_free(&ab->parser);
|
2018-06-02 15:50:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (interpreter_initialized) {
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_interpreter_free(&ab->intr);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (lexer_initialized) {
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_lexer_free(&ab->lexer);
|
|
|
|
}
|
2018-03-16 23:09:11 -07:00
|
|
|
}
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref_free(&null_ref);
|
2018-03-16 23:09:11 -07:00
|
|
|
|
|
|
|
return result;
|
2018-02-11 21:52:28 -08:00
|
|
|
}
|
|
|
|
|
2018-02-17 20:31:30 -08:00
|
|
|
void _sanitize(char* to, const char* from, size_t buffer_size) {
|
|
|
|
size_t index = 0;
|
2018-04-21 14:09:01 -07:00
|
|
|
while (*from && index < (buffer_size - 2)) {
|
|
|
|
if (*from == '+' || *from == '*' || *from == '\\')
|
|
|
|
to[index++] = '\\';
|
2018-02-17 20:31:30 -08:00
|
|
|
to[index++] = *(from++);
|
|
|
|
}
|
|
|
|
to[index] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:48:15 -07:00
|
|
|
void _initialize_behavior(libab_behavior* behavior, libab_ref* type,
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_function_ptr func) {
|
2018-05-16 23:10:33 -07:00
|
|
|
behavior->variant = BIMPL_INTERNAL;
|
|
|
|
behavior->data_u.internal = func;
|
2018-03-15 19:41:11 -07:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result _register_operator(libab* ab, const char* op,
|
|
|
|
libab_operator_variant token_type,
|
|
|
|
int precedence, int associativity,
|
2018-06-01 15:24:55 -07:00
|
|
|
const char* function) {
|
2018-02-17 20:31:30 -08:00
|
|
|
char op_buffer[8];
|
2018-02-11 22:50:44 -08:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
libab_table_entry* new_entry;
|
2018-05-09 15:05:40 -07:00
|
|
|
libab_operator* new_operator = NULL;
|
2018-04-21 14:09:01 -07:00
|
|
|
if ((new_entry = malloc(sizeof(*new_entry)))) {
|
2018-02-17 14:00:37 -08:00
|
|
|
new_entry->variant = ENTRY_OP;
|
2018-05-09 15:05:40 -07:00
|
|
|
new_operator = &(new_entry->data_u.op);
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_operator_init(new_operator, token_type, precedence, associativity,
|
2018-06-01 15:24:55 -07:00
|
|
|
function);
|
2018-02-11 22:50:44 -08:00
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-02-17 20:31:30 -08:00
|
|
|
_sanitize(op_buffer, op, 8);
|
2018-04-21 14:09:01 -07:00
|
|
|
result = libab_convert_lex_result(
|
|
|
|
eval_config_add(&ab->lexer.config, op_buffer, TOKEN_OP));
|
2018-02-17 14:00:37 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-04-24 11:32:57 -07:00
|
|
|
result = libab_table_put(libab_ref_get(&ab->table), op, new_entry);
|
2018-02-11 22:50:44 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-05-09 15:05:40 -07:00
|
|
|
if (new_operator)
|
|
|
|
libab_operator_free(new_operator);
|
2018-03-17 20:56:25 -07:00
|
|
|
eval_config_remove(&ab->lexer.config, op, TOKEN_OP);
|
2018-02-11 22:50:44 -08:00
|
|
|
free(new_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_register_operator_infix(libab* ab, const char* op,
|
|
|
|
int precedence, int associativity,
|
2018-06-01 15:24:55 -07:00
|
|
|
const char* function) {
|
2018-04-21 14:09:01 -07:00
|
|
|
return _register_operator(ab, op, OPERATOR_INFIX, precedence, associativity,
|
2018-06-01 15:24:55 -07:00
|
|
|
function);
|
2018-02-17 14:00:37 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_register_operator_prefix(libab* ab, const char* op,
|
2018-06-01 15:24:55 -07:00
|
|
|
const char* function) {
|
|
|
|
return _register_operator(ab, op, OPERATOR_PREFIX, 0, 0, function);
|
2018-02-17 14:00:37 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_register_operator_postfix(libab* ab, const char* op,
|
2018-06-01 15:24:55 -07:00
|
|
|
const char* function) {
|
|
|
|
return _register_operator(ab, op, OPERATOR_POSTFIX, 0, 0, function);
|
2018-02-17 14:00:37 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_result _create_value_function_internal(libab_ref* into, libab_ref* type,
|
2018-05-28 01:18:06 -07:00
|
|
|
libab_function_ptr func,
|
|
|
|
libab_ref* scope) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref function_ref;
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result result =
|
2018-06-17 19:43:18 -07:00
|
|
|
libab_create_function_internal(&function_ref, libab_free_function, func, scope);
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref_null(into);
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref_free(into);
|
|
|
|
result = libab_create_value_ref(into, &function_ref, type);
|
|
|
|
}
|
|
|
|
libab_ref_free(&function_ref);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result _create_value_function_list(libab_ref* into, libab_ref* type) {
|
|
|
|
libab_ref list_ref;
|
|
|
|
libab_result result = libab_create_function_list(&list_ref, type);
|
|
|
|
libab_ref_null(into);
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref_free(into);
|
|
|
|
result = libab_create_value_ref(into, &list_ref, type);
|
|
|
|
}
|
|
|
|
libab_ref_free(&list_ref);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result _libab_register_function_existing(libab* ab,
|
|
|
|
libab_table_entry* entry,
|
|
|
|
libab_ref* function_val) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_value* old_value;
|
|
|
|
libab_parsetype* old_type;
|
2018-02-11 22:50:44 -08:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
2018-05-14 21:32:36 -07:00
|
|
|
|
|
|
|
old_value = libab_ref_get(&entry->data_u.value);
|
|
|
|
old_type = libab_ref_get(&old_value->type);
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (old_type->data_u.base == &_basetype_function_list) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_function_list* list = libab_ref_get(&old_value->data);
|
|
|
|
result = libab_function_list_insert(list, function_val);
|
2018-05-17 14:53:48 -07:00
|
|
|
} else if (old_type->data_u.base == &_basetype_function) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref new_list;
|
2018-05-17 14:53:48 -07:00
|
|
|
result =
|
|
|
|
_create_value_function_list(&new_list, &ab->type_function_list);
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
libab_function_list* list =
|
|
|
|
libab_ref_get(&((libab_value*)libab_ref_get(&new_list))->data);
|
2018-05-14 21:32:36 -07:00
|
|
|
result = libab_function_list_insert(list, &entry->data_u.value);
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-14 21:32:36 -07:00
|
|
|
result = libab_function_list_insert(list, function_val);
|
|
|
|
}
|
|
|
|
}
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref_swap(&entry->data_u.value, &new_list);
|
|
|
|
}
|
|
|
|
libab_ref_free(&new_list);
|
2018-02-11 22:50:44 -08:00
|
|
|
} else {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_ref_swap(&entry->data_u.value, function_val);
|
2018-02-11 22:50:44 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 21:32:36 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result _libab_register_function_new(libab* ab, const char* name,
|
|
|
|
libab_ref* function_val) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
libab_table_entry* entry;
|
2018-05-17 14:53:48 -07:00
|
|
|
if ((entry = malloc(sizeof(*entry)))) {
|
2018-05-14 21:32:36 -07:00
|
|
|
entry->variant = ENTRY_VALUE;
|
|
|
|
libab_ref_copy(function_val, &entry->data_u.value);
|
|
|
|
result = libab_table_put(libab_ref_get(&ab->table), name, entry);
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_table_entry_free(entry);
|
|
|
|
free(entry);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = LIBAB_MALLOC;
|
2018-02-11 22:50:44 -08:00
|
|
|
}
|
|
|
|
|
2018-05-14 21:32:36 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_register_function(libab* ab, const char* name,
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_ref* type, libab_function_ptr func) {
|
2018-05-14 21:32:36 -07:00
|
|
|
libab_table_entry* existing_entry;
|
|
|
|
libab_ref function_value;
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_result result =
|
2018-05-28 01:18:06 -07:00
|
|
|
_create_value_function_internal(&function_value, type, func, &ab->table);
|
2018-05-17 14:53:48 -07:00
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
existing_entry = libab_table_search_filter(
|
|
|
|
libab_ref_get(&ab->table), name, NULL, libab_table_compare_value);
|
|
|
|
if (existing_entry) {
|
|
|
|
result = _libab_register_function_existing(ab, existing_entry,
|
|
|
|
&function_value);
|
2018-05-14 21:32:36 -07:00
|
|
|
} else {
|
|
|
|
result = _libab_register_function_new(ab, name, &function_value);
|
|
|
|
}
|
|
|
|
libab_ref_free(&function_value);
|
2018-02-11 22:50:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_register_basetype(libab* ab, const char* name,
|
|
|
|
libab_basetype* basetype) {
|
2018-04-17 12:07:22 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
libab_table_entry* new_entry;
|
2018-04-21 14:09:01 -07:00
|
|
|
if ((new_entry = malloc(sizeof(*new_entry)))) {
|
2018-04-17 12:07:22 -07:00
|
|
|
new_entry->variant = ENTRY_BASETYPE;
|
|
|
|
new_entry->data_u.basetype = basetype;
|
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-04-24 11:32:57 -07:00
|
|
|
result = libab_table_put(libab_ref_get(&ab->table), name, new_entry);
|
2018-04-17 12:07:22 -07:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-04-17 12:07:22 -07:00
|
|
|
free(new_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_result _prepare_types(libab* ab, void (*free_function)(void*)) {
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
|
|
|
|
ab->basetype_num.count = 0;
|
|
|
|
ab->basetype_num.params = NULL;
|
|
|
|
ab->basetype_num.free_function = free_function;
|
|
|
|
|
|
|
|
libab_ref_null(&ab->type_num);
|
|
|
|
libab_ref_null(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_null(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
libab_ref_free(&ab->type_num);
|
2018-05-17 14:53:48 -07:00
|
|
|
result =
|
|
|
|
libab_instantiate_basetype(&ab->basetype_num, &ab->type_num, 0);
|
2018-05-01 21:48:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
libab_ref_free(&ab->type_function_list);
|
|
|
|
result = libab_instantiate_basetype(&_basetype_function_list,
|
|
|
|
&ab->type_function_list, 0);
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:04 -07:00
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
libab_ref_free(&ab->type_unit);
|
|
|
|
result = libab_instantiate_basetype(&_basetype_unit, &ab->type_unit, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:48:15 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
result = libab_register_basetype(ab, "num", &ab->basetype_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
result = libab_register_basetype(ab, "function", &_basetype_function);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-17 14:53:48 -07:00
|
|
|
result = libab_register_basetype(ab, "function_list",
|
2018-05-01 21:48:15 -07:00
|
|
|
&_basetype_function_list);
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:04 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
|
|
|
result = libab_register_basetype(ab, "unit", &_basetype_unit);
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_ref_free(&ab->type_num);
|
|
|
|
libab_ref_free(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_free(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_ref_null(&ab->type_num);
|
|
|
|
libab_ref_null(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_null(&ab->type_unit);
|
2018-05-01 21:48:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:54:23 -07:00
|
|
|
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);
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-04-20 14:54:23 -07:00
|
|
|
result = libab_parser_parse_type(&ab->parser, &tokens, type, into);
|
|
|
|
}
|
2018-05-14 21:32:36 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-06-17 19:01:15 -07:00
|
|
|
result = libab_resolve_parsetype_inplace(libab_ref_get(into),
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_ref_get(&ab->table));
|
2018-05-14 21:32:36 -07:00
|
|
|
}
|
2018-04-20 14:54:23 -07:00
|
|
|
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);
|
|
|
|
ll_free(&tokens);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:53:48 -07:00
|
|
|
libab_basetype* libab_get_basetype_num(libab* ab) { return &ab->basetype_num; }
|
2018-05-11 20:12:03 -07:00
|
|
|
|
|
|
|
libab_basetype* libab_get_basetype_function(libab* ab) {
|
|
|
|
return &_basetype_function;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_basetype* libab_get_basetype_function_list(libab* ab) {
|
|
|
|
return &_basetype_function_list;
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_basetype* libab_get_basetype_unit(libab* ab) {
|
|
|
|
return &_basetype_unit;
|
|
|
|
}
|
|
|
|
|
2018-05-27 00:11:00 -07:00
|
|
|
void libab_get_type_num(libab* ab, libab_ref* into) {
|
|
|
|
libab_ref_copy(&ab->type_num, into);
|
|
|
|
}
|
|
|
|
|
|
|
|
void libab_get_type_function_list(libab* ab, libab_ref* into) {
|
|
|
|
libab_ref_copy(&ab->type_function_list, into);
|
|
|
|
}
|
|
|
|
|
2018-06-02 15:37:04 -07:00
|
|
|
void libab_get_type_unit(libab* ab, libab_ref* into) {
|
|
|
|
libab_ref_copy(&ab->type_unit, into);
|
|
|
|
}
|
|
|
|
|
2018-06-02 16:06:13 -07:00
|
|
|
void libab_get_unit_value(libab* ab, libab_ref* into) {
|
|
|
|
libab_interpreter_unit_value(&ab->intr, into);
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:53 -07:00
|
|
|
libab_result _create_tree(libab* ab, const char* string, libab_tree** into) {
|
2018-05-18 14:15:15 -07:00
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
ll tokens;
|
2018-05-26 21:55:30 -07:00
|
|
|
|
2018-05-18 14:15:15 -07:00
|
|
|
ll_init(&tokens);
|
2018-06-21 17:06:53 -07:00
|
|
|
*into = NULL;
|
2018-05-18 14:15:15 -07:00
|
|
|
result = libab_lexer_lex(&ab->lexer, string, &tokens);
|
|
|
|
|
2018-06-21 17:06:53 -07:00
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
result = libab_parser_parse(&ab->parser, &tokens, string, into);
|
|
|
|
}
|
|
|
|
|
|
|
|
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);
|
|
|
|
ll_free(&tokens);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result _handle_va_params(libab* ab, libab_ref_vec* into, size_t param_count, va_list args) {
|
|
|
|
libab_result result = libab_ref_vec_init(into);
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
while(result == LIBAB_SUCCESS && param_count--) {
|
|
|
|
result = libab_ref_vec_insert(into, va_arg(args, libab_ref*));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result != LIBAB_SUCCESS) {
|
|
|
|
libab_ref_vec_free(into);
|
|
|
|
}
|
2018-05-18 14:15:15 -07:00
|
|
|
}
|
2018-06-21 17:06:53 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_run(libab* ab, const char* string, libab_ref* value) {
|
|
|
|
libab_result result;
|
|
|
|
libab_tree* root;
|
|
|
|
|
|
|
|
libab_ref_null(value);
|
|
|
|
result = _create_tree(ab, string, &root);
|
2018-05-18 14:15:15 -07:00
|
|
|
|
2018-05-26 21:55:30 -07:00
|
|
|
if (result == LIBAB_SUCCESS) {
|
2018-05-18 14:15:15 -07:00
|
|
|
libab_ref_free(value);
|
2018-06-21 17:06:53 -07:00
|
|
|
result = libab_interpreter_run(&ab->intr, root, &ab->table, SCOPE_FORCE, value);
|
2018-05-18 14:15:15 -07:00
|
|
|
libab_tree_free_recursive(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-01 23:32:19 -07:00
|
|
|
libab_result libab_run_function(libab* ab, const char* function,
|
|
|
|
libab_ref* into,
|
|
|
|
size_t param_count, ...) {
|
|
|
|
libab_ref_vec params;
|
|
|
|
va_list args;
|
|
|
|
libab_result result = LIBAB_SUCCESS;
|
|
|
|
|
|
|
|
va_start(args, param_count);
|
|
|
|
libab_ref_null(into);
|
2018-06-21 17:06:53 -07:00
|
|
|
result = _handle_va_params(ab, ¶ms, param_count, args);
|
2018-06-01 23:32:19 -07:00
|
|
|
if(result == LIBAB_SUCCESS) {
|
2018-06-21 17:06:53 -07:00
|
|
|
libab_ref_free(into);
|
|
|
|
result = libab_interpreter_run_function(&ab->intr, &ab->table, function, ¶ms, into);
|
2018-06-01 23:32:19 -07:00
|
|
|
|
2018-06-21 17:06:53 -07:00
|
|
|
libab_ref_vec_free(¶ms);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_run_tree(libab* ab, libab_tree* tree, libab_ref* value) {
|
|
|
|
return libab_interpreter_run(&ab->intr, tree, &ab->table, SCOPE_FORCE, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_run_scoped(libab* ab, const char* string, libab_ref* scope, libab_ref* into) {
|
|
|
|
libab_result result;
|
|
|
|
libab_tree* root;
|
|
|
|
|
|
|
|
libab_ref_null(into);
|
|
|
|
result = _create_tree(ab, string, &root);
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
libab_ref_free(into);
|
|
|
|
result = libab_interpreter_run(&ab->intr, root, scope, SCOPE_NONE, into);
|
|
|
|
libab_tree_free_recursive(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
libab_result libab_run_function_scoped(libab* ab, const char* function, libab_ref* scope, libab_ref* into,
|
|
|
|
size_t param_count, ...) {
|
|
|
|
libab_ref_vec params;
|
|
|
|
libab_result result;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, param_count);
|
|
|
|
libab_ref_null(into);
|
|
|
|
result = _handle_va_params(ab, ¶ms, param_count, args);
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
libab_ref_free(into);
|
|
|
|
result = libab_interpreter_run_function(&ab->intr, scope, function, ¶ms, into);
|
2018-06-01 23:32:19 -07:00
|
|
|
|
|
|
|
libab_ref_vec_free(¶ms);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:53 -07:00
|
|
|
libab_result libab_run_tree_scoped(libab* ab, libab_tree* tree, libab_ref* scope, libab_ref* into) {
|
|
|
|
return libab_interpreter_run(&ab->intr, tree, scope, SCOPE_NONE, into);
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:52:28 -08:00
|
|
|
libab_result libab_free(libab* ab) {
|
2018-05-31 19:45:36 -07:00
|
|
|
libab_table_free(libab_ref_get(&ab->table));
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref_free(&ab->table);
|
2018-05-01 21:48:15 -07:00
|
|
|
libab_ref_free(&ab->type_num);
|
|
|
|
libab_ref_free(&ab->type_function_list);
|
2018-06-02 15:37:04 -07:00
|
|
|
libab_ref_free(&ab->type_unit);
|
2018-02-11 22:23:02 -08:00
|
|
|
libab_parser_free(&ab->parser);
|
2018-04-24 11:35:27 -07:00
|
|
|
libab_interpreter_free(&ab->intr);
|
2018-02-11 21:52:28 -08:00
|
|
|
return libab_lexer_free(&ab->lexer);
|
|
|
|
}
|