Add error call stack if call stack exceeds maximum capacity
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Dominic Grimm 2023-08-05 20:19:52 +02:00
parent 411665061d
commit 7490439223
9 changed files with 138 additions and 102 deletions

View file

@ -56,9 +56,9 @@ struct hf__result words__word(struct hf__interpreter *const interpreter,
}
for (size_t i = (*word)->body_len - 1; i != 0 - 1; i--) {
hf__parser__node_array_push(
&interpreter->call_stack, &interpreter->call_stack_len,
&interpreter->call_stack_size, (*word)->body[i]);
hf__parser__node_array_push(&interpreter->call_stack,
&interpreter->call_stack_len,
&interpreter->call_stack_cap, (*word)->body[i]);
interpreter->call_stack[interpreter->call_stack_len - 1].is_owner = false;
}
@ -343,12 +343,12 @@ const hf__interpreter__word_func_t
};
void hf__interpreter__stack_push(long **arr, size_t *const len,
size_t *const size, long item) {
if (*len > *size) {
size_t *const cap, long item) {
if (*len > *cap) {
return;
} else if (*len == *size) {
*size += 1 + (*size / 2);
*arr = realloc(*arr, sizeof(long) * (*size));
} else if (*len == *cap) {
*cap += 1 + (*cap / 2);
*arr = realloc(*arr, sizeof(long) * (*cap));
}
(*arr)[*len] = item;
@ -363,15 +363,27 @@ hf__interpreter__run(struct hf__interpreter *const interpreter) {
const struct hf__node *const top =
interpreter->call_stack + --interpreter->call_stack_len;
SET_8_VALUE_COLOUR(TXT_RED);
printf("--- type = %u ---\n", top->type);
if (top->type == HF__NODE_TYPE__WORD) {
printf("--- word name = \"%s\"\n", top->value.word.value);
}
SET_8_VALUE_COLOUR(TXT_DEFAULT);
const hf__interpreter__word_func_t func =
HF__INTERPRETER__WORD_FUNCTION[top->type];
return func ? func(interpreter, top) : HF__OK;
if (func) {
struct hf__result res = func(interpreter, top);
if (res.ok && interpreter->call_stack_cap_max != 0 &&
interpreter->call_stack_cap >= interpreter->call_stack_cap_max) {
const size_t required_size =
snprintf(NULL, 0, "%lu >= %lu", interpreter->call_stack_cap,
interpreter->call_stack_cap_max);
char *msg = malloc(sizeof(char) * (required_size + 1));
snprintf(msg, required_size + 1, "%lu >= %lu",
interpreter->call_stack_cap, interpreter->call_stack_cap_max);
return HF__ERR_CUSTOM(HF__ERROR__INTERPRETER__CALL_STACK_TOO_BIG, msg,
true);
} else {
return res;
}
} else {
return HF__OK;
}
}