From 03a9b46e33001d5275e698a79907fc2de4053cac Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sun, 6 Aug 2023 11:20:14 +0200 Subject: [PATCH] Refactor stack to now use ints instead of long ints --- include/hydroforth/interpreter.h | 6 +++--- include/hydroforth/lexer.h | 2 ++ include/hydroforth/parser.h | 2 +- src/hydroforth/interpreter.c | 22 +++++++++---------- src/hydroforth/lexer.c | 36 ++++++++++++++++++++++++++++---- src/hydroforth/parser.c | 10 ++++++++- src/main.c | 8 +++---- 7 files changed, 62 insertions(+), 24 deletions(-) diff --git a/include/hydroforth/interpreter.h b/include/hydroforth/interpreter.h index 6cb37e8..e96af9a 100644 --- a/include/hydroforth/interpreter.h +++ b/include/hydroforth/interpreter.h @@ -27,7 +27,7 @@ struct hf__interpreter { struct hf__hashmap words; // struct hf__interpreter__word * - long *stack; + int *stack; size_t stack_len; size_t stack_size; @@ -43,8 +43,8 @@ typedef struct hf__result (*hf__interpreter__word_func_t)( extern const hf__interpreter__word_func_t HF__INTERPRETER__WORD_FUNCTION[__HF__NODE_TYPE__N]; -extern void hf__interpreter__stack_push(long **arr, size_t *const len, - size_t *const cap, long item); +extern void hf__interpreter__stack_push(int **arr, size_t *const len, + size_t *const cap, int item); extern struct hf__result hf__interpreter__run(struct hf__interpreter *const interpreter); diff --git a/include/hydroforth/lexer.h b/include/hydroforth/lexer.h index ee1293f..2164fa7 100644 --- a/include/hydroforth/lexer.h +++ b/include/hydroforth/lexer.h @@ -9,6 +9,8 @@ enum hf__token_type { HF__TOKEN_TYPE__CHAR, HF__TOKEN_TYPE__WORD, + HF__TOKEN_TYPE__PERIOD_STRING, + HF__TOKEN_TYPE__COLON, HF__TOKEN_TYPE__SEMICOLON, diff --git a/include/hydroforth/parser.h b/include/hydroforth/parser.h index 7aa2b4c..62b87a2 100644 --- a/include/hydroforth/parser.h +++ b/include/hydroforth/parser.h @@ -50,7 +50,7 @@ struct hf__node_value__word_def { }; union hf__node_value { - long number; + int number; char ch; struct hf__node_value__word word; struct hf__node_value__word_def *word_def; diff --git a/src/hydroforth/interpreter.c b/src/hydroforth/interpreter.c index 1f4d876..a3a582c 100644 --- a/src/hydroforth/interpreter.c +++ b/src/hydroforth/interpreter.c @@ -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, &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 - 2]; 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); } - printf("%li", interpreter->stack[--interpreter->stack_len]); + printf("%i", interpreter->stack[--interpreter->stack_len]); 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); } - const unsigned long max = interpreter->stack[--interpreter->stack_len]; - for (unsigned long i = 0; i < max; i++) { + const int max = interpreter->stack[--interpreter->stack_len]; + for (int i = 0; i < max; i++) { putchar(' '); } @@ -271,8 +271,8 @@ struct hf__result words__crs(struct hf__interpreter *const interpreter, return HF__ERR(HF__ERROR__INTERPRETER__STACK_UNDERFLOW); } - const unsigned long max = interpreter->stack[--interpreter->stack_len]; - for (unsigned long i = 0; i < max; i++) { + const int max = interpreter->stack[--interpreter->stack_len]; + for (int i = 0; i < max; i++) { putchar('\n'); } @@ -284,7 +284,7 @@ struct hf__result words__debug(struct hf__interpreter *const interpreter, SET_8_VALUE_COLOUR(TXT_CYAN); puts("\n===\nDEBUG:"); 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("==="); SET_8_VALUE_COLOUR(TXT_DEFAULT); @@ -342,13 +342,13 @@ const hf__interpreter__word_func_t [HF__NODE_TYPE__EXIT] = words__exit, }; -void hf__interpreter__stack_push(long **arr, size_t *const len, - size_t *const cap, long item) { +void hf__interpreter__stack_push(int **arr, size_t *const len, + size_t *const cap, int item) { if (*len > *cap) { return; } else if (*len == *cap) { *cap += 1 + (*cap / 2); - *arr = realloc(*arr, sizeof(long) * (*cap)); + *arr = realloc(*arr, sizeof(int) * (*cap)); } (*arr)[*len] = item; diff --git a/src/hydroforth/lexer.c b/src/hydroforth/lexer.c index c939722..571795d 100644 --- a/src/hydroforth/lexer.c +++ b/src/hydroforth/lexer.c @@ -85,15 +85,13 @@ void hf__lex(const char *const src, const size_t src_len, goto TOKEN_IS_WORD; } } 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; - start = ++i; + token.location.start = ++i; while (src[i] != '\n' && i < src_len) { i++; } - - token.location.start = start; token.location.end = i - 1; } else if (str_len == 2 && strncmp(src + start, "--", 2) == 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.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 { TOKEN_IS_WORD: token.type = HF__TOKEN_TYPE__WORD; diff --git a/src/hydroforth/parser.c b/src/hydroforth/parser.c index 6a6498b..ada70fe 100644 --- a/src/hydroforth/parser.c +++ b/src/hydroforth/parser.c @@ -63,7 +63,7 @@ struct hf__result hf__parse(struct hf__parser *const parser, case HF__TOKEN_TYPE__NUMBER: { size_t j = tokens[i].location.start; bool negative = false; - long number = 0; + int number = 0; if (src[tokens[i].location.start] == '-') { j++; @@ -231,6 +231,14 @@ struct hf__result hf__parse(struct hf__parser *const parser, 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: { const size_t start = i++; bool got_end = false; diff --git a/src/main.c b/src/main.c index d6c2ca1..f44cdad 100644 --- a/src/main.c +++ b/src/main.c @@ -117,7 +117,7 @@ struct hf__result shell(const struct arguments *const arguments) { .cap = HF__INTERPRETER__WORDS_CAP, }, - .stack = malloc(sizeof(long) * 10), + .stack = malloc(sizeof(int) * 10), .stack_len = 0, .stack_size = 10, @@ -177,7 +177,7 @@ struct hf__result shell(const struct arguments *const arguments) { printf("stack:"); SET_8_VALUE_COLOUR(TXT_YELLOW); 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); putchar('\n'); @@ -267,7 +267,7 @@ int main(int argc, char *argv[]) { .cap = HF__INTERPRETER__WORDS_CAP, }, - .stack = malloc(sizeof(long) * 10), + .stack = malloc(sizeof(int) * 10), .stack_len = 0, .stack_size = 10, @@ -314,7 +314,7 @@ int main(int argc, char *argv[]) { printf("stack:"); SET_8_VALUE_COLOUR(TXT_YELLOW); 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); putchar('\n');