From: Elijah Cohen Date: Sat, 23 Nov 2024 01:13:53 +0000 (+0000) Subject: added tostr, fixed a couple memory leaks X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;ds=sidebyside;p=klapaucius added tostr, fixed a couple memory leaks --- diff --git a/src/builtins/io.c b/src/builtins/io.c index ef3f83d..c514198 100644 --- a/src/builtins/io.c +++ b/src/builtins/io.c @@ -30,10 +30,12 @@ Sexpr* io_printstr(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* arg = eval(clone(car(b->value.b.args)), env); if(arg->type != STR) { sexpr_free(b); + sexpr_free(arg); return cons(from_nil(), rest); } PRINT(arg->value.str); sexpr_free(b); + sexpr_free(arg); return cons(from_t(), rest); } @@ -72,6 +74,8 @@ Sexpr* io_readfile(Sexpr* b, Sexpr* rest, Sexpr* env) { sexpr_free(b); return cons(from_nil(), rest); } + sexpr_free(firstarg); + sexpr_free(b); fseek(thefile, 0, SEEK_END); size_t fsz = ftell(thefile); fseek(thefile, 0, SEEK_SET); diff --git a/src/builtins/strings.c b/src/builtins/strings.c index 09a1d04..7118295 100644 --- a/src/builtins/strings.c +++ b/src/builtins/strings.c @@ -20,11 +20,13 @@ Sexpr* s_strlen(Sexpr* b, Sexpr* rest, Sexpr* env) { if(unquote(arg)->type != STR) { ERR("strlen: ", "argument not string"); sexpr_free(b); + sexpr_free(arg); return cons(from_nil(), rest); } #endif // typecheck size_t sl = strlen(unquote(arg)->value.str); sexpr_free(b); + sexpr_free(arg); return cons (from_uint(sl), rest); } @@ -184,6 +186,20 @@ Sexpr* s_strtok(Sexpr* b, Sexpr* rest, Sexpr* env) { return cons(from_quote(newres), rest); } +Sexpr* s_tostr(Sexpr* b, Sexpr* rest, Sexpr* env) { + if(STRINGS_TOSTR_ARGS != u64_get_num_args(b)) { + return cons(b, rest); + } + Sexpr* args = b->value.b.args; + Sexpr* arg = eval(clone(car(args)), env); + char* thestr = sprint_sexpr(arg); + Sexpr* out = cons(from_string(thestr), rest); + free(thestr); + sexpr_free(arg); + sexpr_free(b); + return out; +} + Sexpr* x_strings_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { uint64_t code = b->value.b.opcode & 0xff; @@ -200,6 +216,8 @@ Sexpr* x_strings_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { return s_substr(b, rest, env); case STRINGS_STRTOK: return s_strtok(b, rest, env); + case STRINGS_TOSTR: + return s_tostr(b, rest, env); default: return from_nil(); } @@ -210,9 +228,10 @@ Sexpr* x_strings_dispatch(Sexpr* b, Sexpr* rest, Sexpr* env) { Sexpr* load_strings_env(Sexpr* env) { load_builtin(STRINGS_STRLEN_STR, (STRINGS_PREFIX << 8) | STRINGS_STRLEN, env); load_builtin(STRINGS_STRCAT_STR, (STRINGS_PREFIX << 8) | STRINGS_STRCAT, env); - load_builtin(STRINGS_STRAT_STR, (STRINGS_PREFIX << 8) | STRINGS_STRAT, env); - load_builtin(STRINGS_STREXPAND_STR, (STRINGS_PREFIX << 8) | STRINGS_STREXPAND, env); - load_builtin(STRINGS_SUBSTR_STR, (STRINGS_PREFIX << 8) | STRINGS_SUBSTR, env); - load_builtin(STRINGS_STRTOK_STR, (STRINGS_PREFIX << 8) | STRINGS_STRTOK, env); + load_builtin(STRINGS_STRAT_STR, (STRINGS_PREFIX << 8) | STRINGS_STRAT, env); + load_builtin(STRINGS_STREXPAND_STR, (STRINGS_PREFIX << 8) | STRINGS_STREXPAND, env); + load_builtin(STRINGS_SUBSTR_STR, (STRINGS_PREFIX << 8) | STRINGS_SUBSTR, env); + load_builtin(STRINGS_STRTOK_STR, (STRINGS_PREFIX << 8) | STRINGS_STRTOK, env); + load_builtin(STRINGS_TOSTR_STR, (STRINGS_PREFIX << 8) | STRINGS_TOSTR, env); return env; } diff --git a/src/builtins/strings.h b/src/builtins/strings.h index f5d2def..289eea5 100644 --- a/src/builtins/strings.h +++ b/src/builtins/strings.h @@ -25,6 +25,9 @@ #define STRINGS_STRTOK 0x05 #define STRINGS_STRTOK_ARGS 2 #define STRINGS_STRTOK_STR "strtok" +#define STRINGS_TOSTR 0x06 +#define STRINGS_TOSTR_ARGS 1 +#define STRINGS_TOSTR_STR "tostr" Sexpr* x_strings_dispatch(Sexpr* s, Sexpr* rest, Sexpr* env); Sexpr* load_strings_env(Sexpr* env); diff --git a/src/test.c b/src/test.c index 8b6611d..5514e9d 100644 --- a/src/test.c +++ b/src/test.c @@ -340,7 +340,34 @@ void eval_tests() { run_eval_test("(+ 5 (* 5))"); run_eval_test("(car 5)"); + + run_eval_test("\"testing\""); + run_eval_test("(strlen \"hi\")"); + run_eval_test("(strlen 54)"); + run_eval_test("(strcat \"hi \" \"there\")"); + run_eval_test("(strcat \"hi \" 55)"); + run_eval_test("(strat 5 \"hello world\")"); + run_eval_test("(strat nil \"hello world\")"); + run_eval_test("(strexpand \"test\")"); + run_eval_test("(strexpand 43)"); + run_eval_test("(substr \"oo\" \"looopy loop\")"); + run_eval_test("(substr 74 \"looopy loop\")"); + run_eval_test("(strtok \" /\" \"this/is a/test\")"); + run_eval_test("(strtok \" /\" 65)"); + + run_eval_test("(print 65)"); + run_eval_test("(printstr 65)"); + run_eval_test("(printstr \"test\")"); + run_eval_test("(pb (+ 5))"); + run_eval_test("(writefile \"neverusethisforanythingelse.txt\" \"test\noutput\")"); + run_eval_test("(writefile 54 \"test output\")"); + run_eval_test("(readfile \"neverusethisforanythingelse.txt\")"); + run_eval_test("(readfile 63)"); + run_eval_test("(readfile \"fileoughtnotexist.txt\")"); + + run_eval_test("(tostr (cons 43 \"test\"))"); + } void many_asserts() { @@ -420,7 +447,7 @@ void run_tests(){ //isolating_problem(); //test_string_parsing(); eval_tests(); - many_asserts(); + //many_asserts(); //memtest_eval(); //mem_testing(); //mem_parser();