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);
}
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);
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);
}
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;
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();
}
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;
}
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() {
//isolating_problem();
//test_string_parsing();
eval_tests();
- many_asserts();
+ //many_asserts();
//memtest_eval();
//mem_testing();
//mem_parser();