From 966905fba74407dfc4086674ecf199f20e2683fb Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Fri, 5 Jan 2024 16:10:56 -0800 Subject: fix memory leaks; no error until exit --- LICENSE | 7 ++ Makefile | 2 +- examples/repl.stem | 2 + main.c | 68 +++++------ parser.c | 335 +++++++++++++++++++++++++++++++++++++++++------------ parser.h | 2 +- uforth | Bin 26680 -> 0 bytes 7 files changed, 304 insertions(+), 112 deletions(-) create mode 100644 LICENSE create mode 100644 examples/repl.stem delete mode 100755 uforth diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..55809c3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 Preston Pan + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile index 5cc8982..9dec505 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ # @version 0.1 all: - cc *.c -o stem + cc *.c -o stem -lm clean: rm stem diff --git a/examples/repl.stem b/examples/repl.stem new file mode 100644 index 0000000..30efb40 --- /dev/null +++ b/examples/repl.stem @@ -0,0 +1,2 @@ +"Welcome to the REPL; exit to exit" . +loop [ "> " read evalstr loop ] func loop diff --git a/main.c b/main.c index 34bad00..9fd2eee 100644 --- a/main.c +++ b/main.c @@ -3,57 +3,51 @@ #include #include -#define MAX 50 -#define JUSTDO(a) \ - if (!(a)) { \ - perror(#a); \ - exit(1); \ - } - extern ht_t *WORD_TABLE; extern array_t *STACK; extern char *INBUF; extern parser_t *PARSER; -/** char *get_line FILE *f - * reads an arbitrarily long line of text or until EOF - * caller must free the pointer returned after use - */ -char *get_line(FILE *f) { - int len = MAX; - char buf[MAX], *e = NULL, *ret; - JUSTDO(ret = calloc(MAX, 1)); - while (fgets(buf, MAX, f)) { - if (len - strlen(ret) < MAX) - JUSTDO(ret = realloc(ret, len *= 2)); - strcat(ret, buf); - if ((e = strrchr(ret, '\n'))) - break; - } - if (e) - *e = '\0'; - return ret; +void usage() { + printf("Usage: stem [-hv] [file]\n"); + exit(1); +} + +void version() { + printf("Author: Preston Pan, MIT License 2023\n"); + printf("stem, version 1.0\n"); + exit(0); } int main(int argc, char **argv) { - PARSER = init_parser(""); value_t *v; + size_t len; + + if (argc < 2) { + usage(); + } + + if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { + usage(); + } else if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { + version(); + } + + FILE *fp = fopen(argv[1], "rb"); + ssize_t bytes_read = getdelim(&INBUF, &len, '\0', fp); + + PARSER = init_parser(INBUF); STACK = init_array(10); WORD_TABLE = init_ht(500); - printf("exit to exit REPL;\n"); while (1) { - printf("> "); - INBUF = get_line(stdin); - parser_reset(PARSER, INBUF); - while (1) { - v = parser_get_next(PARSER); - if (v == NULL) - break; - eval(v); - } - free(INBUF); + v = parser_get_next(PARSER); + if (v == NULL) + break; + eval(v); } + + free(INBUF); ht_free(WORD_TABLE); array_free(STACK); free(PARSER); diff --git a/parser.c b/parser.c index cee5e3b..0ea903a 100644 --- a/parser.c +++ b/parser.c @@ -7,6 +7,13 @@ #include #include +#define MAX 50 +#define JUSTDO(a) \ + if (!(a)) { \ + perror(#a); \ + exit(1); \ + } + array_t *STACK; ht_t *WORD_TABLE; char *INBUF; @@ -328,10 +335,33 @@ void print_value(value_t *v) { print_value(v->quote->items[i]); } break; + case VERR: + printf("STACK ERR\n"); + break; } } -void eval_error() { exit(1); } +char *get_line(FILE *f) { + int len = MAX; + char buf[MAX], *e = NULL, *ret; + JUSTDO(ret = calloc(MAX, 1)); + while (fgets(buf, MAX, f)) { + if (len - strlen(ret) < MAX) + JUSTDO(ret = realloc(ret, len *= 2)); + strcat(ret, buf); + if ((e = strrchr(ret, '\n'))) + break; + } + if (e) + *e = '\0'; + return ret; +} + +bool eval_error() { + value_t *v = init_value(VERR); + array_append(STACK, v); + return true; +} bool eval_builtins(value_t *v) { char *str = v->str_word->value; @@ -342,31 +372,44 @@ bool eval_builtins(value_t *v) { if (strcmp(str, "func") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); + } + if (v1->type != VWORD) { + value_free(v); + return eval_error(); } - if (v1->type != VWORD) - eval_error(); ht_add(WORD_TABLE, string_copy(v1->str_word), v2); value_free(v1); } else if (strcmp(str, "+") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); } retval = init_value(VFLOAT); if (v1->type == VINT && v2->type == VINT) { retval->type = VINT; } + if (v1->type != VINT && v1->type != VFLOAT || + v2->type != VINT && v2->type != VFLOAT) { + array_append(STACK, v1); + array_append(STACK, v2); + value_free(v); + return eval_error(); + } retval->int_float = v1->int_float + v2->int_float; array_append(STACK, retval); value_free(v1); @@ -374,12 +417,14 @@ bool eval_builtins(value_t *v) { } else if (strcmp(str, "-") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); } retval = init_value(VFLOAT); if (v1->type == VINT && v2->type == VINT) { @@ -392,12 +437,14 @@ bool eval_builtins(value_t *v) { } else if (strcmp(str, "/") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); } retval = init_value(VFLOAT); if (v1->type == VINT && v2->type == VINT) { @@ -410,12 +457,14 @@ bool eval_builtins(value_t *v) { } else if (strcmp(str, "*") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); } retval = init_value(VFLOAT); if (v1->type == VINT && v2->type == VINT) { @@ -428,12 +477,14 @@ bool eval_builtins(value_t *v) { } else if (strcmp(str, "pow") == 0) { v2 = array_pop(STACK); if (v2 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } v1 = array_pop(STACK); if (v1 == NULL) { + value_free(v); array_append(STACK, v2); - eval_error(); + return eval_error(); } retval = init_value(VFLOAT); if (v1->type == VINT && v2->type == VINT) { @@ -446,7 +497,9 @@ bool eval_builtins(value_t *v) { } else if (strcmp(str, "eval") == 0) { v1 = array_pop(STACK); if (v1 == NULL) { - eval_error(); + value_free(v); + array_append(STACK, v1); + return eval_error(); } if (v1->type == VQUOTE) { for (int i = 0; i < v1->quote->size; i++) { @@ -456,61 +509,100 @@ bool eval_builtins(value_t *v) { } else { eval(v1); } + } else if (strcmp(str, "evalstr") == 0) { + v1 = array_pop(STACK); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } + if (v1->type != VSTR) { + array_append(STACK, v1); + value_free(v); + return eval_error(); + } + parser_t *tmp_p = init_parser(v1->str_word->value); + value_t *cur; + while (1) { + cur = parser_get_next(tmp_p); + if (cur == NULL) + break; + eval(cur); + } + free(tmp_p); + value_free(v1); + } else if (strcmp(str, ".") == 0) { v1 = array_pop(STACK); if (v1 == NULL) { - eval_error(); + value_free(v); + return eval_error(); } print_value(v1); value_free(v1); + } else if (strcmp(str, "qstack") == 0) { + retval = init_value(VQUOTE); + retval->quote = array_copy(STACK); + array_append(STACK, retval); } else if (strcmp(str, "?") == 0) { for (int i = 0; i < STACK->size; i++) { print_value(STACK->items[i]); } } else if (strcmp(str, "sin") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = sinhl(v1->int_float); array_append(STACK, retval); value_free(v1); } else if (strcmp(str, "cos") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = coshl(v1->int_float); array_append(STACK, retval); value_free(v1); } else if (strcmp(str, "exp") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = expl(v1->int_float); array_append(STACK, retval); value_free(v1); } else if (strcmp(str, "floor") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = floor(v1->int_float); array_append(STACK, retval); value_free(v1); } else if (strcmp(str, "ceil") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = ceil(v1->int_float); array_append(STACK, retval); value_free(v1); } else if (strcmp(str, "ln") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VFLOAT); retval->int_float = logl(v1->int_float); array_append(STACK, retval); @@ -521,11 +613,16 @@ bool eval_builtins(value_t *v) { array_append(STACK, retval); } else if (strcmp(str, "compose") == 0) { v2 = array_pop(STACK); - if (v2 == NULL) - eval_error(); + if (v2 == NULL) { + value_free(v); + return eval_error(); + } v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + return eval_error(); + } if (v2->type == VSTR && v1->type == VSTR) { retval = init_value(VSTR); @@ -545,25 +642,38 @@ bool eval_builtins(value_t *v) { free(v2->quote); free(v2); } else { - eval_error(); + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); } array_append(STACK, retval); } else if (strcmp(str, "wtostr") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } - if (v1->type != VWORD) - eval_error(); + if (v1->type != VWORD) { + value_free(v); + array_append(STACK, v1); + return eval_error(); + } v1->type = VSTR; array_append(STACK, v1); } else if (strcmp(str, "=") == 0) { v2 = array_pop(STACK); - if (v2 == NULL) - eval_error(); + if (v2 == NULL) { + value_free(v); + return eval_error(); + } v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + return eval_error(); + } retval = init_value(VINT); if (v1->type == VSTR && v2->type == VSTR || @@ -573,24 +683,38 @@ bool eval_builtins(value_t *v) { (v2->type == VINT || v2->type == VFLOAT)) { retval->int_float = v1->int_float == v2->int_float; } else { - eval_error(); + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); } array_append(STACK, retval); value_free(v1); value_free(v2); } else if (strcmp(str, "if") == 0) { v3 = array_pop(STACK); - if (v3 == NULL) - eval_error(); + if (v3 == NULL) { + value_free(v); + return eval_error(); + } v2 = array_pop(STACK); - if (v2 == NULL) - eval_error(); + if (v2 == NULL) { + value_free(v); + array_append(STACK, v3); + return eval_error(); + } v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + array_append(STACK, v3); + return eval_error(); + } - if (v3->type != VINT) - eval_error(); + if (v3->type != VINT) { + value_free(v); + return eval_error(); + } if (v3->int_float) { if (v1->type == VQUOTE) { @@ -620,21 +744,32 @@ bool eval_builtins(value_t *v) { } } else if (strcmp(str, "curry") == 0) { v2 = array_pop(STACK); - if (v2 == NULL) - eval_error(); + if (v2 == NULL) { + value_free(v); + return eval_error(); + } v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + return eval_error(); + } - if (v2->type != VQUOTE) - eval_error(); + if (v2->type != VQUOTE) { + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); + } array_append(v2->quote, v1); array_append(STACK, v2); } else if (strcmp(str, "len") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = init_value(VINT); if (v1->type == VINT || v1->type == VFLOAT) { @@ -648,43 +783,99 @@ bool eval_builtins(value_t *v) { array_append(STACK, retval); } else if (strcmp(str, "dup") == 0) { v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } retval = value_copy(v1); array_append(STACK, v1); array_append(STACK, retval); + } else if (strcmp(str, "type") == 0) { + v1 = array_pop(STACK); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } + + retval = init_value(VINT); + retval->int_float = v1->type; + array_append(STACK, v1); + array_append(STACK, retval); + } else if (strcmp(str, "quote") == 0) { + v1 = array_pop(STACK); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } + + retval = init_value(VQUOTE); + retval->quote = init_array(10); + array_append(retval->quote, v1); + array_append(STACK, retval); } else if (strcmp(str, "exit") == 0) { ht_free(WORD_TABLE); array_free(STACK); free(INBUF); free(PARSER); value_free(v); - exit(1); + exit(0); + } else if (strcmp(str, "read") == 0) { + v1 = array_pop(STACK); + if (v1 == NULL) { + value_free(v); + return eval_error(); + } + + if (v1->type != VSTR) { + value_free(v); + array_append(STACK, v1); + return eval_error(); + } + printf("%s", v1->str_word->value); + retval = init_value(VSTR); + retval->str_word = init_string(get_line(stdin)); + array_append(STACK, retval); + value_free(v1); } else if (strcmp(str, "vat") == 0) { v2 = array_pop(STACK); - if (v2 == NULL) - eval_error(); + if (v2 == NULL) { + value_free(v); + return eval_error(); + } v1 = array_pop(STACK); - if (v1 == NULL) - eval_error(); + if (v1 == NULL) { + value_free(v); + array_append(STACK, v2); + return eval_error(); + } if (v1->type != VINT) { - eval_error(); + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); } if (v2->type == VINT || v2->type == VFLOAT) { - eval_error(); + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); } if (v2->type == VQUOTE) { if (v2->quote->size <= v1->int_float) { - eval_error(); + value_free(v); + array_append(STACK, v1); + array_append(STACK, v2); + return eval_error(); } array_append(STACK, v2); array_append(STACK, value_copy(v2->quote->items[(int)v1->int_float])); value_free(v1); } else { - eval_error(); + value_free(v); + return eval_error(); } } else { return false; @@ -714,19 +905,17 @@ void eval(value_t *v) { case VFLOAT: case VSTR: case VQUOTE: + case VERR: array_append(STACK, v); - printf("OK\n"); break; case VWORD: if (v->escaped) { v->escaped = false; array_append(STACK, v); - printf("OK\n"); } else { if (!eval_builtins(v)) { if (!eval_ht(v)) { array_append(STACK, v); - printf("OK\n"); } } } diff --git a/parser.h b/parser.h index 168c7e9..334834a 100644 --- a/parser.h +++ b/parser.h @@ -14,7 +14,7 @@ struct ARRAY_STRUCT { }; struct VALUE_STRUCT { - enum { VWORD, VINT, VFLOAT, VSTR, VQUOTE } type; + enum { VWORD, VINT, VFLOAT, VSTR, VQUOTE, VERR } type; union { long double int_float; array_t *quote; diff --git a/uforth b/uforth deleted file mode 100755 index fc38117..0000000 Binary files a/uforth and /dev/null differ -- cgit