libabacus/src/interactive.c

117 lines
3.5 KiB
C
Raw Normal View History

2018-05-23 15:41:17 -07:00
#include "libabacus.h"
2018-05-26 20:43:36 -07:00
#include "util.h"
2018-05-26 21:55:30 -07:00
#include "value.h"
#include <stdio.h>
2018-05-23 15:41:17 -07:00
2018-05-26 21:55:30 -07:00
#define TRY(expression) \
if (result == LIBAB_SUCCESS) \
result = expression;
2018-05-23 15:41:17 -07:00
#define INTERACTIONS 5
void* impl_parse(const char* string) {
double* data = malloc(sizeof(*data));
2018-05-26 21:55:30 -07:00
if (data) {
2018-05-23 15:41:17 -07:00
*data = strtod(string, NULL);
}
return data;
}
2018-05-26 21:55:30 -07:00
void impl_free(void* data) { free(data); }
2018-05-23 15:41:17 -07:00
libab_result function_atan(libab* ab, libab_ref_vec* params, libab_ref* into) {
2018-05-23 15:41:17 -07:00
printf("atan called\n");
libab_ref_null(into);
return LIBAB_SUCCESS;
}
libab_result function_atan2(libab* ab, libab_ref_vec* params, libab_ref* into) {
2018-05-23 15:41:17 -07:00
printf("atan2 called\n");
libab_ref_null(into);
return LIBAB_SUCCESS;
}
libab_result function_operator(libab* ab, libab_ref_vec* params, libab_ref* into) {
2018-05-26 20:43:36 -07:00
libab_result result = LIBAB_SUCCESS;
libab_ref left_ref;
libab_value* left_value;
double right;
double left;
double* return_value;
2018-05-23 15:41:17 -07:00
printf("operator called\n");
2018-05-26 20:43:36 -07:00
libab_ref_vec_index(params, 0, &left_ref);
left_value = libab_ref_get(&left_ref);
2018-05-26 21:55:30 -07:00
left = *((double*)libab_unwrap_param(params, 0));
right = *((double*)libab_unwrap_param(params, 1));
2018-05-26 20:43:36 -07:00
return_value = malloc(sizeof(*return_value));
2018-05-26 21:55:30 -07:00
if (return_value == NULL) {
2018-05-26 20:43:36 -07:00
result = LIBAB_MALLOC;
libab_ref_null(into);
} else {
*return_value = left + right;
2018-05-26 21:55:30 -07:00
result = libab_create_value_raw(into, return_value, &left_value->type);
2018-05-26 20:43:36 -07:00
}
libab_ref_free(&left_ref);
return result;
2018-05-23 15:41:17 -07:00
}
libab_result register_functions(libab* ab) {
libab_result result = LIBAB_SUCCESS;
libab_ref trig_type;
libab_ref atan2_type;
libab_ref difficult_type;
result = libab_create_type(ab, &trig_type, "(num)->num");
TRY(libab_create_type(ab, &atan2_type, "(num, num)->num"));
TRY(libab_create_type(ab, &difficult_type, "((num)->num)->num"));
TRY(libab_register_function(ab, "atan", &trig_type, function_atan));
TRY(libab_register_function(ab, "atan2", &atan2_type, function_atan2));
2018-05-26 21:55:30 -07:00
TRY(libab_register_operator_infix(ab, "+", 0, -1, &atan2_type,
function_operator));
TRY(libab_register_operator_infix(ab, "-", 0, -1, &atan2_type,
function_operator));
TRY(libab_register_operator_infix(ab, "*", 1, -1, &atan2_type,
function_operator));
TRY(libab_register_operator_infix(ab, "/", 1, -1, &atan2_type,
function_operator));
2018-05-23 15:41:17 -07:00
libab_ref_free(&trig_type);
libab_ref_free(&atan2_type);
libab_ref_free(&difficult_type);
return result;
}
int main() {
char input_buffer[2048];
int interaction_count = INTERACTIONS;
libab_ref eval_into;
libab_result result;
libab_result eval_result;
libab ab;
2018-05-26 21:55:30 -07:00
if (libab_init(&ab, impl_parse, impl_free) != LIBAB_SUCCESS) {
2018-05-23 15:41:17 -07:00
fprintf(stderr, "Failed to initialize libab.\n");
exit(1);
}
result = register_functions(&ab);
2018-05-26 21:55:30 -07:00
while (interaction_count-- && result == LIBAB_SUCCESS) {
2018-05-23 15:41:17 -07:00
printf("(%d) > ", INTERACTIONS - interaction_count);
fgets(input_buffer, 2048, stdin);
eval_result = libab_run(&ab, input_buffer, &eval_into);
2018-05-26 21:55:30 -07:00
if (eval_result != LIBAB_SUCCESS) {
2018-05-23 15:41:17 -07:00
printf("Invalid input.\n");
}
libab_ref_free(&eval_into);
}
libab_free(&ab);
}