2018-02-11 21:52:28 -08:00
|
|
|
#include "libabacus.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"
|
|
|
|
#include <stdlib.h>
|
2018-02-11 21:52:28 -08:00
|
|
|
|
|
|
|
libab_result libab_init(libab* ab) {
|
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);
|
|
|
|
result = libab_create_table(&ab->table, &null_ref);
|
|
|
|
|
|
|
|
if(result == LIBAB_SUCCESS) {
|
|
|
|
libab_parser_init(&ab->parser, &ab->table);
|
2018-04-24 16:59:53 -07:00
|
|
|
libab_interpreter_init(&ab->intr, &ab->table, &ab->impl);
|
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-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-03-16 23:09:11 -07:00
|
|
|
libab_parser_free(&ab->parser);
|
|
|
|
libab_lexer_free(&ab->lexer);
|
|
|
|
}
|
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-04-21 14:09:01 -07:00
|
|
|
void _initialize_behavior(libab* ab, libab_behavior* behavior, libab_ref* type,
|
|
|
|
libab_function_ptr func) {
|
2018-04-20 00:25:31 -07:00
|
|
|
behavior->impl.variant = BIMPL_INTERNAL;
|
|
|
|
behavior->impl.data_u.internal = func;
|
2018-04-20 14:54:58 -07:00
|
|
|
libab_ref_copy(type, &behavior->type);
|
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,
|
|
|
|
libab_ref* type, libab_function_ptr func) {
|
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-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-02-11 22:50:44 -08:00
|
|
|
new_entry->data_u.op.precedence = precedence;
|
2018-02-21 19:06:00 -08:00
|
|
|
new_entry->data_u.op.associativity = associativity;
|
2018-03-17 17:38:13 -07:00
|
|
|
new_entry->data_u.op.type = token_type;
|
2018-04-20 14:54:58 -07:00
|
|
|
_initialize_behavior(ab, &(new_entry->data_u.op.behavior), type, func);
|
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) {
|
|
|
|
if (new_entry)
|
2018-04-17 22:14:07 -07:00
|
|
|
libab_ref_free(&new_entry->data_u.op.behavior.type);
|
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,
|
|
|
|
libab_ref* type,
|
|
|
|
libab_function_ptr func) {
|
|
|
|
return _register_operator(ab, op, OPERATOR_INFIX, precedence, associativity,
|
|
|
|
type, func);
|
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,
|
|
|
|
libab_ref* type,
|
|
|
|
libab_function_ptr func) {
|
2018-03-17 20:56:25 -07:00
|
|
|
return _register_operator(ab, op, OPERATOR_PREFIX, 0, 0, type, func);
|
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,
|
|
|
|
libab_ref* type,
|
|
|
|
libab_function_ptr func) {
|
2018-03-17 20:56:25 -07:00
|
|
|
return _register_operator(ab, op, OPERATOR_POSTFIX, 0, 0, type, func);
|
2018-02-17 14:00:37 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
libab_result libab_register_function(libab* ab, const char* name,
|
|
|
|
libab_ref* type, libab_function_ptr func) {
|
2018-02-11 22:50:44 -08: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-02-17 14:00:37 -08:00
|
|
|
new_entry->variant = ENTRY_FUN;
|
2018-04-21 14:09:01 -07:00
|
|
|
_initialize_behavior(ab, &(new_entry->data_u.function.behavior), type,
|
|
|
|
func);
|
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-04-24 11:32:57 -07:00
|
|
|
result = libab_table_put(libab_ref_get(&ab->table), name, new_entry);
|
2018-02-11 22:50:44 -08:00
|
|
|
}
|
|
|
|
|
2018-04-21 14:09:01 -07:00
|
|
|
if (result != LIBAB_SUCCESS) {
|
|
|
|
if (new_entry)
|
2018-04-17 22:14:07 -07:00
|
|
|
libab_ref_free(&new_entry->data_u.function.behavior.type);
|
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_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-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);
|
|
|
|
}
|
|
|
|
ll_foreach(&tokens, NULL, compare_always, libab_lexer_foreach_match_free);
|
|
|
|
ll_free(&tokens);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:52:28 -08:00
|
|
|
libab_result libab_free(libab* ab) {
|
2018-04-24 11:32:57 -07:00
|
|
|
libab_ref_free(&ab->table);
|
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);
|
|
|
|
}
|