Compare commits
8 Commits
5492aa6f63
...
22b738a4f0
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | 22b738a4f0 | |
Danila Fedorin | 88ec979ba5 | |
Danila Fedorin | 763cfdd7a3 | |
Danila Fedorin | 065554bcdf | |
Danila Fedorin | 27c79d44f5 | |
Danila Fedorin | 28f7cd1b30 | |
Danila Fedorin | 695fbed235 | |
Danila Fedorin | 1158b29c1b |
|
@ -8,7 +8,7 @@ project(libabacus)
|
|||
|
||||
add_compile_options(-pedantic -Wall)
|
||||
|
||||
add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c)
|
||||
add_library(abacus STATIC src/lexer.c src/util.c src/table.c src/parser.c src/libabacus.c src/tree.c src/debug.c src/parsetype.c src/reserved.c src/trie.c src/refcount.c src/ref_vec.c src/ref_trie.c src/basetype.c src/value.c src/custom.c src/interpreter.c src/function_list.c src/free_functions.c)
|
||||
add_executable(libabacus src/main.c)
|
||||
add_executable(interactive src/interactive.c)
|
||||
add_subdirectory(external/liblex)
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef LIBABACUS_FREE_FUNCTIONS_H
|
||||
#define LIBABACUS_FREE_FUNCTIONS_H
|
||||
|
||||
/**
|
||||
* Free functions. Because a lot of the reference
|
||||
* counting operations require free functions,
|
||||
* and redeclaring them in mutliple files makes no
|
||||
* sense (also, it doesn't link :^) ), we
|
||||
* put them all here.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Frees a libab_function.
|
||||
* @param func the function to free.
|
||||
*/
|
||||
void free_function(void* func);
|
||||
/**
|
||||
* Frees a libab_function_list.
|
||||
* @param func_list the function list to free.
|
||||
*/
|
||||
void free_function_list(void* func_list);
|
||||
/**
|
||||
* Frees a unit. This is a no-op.
|
||||
* @param unit the unit to free.
|
||||
*/
|
||||
void free_unit(void* unit);
|
||||
/**
|
||||
* Frees a parsetype.
|
||||
* @param parsetype the parsetype to free.
|
||||
*/
|
||||
void free_parsetype(void* parsetype);
|
||||
/**
|
||||
* Frees a table.
|
||||
* @param table the table to free.
|
||||
*/
|
||||
void free_table(void* table);
|
||||
/**
|
||||
* Frees a value.
|
||||
* @param value the value to free.
|
||||
*/
|
||||
void free_value(void* value);
|
||||
|
||||
#endif
|
|
@ -8,6 +8,16 @@
|
|||
|
||||
struct libab_s;
|
||||
|
||||
/**
|
||||
* Scope moe used to determine how the interpreter handles
|
||||
* scoping.
|
||||
*/
|
||||
enum libab_interpreter_scope_mode_e {
|
||||
SCOPE_NORMAL,
|
||||
SCOPE_FORCE,
|
||||
SCOPE_NONE
|
||||
};
|
||||
|
||||
/**
|
||||
* The interpreter struct used to encapsulate
|
||||
* any interpreter-specific data.
|
||||
|
@ -20,6 +30,7 @@ struct libab_interpreter_s {
|
|||
libab_ref value_unit;
|
||||
};
|
||||
|
||||
typedef enum libab_interpreter_scope_mode_e libab_interpreter_scope_mode;
|
||||
typedef struct libab_interpreter_s libab_interpreter;
|
||||
|
||||
/**
|
||||
|
@ -32,11 +43,13 @@ libab_result libab_interpreter_init(libab_interpreter* intr, struct libab_s* ab)
|
|||
* Uses the interpreter to run the given parse tree.
|
||||
* @param intr the interpreter to use to run the code.
|
||||
* @param tree the tree to run.
|
||||
* @param mode the scope mode to use.
|
||||
* @param into the reference into which the result of the execution will be
|
||||
* stored.
|
||||
* @return the result of the execution.
|
||||
*/
|
||||
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
|
||||
libab_interpreter_scope_mode mode,
|
||||
libab_ref* into);
|
||||
/**
|
||||
* Calls a function with the given parameters.
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "refcount.h"
|
||||
#include "result.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
/**
|
||||
* A node in the trie.
|
||||
|
@ -75,6 +76,16 @@ libab_result libab_ref_trie_put(libab_ref_trie* trie, const char* key,
|
|||
*/
|
||||
void libab_ref_trie_get(const libab_ref_trie* trie, const char* key,
|
||||
libab_ref* into);
|
||||
/**
|
||||
* Calls the given function on every element in the reference-counted
|
||||
* tree.
|
||||
* @param trie the trie to run the operation on.
|
||||
* @param func the function to call.
|
||||
* return the result of the functions, and any necessary allocations.
|
||||
*/
|
||||
libab_result libab_ref_trie_foreach(const libab_ref_trie* trie,
|
||||
libab_result (*func)(const libab_ref*, const char*, va_list),
|
||||
...);
|
||||
/**
|
||||
* Releases the trie, decrementing the refcounts of all
|
||||
* the values stored inside.
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
#include "free_functions.h"
|
||||
#include "custom.h"
|
||||
#include "function_list.h"
|
||||
#include "parsetype.h"
|
||||
#include "table.h"
|
||||
#include "value.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void free_function(void* func) {
|
||||
libab_function_free(func);
|
||||
free(func);
|
||||
}
|
||||
void free_function_list(void* function_list) {
|
||||
libab_function_list_free(function_list);
|
||||
free(function_list);
|
||||
}
|
||||
void free_unit(void* unit) {
|
||||
|
||||
}
|
||||
void free_parsetype(void* parsetype) {
|
||||
libab_parsetype_free(parsetype);
|
||||
free(parsetype);
|
||||
}
|
||||
void free_table(void* table) {
|
||||
libab_table_free(table);
|
||||
free(table);
|
||||
}
|
||||
void free_value(void* value) {
|
||||
libab_value_free(value);
|
||||
free(value);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#include "libabacus.h"
|
||||
#include "util.h"
|
||||
#include "value.h"
|
||||
#include "free_functions.h"
|
||||
|
||||
libab_result libab_interpreter_init(libab_interpreter* intr, libab* ab) {
|
||||
libab_result result;
|
||||
|
@ -73,6 +74,28 @@ int _interpreter_type_contains_placeholders(libab_ref* type) {
|
|||
return placeholder;
|
||||
}
|
||||
|
||||
void _interpreter_search_type_param(libab_ref_trie* params,
|
||||
libab_ref* scope,
|
||||
const char* name,
|
||||
libab_ref* into) {
|
||||
libab_ref_trie_get(params, name, into);
|
||||
|
||||
if(libab_ref_get(into) == NULL) {
|
||||
libab_table_search_type_param(libab_ref_get(scope), name, into);
|
||||
}
|
||||
}
|
||||
|
||||
libab_parsetype* _interpreter_search_type_param_raw(libab_ref_trie* params,
|
||||
libab_ref* scope,
|
||||
const char* name) {
|
||||
libab_ref into;
|
||||
libab_parsetype* to_return;
|
||||
_interpreter_search_type_param(params, scope, name, &into);
|
||||
to_return = libab_ref_get(&into);
|
||||
libab_ref_free(&into);
|
||||
return to_return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the two types, filling in any missing type parameters
|
||||
* in the respective type tries.
|
||||
|
@ -85,13 +108,13 @@ int _interpreter_type_contains_placeholders(libab_ref* type) {
|
|||
libab_result _interpreter_compare_types(libab_ref* left_type,
|
||||
libab_ref* right_type,
|
||||
libab_ref_trie* left_params,
|
||||
libab_ref_trie* right_params) {
|
||||
libab_ref_trie* right_params,
|
||||
libab_ref* scope) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
int left_placeholder;
|
||||
int right_placeholder;
|
||||
libab_parsetype* left = libab_ref_get(left_type);
|
||||
libab_parsetype* right = libab_ref_get(right_type);
|
||||
libab_ref param_type;
|
||||
|
||||
left_placeholder = left->variant & LIBABACUS_TYPE_F_PLACE;
|
||||
right_placeholder = right->variant & LIBABACUS_TYPE_F_PLACE;
|
||||
|
@ -100,9 +123,7 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
|
|||
} else {
|
||||
if (left_placeholder) {
|
||||
const char* name = left->data_u.name;
|
||||
libab_ref_trie_get(left_params, name, ¶m_type);
|
||||
left = libab_ref_get(¶m_type);
|
||||
libab_ref_free(¶m_type);
|
||||
left = _interpreter_search_type_param_raw(left_params, scope, name);
|
||||
if (left == NULL) {
|
||||
if (!_interpreter_type_contains_placeholders(right_type)) {
|
||||
result = libab_ref_trie_put(left_params, name, right_type);
|
||||
|
@ -112,9 +133,7 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
|
|||
}
|
||||
} else if (right_placeholder) {
|
||||
const char* name = right->data_u.name;
|
||||
libab_ref_trie_get(right_params, name, ¶m_type);
|
||||
right = libab_ref_get(¶m_type);
|
||||
libab_ref_free(¶m_type);
|
||||
right = _interpreter_search_type_param_raw(right_params, scope, name);
|
||||
if (right == NULL) {
|
||||
if (!_interpreter_type_contains_placeholders(left_type)) {
|
||||
result = libab_ref_trie_put(right_params, name, left_type);
|
||||
|
@ -148,7 +167,7 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
|
|||
libab_ref_vec_index(&left->children, index, &temp_left);
|
||||
libab_ref_vec_index(&right->children, index, &temp_right);
|
||||
result = _interpreter_compare_types(
|
||||
&temp_left, &temp_right, left_params, right_params);
|
||||
&temp_left, &temp_right, left_params, right_params, scope);
|
||||
libab_ref_free(&temp_left);
|
||||
libab_ref_free(&temp_right);
|
||||
}
|
||||
|
@ -159,11 +178,6 @@ libab_result _interpreter_compare_types(libab_ref* left_type,
|
|||
return result;
|
||||
}
|
||||
|
||||
void _free_parsetype(void* parsetype) {
|
||||
libab_parsetype_free(parsetype);
|
||||
free(parsetype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a type, substituting type parameters for their copies
|
||||
* from the parameter trie.
|
||||
|
@ -174,6 +188,7 @@ void _free_parsetype(void* parsetype) {
|
|||
*/
|
||||
libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
||||
libab_ref_trie* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_parsetype* copy;
|
||||
|
@ -181,7 +196,7 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
|||
|
||||
original = libab_ref_get(type);
|
||||
if (original->variant & LIBABACUS_TYPE_F_PLACE) {
|
||||
libab_ref_trie_get(params, original->data_u.name, into);
|
||||
_interpreter_search_type_param(params, scope, original->data_u.name, into);
|
||||
} else if ((copy = malloc(sizeof(*copy)))) {
|
||||
size_t index = 0;
|
||||
copy->variant = original->variant;
|
||||
|
@ -194,7 +209,7 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
|||
index++) {
|
||||
libab_ref_vec_index(&original->children, index, &temp_child);
|
||||
result = _interpreter_copy_resolved_type(&temp_child, params,
|
||||
&child_copy);
|
||||
scope, &child_copy);
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_vec_insert(©->children, &child_copy);
|
||||
}
|
||||
|
@ -208,9 +223,9 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
|||
}
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_new(into, copy, _free_parsetype);
|
||||
result = libab_ref_new(into, copy, free_parsetype);
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
_free_parsetype(copy);
|
||||
free_parsetype(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -234,10 +249,11 @@ libab_result _interpreter_copy_resolved_type(libab_ref* type,
|
|||
*/
|
||||
libab_result _interpreter_resolve_type_params(libab_ref* type,
|
||||
libab_ref_trie* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
if (_interpreter_type_contains_placeholders(type)) {
|
||||
result = _interpreter_copy_resolved_type(type, params, into);
|
||||
result = _interpreter_copy_resolved_type(type, params, scope, into);
|
||||
} else {
|
||||
libab_ref_copy(type, into);
|
||||
}
|
||||
|
@ -254,7 +270,9 @@ libab_result _interpreter_resolve_type_params(libab_ref* type,
|
|||
*/
|
||||
libab_result _interpreter_check_types(libab_ref_vec* reference_types,
|
||||
libab_ref_vec* params,
|
||||
libab_ref_vec* types) {
|
||||
libab_ref_vec* types,
|
||||
libab_ref* scope,
|
||||
libab_ref_trie* param_map) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref_trie function_params;
|
||||
|
||||
|
@ -277,11 +295,11 @@ libab_result _interpreter_check_types(libab_ref_vec* reference_types,
|
|||
right_temp =
|
||||
&((libab_value*)libab_ref_get(&right_value_temp))->type;
|
||||
result = _interpreter_compare_types(
|
||||
&left_temp, right_temp, &function_params, &child_params);
|
||||
&left_temp, right_temp, &function_params, &child_params, scope);
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = _interpreter_resolve_type_params(
|
||||
right_temp, &child_params, &produced_type);
|
||||
right_temp, &child_params, scope, &produced_type);
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
libab_ref_free(&produced_type);
|
||||
}
|
||||
|
@ -299,9 +317,10 @@ libab_result _interpreter_check_types(libab_ref_vec* reference_types,
|
|||
}
|
||||
}
|
||||
|
||||
libab_ref_trie_free(&function_params);
|
||||
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
*param_map = function_params;
|
||||
} else {
|
||||
libab_ref_trie_free(&function_params);
|
||||
libab_ref_vec_clear(types);
|
||||
}
|
||||
|
||||
|
@ -320,23 +339,29 @@ libab_result _interpreter_check_types(libab_ref_vec* reference_types,
|
|||
*/
|
||||
libab_result _interpreter_find_match(libab_function_list* function_values,
|
||||
libab_ref_vec* params,
|
||||
libab_ref_vec* new_types, libab_ref* match,
|
||||
libab_ref_vec* new_types,
|
||||
libab_ref_trie* param_map,
|
||||
libab_ref* match,
|
||||
int partial) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
size_t index = 0;
|
||||
size_t list_size = libab_function_list_size(function_values);
|
||||
int found_match = 0;
|
||||
libab_ref_trie temp_param_map;
|
||||
libab_ref_vec temp_new_types;
|
||||
libab_ref temp_function_value;
|
||||
libab_value* temp_value;
|
||||
libab_parsetype* temp_function_type;
|
||||
libab_function* temp_function;
|
||||
|
||||
libab_ref_null(match);
|
||||
result = libab_ref_vec_init(&temp_new_types);
|
||||
|
||||
for (; index < list_size && result == LIBAB_SUCCESS; index++) {
|
||||
libab_function_list_index(function_values, index, &temp_function_value);
|
||||
temp_function_type = libab_ref_get(
|
||||
&((libab_value*)libab_ref_get(&temp_function_value))->type);
|
||||
temp_value = libab_ref_get(&temp_function_value);
|
||||
temp_function_type = libab_ref_get(&temp_value->type);
|
||||
temp_function = libab_ref_get(&temp_value->data);
|
||||
|
||||
if (((temp_function_type->children.size == params->size + 1) &&
|
||||
!partial) ||
|
||||
|
@ -344,7 +369,7 @@ libab_result _interpreter_find_match(libab_function_list* function_values,
|
|||
partial)) {
|
||||
/* We found a function that has the correct number of parameters. */
|
||||
result = _interpreter_check_types(
|
||||
&temp_function_type->children, params, &temp_new_types);
|
||||
&temp_function_type->children, params, &temp_new_types, &temp_function->scope, &temp_param_map);
|
||||
if (result == LIBAB_MISMATCHED_TYPE) {
|
||||
/* Mismatch is OK. */
|
||||
result = LIBAB_SUCCESS;
|
||||
|
@ -356,6 +381,7 @@ libab_result _interpreter_find_match(libab_function_list* function_values,
|
|||
* new_types, and use new memory for temp list. */
|
||||
found_match = 1;
|
||||
*new_types = temp_new_types;
|
||||
*param_map = temp_param_map;
|
||||
libab_ref_free(match);
|
||||
libab_ref_copy(&temp_function_value, match);
|
||||
result = libab_ref_vec_init(&temp_new_types);
|
||||
|
@ -367,13 +393,17 @@ libab_result _interpreter_find_match(libab_function_list* function_values,
|
|||
* initialized, and the call is ambigous. Free all data. */
|
||||
libab_ref_vec_free(new_types);
|
||||
libab_ref_vec_free(&temp_new_types);
|
||||
libab_ref_trie_free(param_map);
|
||||
libab_ref_trie_free(&temp_param_map);
|
||||
result = LIBAB_AMBIGOUS_CALL;
|
||||
}
|
||||
} else {
|
||||
/* Something bad happened. Free data as best as we can. */
|
||||
libab_ref_vec_free(&temp_new_types);
|
||||
if (found_match)
|
||||
if (found_match) {
|
||||
libab_ref_vec_free(new_types);
|
||||
libab_ref_trie_free(param_map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -439,16 +469,48 @@ libab_result _interpreter_cast_params(libab_ref_vec* params,
|
|||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
||||
libab_ref* into, libab_ref* scope,
|
||||
libab_interpreter_scope_mode scope_mode);
|
||||
|
||||
/**
|
||||
* Calls a tree-based function with the given parameters.
|
||||
* @param tree the tree function to call.
|
||||
* @param the parameters to give to the function.
|
||||
* @param scope the scope used for the call.
|
||||
* @param into the reference to store the result into;
|
||||
* @return the result of the call.
|
||||
*/
|
||||
libab_result _interpreter_call_tree(libab_tree* tree, libab_ref_vec* params,
|
||||
libab_result _interpreter_call_tree(struct interpreter_state* state,
|
||||
libab_tree* tree,
|
||||
libab_ref_vec* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref new_scope;
|
||||
libab_tree* child;
|
||||
libab_ref param;
|
||||
libab_table* new_scope_raw;
|
||||
size_t i;
|
||||
libab_result result = libab_create_table(&new_scope, scope);
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
new_scope_raw = libab_ref_get(&new_scope);
|
||||
for(i = 0; i < tree->children.size - 1 && result == LIBAB_SUCCESS; i++) {
|
||||
child = vec_index(&tree->children, i);
|
||||
libab_ref_vec_index(params, i, ¶m);
|
||||
result = libab_put_table_value(new_scope_raw, child->string_value, ¶m);
|
||||
libab_ref_free(¶m);
|
||||
}
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = _interpreter_run(state,
|
||||
vec_index(&tree->children, tree->children.size - 1),
|
||||
into, &new_scope, SCOPE_NONE);
|
||||
}
|
||||
|
||||
libab_ref_free(&new_scope);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -463,12 +525,13 @@ libab_result _interpreter_call_tree(libab_tree* tree, libab_ref_vec* params,
|
|||
libab_result _interpreter_call_behavior(struct interpreter_state* state,
|
||||
libab_behavior* behavior,
|
||||
libab_ref_vec* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
if (behavior->variant == BIMPL_INTERNAL) {
|
||||
result = behavior->data_u.internal(state->ab, params, into);
|
||||
} else {
|
||||
result = _interpreter_call_tree(behavior->data_u.tree, params, into);
|
||||
result = _interpreter_call_tree(state, behavior->data_u.tree, params, scope, into);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -479,19 +542,21 @@ libab_result _interpreter_call_behavior(struct interpreter_state* state,
|
|||
* @param into the reference to store the copy into.
|
||||
*/
|
||||
libab_result _interpreter_copy_function_basic(libab_ref* function,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_function* func = libab_ref_get(function);
|
||||
void (*free_function)(void*) = function->count->free_func;
|
||||
return libab_create_function_behavior(into, free_function, &func->behavior, &func->scope);
|
||||
return libab_create_function_behavior(into, free_function, &func->behavior, scope);
|
||||
}
|
||||
|
||||
libab_result _interpreter_copy_function_with_params(libab_ref* function,
|
||||
libab_ref_vec* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
int index = 0;
|
||||
libab_ref param;
|
||||
libab_function* func;
|
||||
libab_result result = _interpreter_copy_function_basic(function, into);
|
||||
libab_result result = _interpreter_copy_function_basic(function, scope, into);
|
||||
func = libab_ref_get(into);
|
||||
|
||||
for(; index < params->size && result == LIBAB_SUCCESS; index++) {
|
||||
|
@ -555,6 +620,7 @@ libab_result _interpreter_copy_type_offset(libab_ref* type,
|
|||
libab_result _interpreter_partially_apply(struct interpreter_state* state,
|
||||
libab_ref* function,
|
||||
libab_ref_vec* params,
|
||||
libab_ref* scope,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_value* value;
|
||||
|
@ -563,7 +629,7 @@ libab_result _interpreter_partially_apply(struct interpreter_state* state,
|
|||
|
||||
value = libab_ref_get(function);
|
||||
libab_ref_null(&new_type);
|
||||
result = _interpreter_copy_function_with_params(&value->data, params, &new_function);
|
||||
result = _interpreter_copy_function_with_params(&value->data, params, scope, &new_function);
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(&new_type);
|
||||
result = _interpreter_copy_type_offset(&value->type, 0, &new_type);
|
||||
|
@ -581,6 +647,44 @@ libab_result _interpreter_partially_apply(struct interpreter_state* state,
|
|||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_foreach_insert_param(const libab_ref* param,
|
||||
const char* key,
|
||||
va_list args) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_table_entry* entry;
|
||||
|
||||
if ((entry = malloc(sizeof(*entry)))) {
|
||||
entry->variant = ENTRY_TYPE_PARAM;
|
||||
libab_ref_copy(param, &entry->data_u.type_param);
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = libab_table_put(libab_ref_get(va_arg(args, libab_ref*)), key, entry);
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_free(&entry->data_u.type_param);
|
||||
free(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_create_scope(libab_ref* into,
|
||||
libab_ref* parent_scope,
|
||||
libab_ref_trie* param_map) {
|
||||
libab_result result = libab_create_table(into, parent_scope);
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_trie_foreach(param_map, _interpreter_foreach_insert_param, into);
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
libab_ref_null(into);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/**
|
||||
* Calls a function with the given, compatible paramters.
|
||||
* @param state the state to use to call the function.
|
||||
|
@ -592,21 +696,32 @@ libab_result _interpreter_partially_apply(struct interpreter_state* state,
|
|||
libab_result _interpreter_perform_function_call(struct interpreter_state* state,
|
||||
libab_ref* to_call,
|
||||
libab_ref_vec* params,
|
||||
libab_ref_trie* param_map,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_value* function_value;
|
||||
libab_function* function;
|
||||
libab_parsetype* function_type;
|
||||
libab_ref new_scope;
|
||||
size_t new_params;
|
||||
|
||||
function_value = libab_ref_get(to_call);
|
||||
function = libab_ref_get(&function_value->data);
|
||||
function_type = libab_ref_get(&function_value->type);
|
||||
new_params = params->size - function->params.size;
|
||||
if (function_type->children.size - new_params == 1) {
|
||||
result = _interpreter_call_behavior(state, &function->behavior, params, into);
|
||||
|
||||
result = _interpreter_create_scope(&new_scope, &function->scope, param_map);
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_null(into);
|
||||
} else if (function_type->children.size - new_params == 1) {
|
||||
result = _interpreter_call_behavior(state, &function->behavior, params, &new_scope, into);
|
||||
} else {
|
||||
result = _interpreter_partially_apply(state, to_call, params, into);
|
||||
result = _interpreter_partially_apply(state, to_call, params, &new_scope, into);
|
||||
}
|
||||
|
||||
libab_ref_free(&new_scope);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -621,7 +736,9 @@ libab_result _interpreter_perform_function_call(struct interpreter_state* state,
|
|||
*/
|
||||
libab_result _interpreter_cast_and_perform_function_call(
|
||||
struct interpreter_state* state,
|
||||
libab_ref* to_call, libab_ref_vec* params, libab_ref_vec* new_types,
|
||||
libab_ref* to_call, libab_ref_vec* params,
|
||||
libab_ref_vec* new_types,
|
||||
libab_ref_trie* param_map,
|
||||
libab_ref* into) {
|
||||
libab_result result;
|
||||
libab_ref_vec new_params;
|
||||
|
@ -636,7 +753,7 @@ libab_result _interpreter_cast_and_perform_function_call(
|
|||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = _interpreter_perform_function_call(state, to_call,
|
||||
&new_params, into);
|
||||
&new_params, param_map, into);
|
||||
}
|
||||
|
||||
libab_ref_vec_free(&new_params);
|
||||
|
@ -659,13 +776,14 @@ libab_result _interpreter_call_function_list(struct interpreter_state* state,
|
|||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref_vec new_types;
|
||||
libab_ref to_call;
|
||||
libab_ref_trie param_map;
|
||||
libab_ref_null(into);
|
||||
|
||||
result =
|
||||
_interpreter_find_match(list, params, &new_types, &to_call, 0);
|
||||
_interpreter_find_match(list, params, &new_types, ¶m_map, &to_call, 0);
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
if (libab_ref_get(&to_call) == NULL) {
|
||||
result = _interpreter_find_match(list, params, &new_types,
|
||||
result = _interpreter_find_match(list, params, &new_types, ¶m_map,
|
||||
&to_call, 1);
|
||||
}
|
||||
}
|
||||
|
@ -677,7 +795,8 @@ libab_result _interpreter_call_function_list(struct interpreter_state* state,
|
|||
if (result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
result = _interpreter_cast_and_perform_function_call(state, &to_call, params,
|
||||
&new_types, into);
|
||||
&new_types, ¶m_map, into);
|
||||
libab_ref_trie_free(¶m_map);
|
||||
libab_ref_vec_free(&new_types);
|
||||
}
|
||||
|
||||
|
@ -700,22 +819,26 @@ libab_result _interpreter_call_function(struct interpreter_state* state,
|
|||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref_vec temp_new_types;
|
||||
libab_ref_trie param_map;
|
||||
libab_value* function_value;
|
||||
libab_parsetype* function_type;
|
||||
libab_function* function_instance;
|
||||
|
||||
function_value = libab_ref_get(function);
|
||||
function_type = libab_ref_get(&function_value->type);
|
||||
function_instance = libab_ref_get(&function_value->data);
|
||||
libab_ref_null(into);
|
||||
|
||||
result = libab_ref_vec_init(&temp_new_types);
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = _interpreter_check_types(&function_type->children,
|
||||
params, &temp_new_types);
|
||||
params, &temp_new_types, &function_instance->scope, ¶m_map);
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
result = _interpreter_cast_and_perform_function_call(
|
||||
state, function, params, &temp_new_types, into);
|
||||
state, function, params, &temp_new_types, ¶m_map, into);
|
||||
libab_ref_trie_free(¶m_map);
|
||||
}
|
||||
|
||||
libab_ref_vec_free(&temp_new_types);
|
||||
|
@ -756,10 +879,6 @@ libab_result _interpreter_try_call(struct interpreter_state* state,
|
|||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
||||
libab_ref* into, libab_ref* scope,
|
||||
int force_scope);
|
||||
|
||||
libab_result _interpreter_require_value(libab_ref* scope, const char* name,
|
||||
libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
|
@ -787,7 +906,7 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||
if (result == LIBAB_SUCCESS) {
|
||||
/* Get first parameter. */
|
||||
result =
|
||||
_interpreter_run(state, va_arg(args, libab_tree*), &temp, scope, 0);
|
||||
_interpreter_run(state, va_arg(args, libab_tree*), &temp, scope, SCOPE_NORMAL);
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_vec_insert(¶ms, &temp);
|
||||
|
@ -798,7 +917,7 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||
/* If infix, get second parameter. */
|
||||
if (result == LIBAB_SUCCESS && to_call->variant == OPERATOR_INFIX) {
|
||||
result = _interpreter_run(state, va_arg(args, libab_tree*), &temp,
|
||||
scope, 0);
|
||||
scope, SCOPE_NORMAL);
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_vec_insert(¶ms, &temp);
|
||||
|
@ -824,7 +943,7 @@ libab_result _interpreter_call_operator(struct interpreter_state* state,
|
|||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
||||
libab_result _interpreter_run_call_node(struct interpreter_state* state,
|
||||
libab_tree* tree, libab_ref* into,
|
||||
libab_ref* scope) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
|
@ -840,7 +959,7 @@ libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
|||
count++) {
|
||||
libab_ref_free(¶m);
|
||||
child = vec_index(&tree->children, count);
|
||||
result = _interpreter_run(state, child, ¶m, scope, 0);
|
||||
result = _interpreter_run(state, child, ¶m, scope, SCOPE_NORMAL);
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_vec_insert(¶ms, ¶m);
|
||||
|
@ -855,7 +974,7 @@ libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
|||
if (result == LIBAB_SUCCESS) {
|
||||
result = _interpreter_run(
|
||||
state, vec_index(&tree->children, tree->children.size - 1), &callee,
|
||||
scope, 0);
|
||||
scope, SCOPE_NORMAL);
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
libab_ref_vec_free(¶ms);
|
||||
}
|
||||
|
@ -872,12 +991,119 @@ libab_result _interpreter_run_function_node(struct interpreter_state* state,
|
|||
return result;
|
||||
}
|
||||
|
||||
int _interpreter_compare_function_param(
|
||||
const void* left, const void* right) {
|
||||
const libab_tree* data = right;
|
||||
return data->variant == TREE_FUN_PARAM;
|
||||
}
|
||||
|
||||
int _interpreter_foreach_resolve_function_param(
|
||||
void* data, va_list args) {
|
||||
libab_tree* tree = data;
|
||||
libab_ref* scope = va_arg(args, libab_ref*);
|
||||
libab_parsetype* type = libab_ref_get(&tree->type);
|
||||
|
||||
return libab_resolve_parsetype(type, libab_ref_get(scope));
|
||||
}
|
||||
|
||||
libab_result _interpreter_resolve_function_params(
|
||||
struct interpreter_state* state, libab_tree* tree,
|
||||
libab_ref* scope) {
|
||||
return vec_foreach(&tree->children, NULL, _interpreter_compare_function_param,
|
||||
_interpreter_foreach_resolve_function_param, scope);
|
||||
}
|
||||
|
||||
int _interpreter_foreach_insert_function_param(
|
||||
void* data, va_list args) {
|
||||
libab_tree* tree = data;
|
||||
libab_ref_vec* into = va_arg(args, libab_ref_vec*);
|
||||
return libab_ref_vec_insert(into, &tree->type);
|
||||
}
|
||||
|
||||
libab_result _interpreter_create_function_type(
|
||||
struct interpreter_state* state, libab_tree* tree, libab_parsetype** type) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_basetype* funciton_type = libab_get_basetype_function(state->ab);
|
||||
|
||||
if((*type = malloc(sizeof(**type)))) {
|
||||
(*type)->variant = LIBABACUS_TYPE_F_PARENT | LIBABACUS_TYPE_F_RESOLVED;
|
||||
(*type)->data_u.base = funciton_type;
|
||||
result = libab_ref_vec_init(&(*type)->children);
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = vec_foreach(&tree->children, NULL, _interpreter_compare_function_param,
|
||||
_interpreter_foreach_insert_function_param, &(*type)->children);
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_vec_insert(&(*type)->children, &tree->type);
|
||||
}
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_vec_free(&(*type)->children);
|
||||
}
|
||||
}
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
free(*type);
|
||||
*type = NULL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_wrap_function_type(
|
||||
struct interpreter_state* state, libab_tree* tree, libab_ref* into) {
|
||||
libab_parsetype* type;
|
||||
libab_result result = _interpreter_create_function_type(state, tree, &type);
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_new(into, type, free_parsetype);
|
||||
}
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_null(into);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_create_function_value(
|
||||
struct interpreter_state* state, libab_tree* tree,
|
||||
libab_ref* scope, libab_ref* into) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref function, type;
|
||||
|
||||
libab_ref_null(&type);
|
||||
libab_ref_null(into);
|
||||
result = libab_create_function_tree(&function, free_function, tree, scope);
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(&type);
|
||||
result = _interpreter_wrap_function_type(state, tree, &type);
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
result = libab_create_value_ref(into, &function, &type);
|
||||
}
|
||||
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
libab_ref_null(into);
|
||||
}
|
||||
libab_ref_free(&function);
|
||||
libab_ref_free(&type);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
||||
libab_ref* into, libab_ref* scope,
|
||||
int force_scope) {
|
||||
libab_interpreter_scope_mode mode) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
libab_ref new_scope;
|
||||
int needs_scope = libab_tree_has_scope(tree->variant) || force_scope;
|
||||
int needs_scope = (mode == SCOPE_FORCE) ||
|
||||
(mode == SCOPE_NORMAL && libab_tree_has_scope(tree->variant));
|
||||
|
||||
if (needs_scope) {
|
||||
result = libab_create_table(&new_scope, scope);
|
||||
|
@ -892,7 +1118,7 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||
while (result == LIBAB_SUCCESS && index < tree->children.size) {
|
||||
libab_ref_free(into);
|
||||
result = _interpreter_run(state, vec_index(&tree->children, index),
|
||||
into, scope, 0);
|
||||
into, scope, SCOPE_NORMAL);
|
||||
index++;
|
||||
}
|
||||
} else if (tree->variant == TREE_NUM) {
|
||||
|
@ -902,7 +1128,7 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||
} else if (tree->variant == TREE_ID) {
|
||||
result = _interpreter_require_value(scope, tree->string_value, into);
|
||||
} else if (tree->variant == TREE_CALL) {
|
||||
result = _interpreter_run_function_node(state, tree, into, scope);
|
||||
result = _interpreter_run_call_node(state, tree, into, scope);
|
||||
} else if (tree->variant == TREE_OP) {
|
||||
libab_operator* to_call = libab_table_search_operator(
|
||||
libab_ref_get(scope), tree->string_value, OPERATOR_INFIX);
|
||||
|
@ -919,6 +1145,22 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||
libab_ref_get(scope), tree->string_value, OPERATOR_POSTFIX);
|
||||
result = _interpreter_call_operator(state, to_call, into, scope,
|
||||
vec_index(&tree->children, 0));
|
||||
} else if (tree->variant == TREE_FUN) {
|
||||
libab_ref function;
|
||||
result =
|
||||
_interpreter_create_function_value(state, tree, scope, &function);
|
||||
|
||||
if(result == LIBAB_SUCCESS) {
|
||||
result = libab_put_table_value(libab_ref_get(scope),
|
||||
tree->string_value, &function);
|
||||
if(result != LIBAB_SUCCESS) {
|
||||
libab_ref_free(&function);
|
||||
libab_ref_null(&function);
|
||||
}
|
||||
}
|
||||
|
||||
libab_ref_copy(&function, into);
|
||||
libab_ref_free(&function);
|
||||
} else {
|
||||
libab_get_unit_value(state->ab, into);
|
||||
}
|
||||
|
@ -931,12 +1173,13 @@ libab_result _interpreter_run(struct interpreter_state* state, libab_tree* tree,
|
|||
}
|
||||
|
||||
libab_result libab_interpreter_run(libab_interpreter* intr, libab_tree* tree,
|
||||
libab_interpreter_scope_mode mode,
|
||||
libab_ref* into) {
|
||||
struct interpreter_state state;
|
||||
libab_result result;
|
||||
|
||||
_interpreter_init(&state, intr);
|
||||
result = _interpreter_run(&state, tree, into, &state.ab->table, 1);
|
||||
result = _interpreter_run(&state, tree, into, &state.ab->table, mode);
|
||||
_interpreter_free(&state);
|
||||
|
||||
return result;
|
||||
|
|
|
@ -5,29 +5,16 @@
|
|||
#include "util.h"
|
||||
#include "value.h"
|
||||
#include <stdlib.h>
|
||||
#include "free_functions.h"
|
||||
|
||||
void _free_function_list(void* function_list) {
|
||||
libab_function_list_free(function_list);
|
||||
free(function_list);
|
||||
}
|
||||
|
||||
static libab_basetype _basetype_function_list = {_free_function_list, NULL, 0};
|
||||
|
||||
void _free_function(void* function) {
|
||||
libab_function_free(function);
|
||||
free(function);
|
||||
}
|
||||
static libab_basetype _basetype_function_list = {free_function_list, NULL, 0};
|
||||
|
||||
static libab_basetype_param _basetype_function_params[] = {{BT_LIST, NULL}};
|
||||
|
||||
static libab_basetype _basetype_function = {_free_function,
|
||||
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 };
|
||||
static libab_basetype _basetype_unit = { free_unit, NULL, 0 };
|
||||
|
||||
libab_result _prepare_types(libab* ab, void (*free_function)(void*));
|
||||
|
||||
|
@ -167,7 +154,7 @@ libab_result _create_value_function_internal(libab_ref* into, libab_ref* type,
|
|||
libab_ref* scope) {
|
||||
libab_ref function_ref;
|
||||
libab_result result =
|
||||
libab_create_function_internal(&function_ref, _free_function, func, scope);
|
||||
libab_create_function_internal(&function_ref, free_function, func, scope);
|
||||
libab_ref_null(into);
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(into);
|
||||
|
@ -407,7 +394,7 @@ libab_result libab_run(libab* ab, const char* string, libab_ref* value) {
|
|||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
libab_ref_free(value);
|
||||
result = libab_interpreter_run(&ab->intr, root, value);
|
||||
result = libab_interpreter_run(&ab->intr, root, SCOPE_NORMAL, value);
|
||||
libab_tree_free_recursive(root);
|
||||
}
|
||||
|
||||
|
|
|
@ -134,6 +134,65 @@ void libab_ref_trie_get(const libab_ref_trie* trie, const char* key,
|
|||
libab_ref_null(into);
|
||||
}
|
||||
|
||||
libab_result _ref_trie_foreach(libab_ref_trie_node* node,
|
||||
char** str, size_t* str_size, size_t depth,
|
||||
libab_result (*func)(const libab_ref*, const char*, va_list),
|
||||
va_list args) {
|
||||
va_list args_copy;
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
if(node == NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if(depth + 1 >= *str_size) {
|
||||
char* new_str = realloc(*str, (*str_size) *= 2);
|
||||
if(new_str) {
|
||||
*str = new_str;
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
(*str)[depth] = node->key;
|
||||
(*str)[depth + 1] = '\0';
|
||||
|
||||
if(libab_ref_get(&node->ref)) {
|
||||
va_copy(args_copy, args);
|
||||
result = func(&node->ref, (*str), args_copy);
|
||||
va_end(args_copy);
|
||||
}
|
||||
|
||||
if(result == LIBAB_SUCCESS)
|
||||
result = _ref_trie_foreach(node->child, str, str_size, depth + 1, func, args);
|
||||
if(result == LIBAB_SUCCESS)
|
||||
result = _ref_trie_foreach(node->next, str, str_size, depth, func, args);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
libab_result libab_ref_trie_foreach(const libab_ref_trie* trie,
|
||||
libab_result (*func)(const libab_ref*, const char*, va_list),
|
||||
...) {
|
||||
va_list args;
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
char* str;
|
||||
size_t string_size = 4;
|
||||
|
||||
if((str = malloc(sizeof(*str) * 4))) {
|
||||
va_start(args, func);
|
||||
result = _ref_trie_foreach(trie->head, &str, &string_size, 0, func, args);
|
||||
va_end(args);
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
}
|
||||
|
||||
free(str);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void libab_ref_trie_free(libab_ref_trie* trie) {
|
||||
_libab_ref_trie_free(trie->head);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ int libab_tree_has_string(libab_tree_variant variant) {
|
|||
int libab_tree_has_scope(libab_tree_variant variant) {
|
||||
return variant == TREE_BASE || variant == TREE_BLOCK ||
|
||||
variant == TREE_IF || variant == TREE_WHILE ||
|
||||
variant == TREE_DOWHILE || variant == TREE_FUN;
|
||||
variant == TREE_DOWHILE;
|
||||
}
|
||||
|
||||
int libab_tree_has_type(libab_tree_variant variant) {
|
||||
|
|
11
src/types.c
11
src/types.c
|
@ -1,11 +0,0 @@
|
|||
#include "types.h"
|
||||
|
||||
libab_result libab_array_init(libab_array* array) {
|
||||
return libab_ref_vec_init(&array->elems);
|
||||
}
|
||||
|
||||
libab_result libab_array_insert(libab_array* array, libab_ref* value) {
|
||||
return libab_ref_vec_insert(&array->elems, value);
|
||||
}
|
||||
|
||||
void libab_array_free(libab_array* array) { libab_ref_vec_free(&array->elems); }
|
21
src/util.c
21
src/util.c
|
@ -2,6 +2,7 @@
|
|||
#include "value.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include "free_functions.h"
|
||||
|
||||
libab_result libab_convert_lex_result(liblex_result to_convert) {
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
|
@ -130,21 +131,16 @@ libab_result libab_instantiate_basetype(libab_basetype* to_instantiate,
|
|||
return result;
|
||||
}
|
||||
|
||||
void _free_table(void* data) {
|
||||
libab_table_free(data);
|
||||
free(data);
|
||||
}
|
||||
|
||||
libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
|
||||
libab_table* table;
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
if ((table = malloc(sizeof(*table)))) {
|
||||
libab_table_init(table);
|
||||
libab_table_set_parent(table, parent);
|
||||
result = libab_ref_new(into, table, _free_table);
|
||||
result = libab_ref_new(into, table, free_table);
|
||||
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
_free_table(table);
|
||||
free_table(table);
|
||||
}
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
|
@ -156,21 +152,16 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent) {
|
|||
return result;
|
||||
}
|
||||
|
||||
void _free_value(void* value) {
|
||||
libab_value_free(value);
|
||||
free(value);
|
||||
}
|
||||
|
||||
libab_result libab_create_value_ref(libab_ref* into, libab_ref* data,
|
||||
libab_ref* type) {
|
||||
libab_value* value;
|
||||
libab_result result = LIBAB_SUCCESS;
|
||||
if ((value = malloc(sizeof(*value)))) {
|
||||
libab_value_init_ref(value, data, type);
|
||||
result = libab_ref_new(into, value, _free_value);
|
||||
result = libab_ref_new(into, value, free_value);
|
||||
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
_free_value(value);
|
||||
free_value(value);
|
||||
}
|
||||
} else {
|
||||
result = LIBAB_MALLOC;
|
||||
|
@ -194,7 +185,7 @@ libab_result libab_create_value_raw(libab_ref* into, void* data,
|
|||
}
|
||||
|
||||
if (result == LIBAB_SUCCESS) {
|
||||
result = libab_ref_new(into, value, _free_value);
|
||||
result = libab_ref_new(into, value, free_value);
|
||||
if (result != LIBAB_SUCCESS) {
|
||||
libab_value_free(value);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue