Move help directly into env.

Fix repl leak.
This commit is contained in:
Sage Vaillancourt 2022-03-29 15:09:33 -04:00 committed by Sage Vaillancourt
parent ce6c536567
commit 803935c637
7 changed files with 21 additions and 37 deletions

View File

@ -282,13 +282,18 @@ void printColored(const char* code)
} }
} }
char* getHelp(const char* symbol) fn(help,
{ "Displays help text for the given function.\n"
"Currently requires the function name as a string, but future syntactic sugar may\n"
"loosen this requirement.\n"
"(? \"+\") => \"(+ 1 2) => 3\""
) {
const char* symbol = params[0].string;
for (int i = 0; i < currentHelp; i++) { for (int i = 0; i < currentHelp; i++) {
struct helpText h = helpTexts[i]; struct helpText h = helpTexts[i];
if (strcmp(symbol, h.symbol) == 0) { if (strcmp(symbol, h.symbol) == 0) {
char* text = calloc(sizeof(char), 1024); Object text = withLen(1024, TYPE_STRING);
char* textCursor = text; char* textCursor = text.string;
textCursor += sprintf(textCursor, "%s", h.help); textCursor += sprintf(textCursor, "%s", h.help);
for (int ti = 0; ti < h.testCount; ti += 2) { for (int ti = 0; ti < h.testCount; ti += 2) {
const char* test = h.tests[ti]; const char* test = h.tests[ti];
@ -303,10 +308,7 @@ char* getHelp(const char* symbol)
return text; return text;
} }
} }
const char* notFound = "Help not found!"; return nullTerminated("Help not found!");
char* help = malloc(sizeof(char) * (strlen(notFound) + 1));
sprintf(help, "%s", notFound);
return help;
} }
fnn(segfault, "seg", "Induces a segfault.") fnn(segfault, "seg", "Induces a segfault.")

View File

@ -49,9 +49,6 @@ struct StructDef* getStructAt(int i);
void addStructDef(struct StructDef def); void addStructDef(struct StructDef def);
/// Needs to be freed!
char* getHelp(const char* symbol);
void printColored(const char* code); void printColored(const char* code);
int runTests(int detailed); int runTests(int detailed);

View File

@ -610,6 +610,7 @@ void repl(struct Environment* env)
size_t length; size_t length;
char *output = stringObj(&o, &length); char *output = stringObj(&o, &length);
printColored(output); printColored(output);
free(output);
printf("\n"); printf("\n");
cleanObject(&o); cleanObject(&o);
free(buf); free(buf);
@ -620,7 +621,7 @@ void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)
{ {
Object args = listObject(); Object args = listObject();
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
nf_addToList(&args, stringFromSlice(argv[i], strlen(argv[i]))); nf_addToList(&args, nullTerminated(argv[i]));
} }
addToEnv(env, "args", args); addToEnv(env, "args", args);
} }

View File

@ -443,16 +443,6 @@ Object systemCall(Object* params, int length, struct Environment* env)
return numberObject(255); return numberObject(255);
} }
Object help(Object* params, int length, struct Environment* env)
{
Object symbol = params[0];
char* help = getHelp(symbol.string);
Object helpText = newObject(TYPE_STRING);
helpText.string = help;
return helpText;
}
char* readFileToString(FILE* input) char* readFileToString(FILE* input)
{ {
size_t capacity = 128; size_t capacity = 128;

View File

@ -195,14 +195,6 @@ fn(takeInput,
"`(def x (input \">> \"))` wait for input, but prompt the user with '>> '.\n" "`(def x (input \">> \"))` wait for input, but prompt the user with '>> '.\n"
); );
/// STRING => STRING
fn(help,
"Displays help text for the given function.\n"
"Currently requires the function name as a string, but future syntactic sugar may\n"
"loosen this requirement.\n"
"(? \"+\") => \"(+ 1 2) => 3\""
);
tfn(readFileToObject, tfn(readFileToObject,
({ isStringy, isStringy }), ({ isStringy, isStringy }),
"Read a file into a string object." "Read a file into a string object."

View File

@ -54,7 +54,7 @@ add_query_param(void* queryParamsV, enum MHD_ValueKind kind, const char* key,
(void) kind; /* Unused. Silent compiler warning. */ (void) kind; /* Unused. Silent compiler warning. */
Object* queryParams = queryParamsV; Object* queryParams = queryParamsV;
Object pair = startList(stringFromSlice(key, strlen(key))); Object pair = startList(nullTerminated(key));
Object parsed; Object parsed;
if (isDigit(value[0])) { if (isDigit(value[0])) {
parsed = parseEval(value, NULL); parsed = parseEval(value, NULL);
@ -73,6 +73,10 @@ answer_to_connection(void* cls, struct MHD_Connection* connection,
const char* version, const char* upload_data, const char* version, const char* upload_data,
size_t* upload_data_size, void** con_cls) size_t* upload_data_size, void** con_cls)
{ {
(void) cls; /* Unused. Silence compiler warning. */
(void) upload_data_size; /* Unused. Silence compiler warning. */
(void) con_cls; /* Unused. Silence compiler warning. */
char* page = NULL; char* page = NULL;
printf("%s :: %s URL: '%s'\n", method, version, url); printf("%s :: %s URL: '%s'\n", method, version, url);
if (upload_data) { if (upload_data) {
@ -107,10 +111,6 @@ answer_to_connection(void* cls, struct MHD_Connection* connection,
page = "<html><body><h1>404, Dumbass.</h1></body></html>"; page = "<html><body><h1>404, Dumbass.</h1></body></html>";
} }
(void) cls; /* Unused. Silent compiler warning. */
(void) upload_data_size; /* Unused. Silent compiler warning. */
(void) con_cls; /* Unused. Silent compiler warning. */
struct MHD_Response* response = MHD_create_response_from_buffer( struct MHD_Response* response = MHD_create_response_from_buffer(
strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT); strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT);
HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response); HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response);

View File

@ -10,8 +10,10 @@ fn(startServer,
fn(addGetRoute, fn(addGetRoute,
"Note: Implementation bugs currently prevent using an inline lambda.\n" "Note: Implementation bugs currently prevent using an inline lambda.\n"
" (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n" " (def homepage (fn () (\"Hello, world!\")))\n"
" (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n" " (get \"/\" homepage)\n"
" (def queryPage (fn (req) (req's queryParams)))\n"
" (get \"/x\" queryPage)\n"
" (serve)\n" " (serve)\n"
); );