Refactor stack to now use ints instead of long ints
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
7490439223
commit
03a9b46e33
7 changed files with 62 additions and 24 deletions
|
@ -27,7 +27,7 @@ struct hf__interpreter {
|
||||||
|
|
||||||
struct hf__hashmap words; // struct hf__interpreter__word *
|
struct hf__hashmap words; // struct hf__interpreter__word *
|
||||||
|
|
||||||
long *stack;
|
int *stack;
|
||||||
size_t stack_len;
|
size_t stack_len;
|
||||||
size_t stack_size;
|
size_t stack_size;
|
||||||
|
|
||||||
|
@ -43,8 +43,8 @@ typedef struct hf__result (*hf__interpreter__word_func_t)(
|
||||||
extern const hf__interpreter__word_func_t
|
extern const hf__interpreter__word_func_t
|
||||||
HF__INTERPRETER__WORD_FUNCTION[__HF__NODE_TYPE__N];
|
HF__INTERPRETER__WORD_FUNCTION[__HF__NODE_TYPE__N];
|
||||||
|
|
||||||
extern void hf__interpreter__stack_push(long **arr, size_t *const len,
|
extern void hf__interpreter__stack_push(int **arr, size_t *const len,
|
||||||
size_t *const cap, long item);
|
size_t *const cap, int item);
|
||||||
|
|
||||||
extern struct hf__result
|
extern struct hf__result
|
||||||
hf__interpreter__run(struct hf__interpreter *const interpreter);
|
hf__interpreter__run(struct hf__interpreter *const interpreter);
|
||||||
|
|
|
@ -9,6 +9,8 @@ enum hf__token_type {
|
||||||
HF__TOKEN_TYPE__CHAR,
|
HF__TOKEN_TYPE__CHAR,
|
||||||
HF__TOKEN_TYPE__WORD,
|
HF__TOKEN_TYPE__WORD,
|
||||||
|
|
||||||
|
HF__TOKEN_TYPE__PERIOD_STRING,
|
||||||
|
|
||||||
HF__TOKEN_TYPE__COLON,
|
HF__TOKEN_TYPE__COLON,
|
||||||
HF__TOKEN_TYPE__SEMICOLON,
|
HF__TOKEN_TYPE__SEMICOLON,
|
||||||
|
|
||||||
|
|
|
@ -50,7 +50,7 @@ struct hf__node_value__word_def {
|
||||||
};
|
};
|
||||||
|
|
||||||
union hf__node_value {
|
union hf__node_value {
|
||||||
long number;
|
int number;
|
||||||
char ch;
|
char ch;
|
||||||
struct hf__node_value__word word;
|
struct hf__node_value__word word;
|
||||||
struct hf__node_value__word_def *word_def;
|
struct hf__node_value__word_def *word_def;
|
||||||
|
|
|
@ -115,7 +115,7 @@ struct hf__result words__dup(struct hf__interpreter *const interpreter,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const long top = interpreter->stack[interpreter->stack_len - 1];
|
const int top = interpreter->stack[interpreter->stack_len - 1];
|
||||||
hf__interpreter__stack_push(&interpreter->stack, &interpreter->stack_len,
|
hf__interpreter__stack_push(&interpreter->stack, &interpreter->stack_len,
|
||||||
&interpreter->stack_size, top);
|
&interpreter->stack_size, top);
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ struct hf__result words__swap(struct hf__interpreter *const interpreter,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const long tmp = interpreter->stack[interpreter->stack_len - 1];
|
const int tmp = interpreter->stack[interpreter->stack_len - 1];
|
||||||
interpreter->stack[interpreter->stack_len - 1] =
|
interpreter->stack[interpreter->stack_len - 1] =
|
||||||
interpreter->stack[interpreter->stack_len - 2];
|
interpreter->stack[interpreter->stack_len - 2];
|
||||||
interpreter->stack[interpreter->stack_len - 2] = tmp;
|
interpreter->stack[interpreter->stack_len - 2] = tmp;
|
||||||
|
@ -221,7 +221,7 @@ struct hf__result words__dot(struct hf__interpreter *const interpreter,
|
||||||
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%li", interpreter->stack[--interpreter->stack_len]);
|
printf("%i", interpreter->stack[--interpreter->stack_len]);
|
||||||
|
|
||||||
return HF__OK;
|
return HF__OK;
|
||||||
}
|
}
|
||||||
|
@ -250,8 +250,8 @@ struct hf__result words__spaces(struct hf__interpreter *const interpreter,
|
||||||
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned long max = interpreter->stack[--interpreter->stack_len];
|
const int max = interpreter->stack[--interpreter->stack_len];
|
||||||
for (unsigned long i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
putchar(' ');
|
putchar(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,8 +271,8 @@ struct hf__result words__crs(struct hf__interpreter *const interpreter,
|
||||||
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned long max = interpreter->stack[--interpreter->stack_len];
|
const int max = interpreter->stack[--interpreter->stack_len];
|
||||||
for (unsigned long i = 0; i < max; i++) {
|
for (int i = 0; i < max; i++) {
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,7 +284,7 @@ struct hf__result words__debug(struct hf__interpreter *const interpreter,
|
||||||
SET_8_VALUE_COLOUR(TXT_CYAN);
|
SET_8_VALUE_COLOUR(TXT_CYAN);
|
||||||
puts("\n===\nDEBUG:");
|
puts("\n===\nDEBUG:");
|
||||||
for (size_t i = interpreter->stack_len - 1; i != 0 - 1; i--) {
|
for (size_t i = interpreter->stack_len - 1; i != 0 - 1; i--) {
|
||||||
printf("%lu : %li\n", i, interpreter->stack[i]);
|
printf("%lu : %i\n", i, interpreter->stack[i]);
|
||||||
}
|
}
|
||||||
puts("===");
|
puts("===");
|
||||||
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
||||||
|
@ -342,13 +342,13 @@ const hf__interpreter__word_func_t
|
||||||
[HF__NODE_TYPE__EXIT] = words__exit,
|
[HF__NODE_TYPE__EXIT] = words__exit,
|
||||||
};
|
};
|
||||||
|
|
||||||
void hf__interpreter__stack_push(long **arr, size_t *const len,
|
void hf__interpreter__stack_push(int **arr, size_t *const len,
|
||||||
size_t *const cap, long item) {
|
size_t *const cap, int item) {
|
||||||
if (*len > *cap) {
|
if (*len > *cap) {
|
||||||
return;
|
return;
|
||||||
} else if (*len == *cap) {
|
} else if (*len == *cap) {
|
||||||
*cap += 1 + (*cap / 2);
|
*cap += 1 + (*cap / 2);
|
||||||
*arr = realloc(*arr, sizeof(long) * (*cap));
|
*arr = realloc(*arr, sizeof(int) * (*cap));
|
||||||
}
|
}
|
||||||
|
|
||||||
(*arr)[*len] = item;
|
(*arr)[*len] = item;
|
||||||
|
|
|
@ -85,15 +85,13 @@ void hf__lex(const char *const src, const size_t src_len,
|
||||||
goto TOKEN_IS_WORD;
|
goto TOKEN_IS_WORD;
|
||||||
}
|
}
|
||||||
} else if (str_len == 1 && src[start] == '\\' &&
|
} else if (str_len == 1 && src[start] == '\\' &&
|
||||||
hf__is_space_like(src[i])) {
|
(hf__is_space_like(src[i]) || src[i] == '\0')) {
|
||||||
token.type = HF__TOKEN_TYPE__BACKSLASH_COMMENT;
|
token.type = HF__TOKEN_TYPE__BACKSLASH_COMMENT;
|
||||||
|
|
||||||
start = ++i;
|
token.location.start = ++i;
|
||||||
while (src[i] != '\n' && i < src_len) {
|
while (src[i] != '\n' && i < src_len) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
token.location.start = start;
|
|
||||||
token.location.end = i - 1;
|
token.location.end = i - 1;
|
||||||
} else if (str_len == 2 && strncmp(src + start, "--", 2) == 0 &&
|
} else if (str_len == 2 && strncmp(src + start, "--", 2) == 0 &&
|
||||||
(hf__is_space_like(src[i]) || src[i] == '\0')) {
|
(hf__is_space_like(src[i]) || src[i] == '\0')) {
|
||||||
|
@ -106,6 +104,36 @@ void hf__lex(const char *const src, const size_t src_len,
|
||||||
|
|
||||||
token.location.start = start;
|
token.location.start = start;
|
||||||
token.location.end = i - 1;
|
token.location.end = i - 1;
|
||||||
|
} else if (str_len == 2 && src[start + 1] == '"' &&
|
||||||
|
hf__is_space_like(src[i])) {
|
||||||
|
switch (src[start]) {
|
||||||
|
case '.':
|
||||||
|
token.type = HF__TOKEN_TYPE__PERIOD_STRING;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
goto TOKEN_IS_WORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
bool got_end = false;
|
||||||
|
while (i < src_len) {
|
||||||
|
if (src[i] == '"' && src[i - 1] != '\\') {
|
||||||
|
got_end = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (got_end) {
|
||||||
|
token.type = HF__TOKEN_TYPE__PERIOD_STRING;
|
||||||
|
token.location.start = start + 3;
|
||||||
|
token.location.end = i - 1;
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
i = start;
|
||||||
|
goto TOKEN_IS_WORD;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
TOKEN_IS_WORD:
|
TOKEN_IS_WORD:
|
||||||
token.type = HF__TOKEN_TYPE__WORD;
|
token.type = HF__TOKEN_TYPE__WORD;
|
||||||
|
|
|
@ -63,7 +63,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
case HF__TOKEN_TYPE__NUMBER: {
|
case HF__TOKEN_TYPE__NUMBER: {
|
||||||
size_t j = tokens[i].location.start;
|
size_t j = tokens[i].location.start;
|
||||||
bool negative = false;
|
bool negative = false;
|
||||||
long number = 0;
|
int number = 0;
|
||||||
|
|
||||||
if (src[tokens[i].location.start] == '-') {
|
if (src[tokens[i].location.start] == '-') {
|
||||||
j++;
|
j++;
|
||||||
|
@ -231,6 +231,14 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case HF__TOKEN_TYPE__PERIOD_STRING:
|
||||||
|
for (size_t j = tokens[i].location.start;
|
||||||
|
j < (tokens[i].location.end + 1); j++) {
|
||||||
|
putchar(src[j]);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
break;
|
||||||
|
|
||||||
case HF__TOKEN_TYPE__COLON: {
|
case HF__TOKEN_TYPE__COLON: {
|
||||||
const size_t start = i++;
|
const size_t start = i++;
|
||||||
bool got_end = false;
|
bool got_end = false;
|
||||||
|
|
|
@ -117,7 +117,7 @@ struct hf__result shell(const struct arguments *const arguments) {
|
||||||
.cap = HF__INTERPRETER__WORDS_CAP,
|
.cap = HF__INTERPRETER__WORDS_CAP,
|
||||||
},
|
},
|
||||||
|
|
||||||
.stack = malloc(sizeof(long) * 10),
|
.stack = malloc(sizeof(int) * 10),
|
||||||
.stack_len = 0,
|
.stack_len = 0,
|
||||||
.stack_size = 10,
|
.stack_size = 10,
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ struct hf__result shell(const struct arguments *const arguments) {
|
||||||
printf("stack:");
|
printf("stack:");
|
||||||
SET_8_VALUE_COLOUR(TXT_YELLOW);
|
SET_8_VALUE_COLOUR(TXT_YELLOW);
|
||||||
for (size_t i = 0; i < interpreter.stack_len; i++) {
|
for (size_t i = 0; i < interpreter.stack_len; i++) {
|
||||||
printf(" %li", interpreter.stack[i]);
|
printf(" %i", interpreter.stack[i]);
|
||||||
}
|
}
|
||||||
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
@ -267,7 +267,7 @@ int main(int argc, char *argv[]) {
|
||||||
.cap = HF__INTERPRETER__WORDS_CAP,
|
.cap = HF__INTERPRETER__WORDS_CAP,
|
||||||
},
|
},
|
||||||
|
|
||||||
.stack = malloc(sizeof(long) * 10),
|
.stack = malloc(sizeof(int) * 10),
|
||||||
.stack_len = 0,
|
.stack_len = 0,
|
||||||
.stack_size = 10,
|
.stack_size = 10,
|
||||||
|
|
||||||
|
@ -314,7 +314,7 @@ int main(int argc, char *argv[]) {
|
||||||
printf("stack:");
|
printf("stack:");
|
||||||
SET_8_VALUE_COLOUR(TXT_YELLOW);
|
SET_8_VALUE_COLOUR(TXT_YELLOW);
|
||||||
for (size_t i = 0; i < interpreter.stack_len; i++) {
|
for (size_t i = 0; i < interpreter.stack_len; i++) {
|
||||||
printf(" %li", interpreter.stack[i]);
|
printf(" %i", interpreter.stack[i]);
|
||||||
}
|
}
|
||||||
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
SET_8_VALUE_COLOUR(TXT_DEFAULT);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
Loading…
Reference in a new issue