From e00d7065220839aa5d1c140faa36774f39741374 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 23 Mar 2022 19:04:32 -0400 Subject: [PATCH] Add function to read a file into a string object. Move webby.pl into dedicate examples/web/ dir. Pull out styles.css into its own file. Fix small parseEval leak. --- src/examples/web/styles.css | 10 +++++++++ src/examples/{ => web}/webby.pl | 23 ++++++-------------- src/pebblisp.c | 1 + src/plfunc.c | 38 +++++++++++++++++++++++++++++++++ src/plfunc.h | 6 +++++- 5 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 src/examples/web/styles.css rename src/examples/{ => web}/webby.pl (85%) diff --git a/src/examples/web/styles.css b/src/examples/web/styles.css new file mode 100644 index 0000000..1204639 --- /dev/null +++ b/src/examples/web/styles.css @@ -0,0 +1,10 @@ +html { + background-color: #ddddff; +} +body { + width: 40%; + margin-left: auto; + margin-right: auto; + font-size: 200%; + font-family: sans; +} diff --git a/src/examples/webby.pl b/src/examples/web/webby.pl similarity index 85% rename from src/examples/webby.pl rename to src/examples/web/webby.pl index a831f20..1981a8e 100755 --- a/src/examples/webby.pl +++ b/src/examples/web/webby.pl @@ -36,10 +36,12 @@ and the interpreter won't even instantly crash over it! It's truly astounding stuff, when you think about it." )) +(def styleHead (head ( + (link ((rel "stylesheet") (href "styles.css"))) +))) + (def homepage (fn (req) (html ( - (head ( - (link ((rel "stylesheet") (href "styles.css"))) - )) + styleHead (body ( (h1 "This is a sweet PebbLisp site") (p (cat "" req)) @@ -52,19 +54,8 @@ stuff, when you think about it." (get "/" homepage) (get "/x" revealer) -(get "/styles.css" (fn () ( -"html { - background-color: #ddddff; -} -body { - width: 40%; - margin-left: auto; - margin-right: auto; - font-size: 200%; - font-family: sans; -} -" -))) +(def styles (rf "styles.css")) +(get "/styles.css" (fn () (styles))) (def PORT 9090) (serve PORT) diff --git a/src/pebblisp.c b/src/pebblisp.c index bfea7da..8543a92 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -729,6 +729,7 @@ void repl(struct Environment* env) add_history(buf); Object o = parseEval(buf, env); printObj(&o); + cleanObject(&o); free(buf); } } diff --git a/src/plfunc.c b/src/plfunc.c index b154374..f507a77 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -353,4 +353,42 @@ Object help(Object symbol, Object ignore, struct Environment* ignore2) return helpText; } +char* readFileToString(FILE* input) +{ + size_t capacity = 128; + char* string = malloc(sizeof(char) * capacity); + int c; + int i = 0; + + while ((c = fgetc(input)) != EOF) { + string[i] = c; + i++; + if (i == capacity) { + char* prev = string; + capacity *= 2; + string = malloc(sizeof(char) * capacity); + memcpy(string, prev, sizeof(char) * capacity / 2); + free(prev); + } + } + + return string; +} + +Object readFileToObject(Object filename, Object ignore, struct Environment* ignore2) +{ + if (filename.type != TYPE_STRING) { + return errorObject(NULL_PARSE); + } + + FILE* file = fopen(filename.string, "r"); + if (!file) { + return errorObject(NULL_PARSE); + } + + Object string = newObject(TYPE_STRING); + string.string = readFileToString(file); + return string; +} + #endif // STANDALONE \ No newline at end of file diff --git a/src/plfunc.h b/src/plfunc.h index d1a9ea8..37cbfe5 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -177,6 +177,10 @@ fn(help, "(? \"+\") => \"(+ 1 2) => 3\"" )(Object symbol, Object ignore, struct Environment* ignore2); +fn(readFileToObject, + "Read a file into a string object." +)(Object filename, Object ignore, struct Environment* ignore2); + #endif // STANDALONE -#endif // PEBBLISP_PLFUNC_H +#endif // PEBBLISP_PLFUNC_H \ No newline at end of file