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.
This commit is contained in:
Sage Vaillancourt 2022-03-23 19:04:32 -04:00 committed by Sage Vaillancourt
parent 06389ac5fc
commit e00d706522
5 changed files with 61 additions and 17 deletions

View File

@ -0,0 +1,10 @@
html {
background-color: #ddddff;
}
body {
width: 40%;
margin-left: auto;
margin-right: auto;
font-size: 200%;
font-family: sans;
}

View File

@ -36,10 +36,12 @@ and the interpreter won't even instantly crash over it! It's truly astounding
stuff, when you think about it." stuff, when you think about it."
)) ))
(def styleHead (head (
(link ((rel "stylesheet") (href "styles.css")))
)))
(def homepage (fn (req) (html ( (def homepage (fn (req) (html (
(head ( styleHead
(link ((rel "stylesheet") (href "styles.css")))
))
(body ( (body (
(h1 "This is a sweet PebbLisp site") (h1 "This is a sweet PebbLisp site")
(p (cat "" req)) (p (cat "" req))
@ -52,19 +54,8 @@ stuff, when you think about it."
(get "/" homepage) (get "/" homepage)
(get "/x" revealer) (get "/x" revealer)
(get "/styles.css" (fn () ( (def styles (rf "styles.css"))
"html { (get "/styles.css" (fn () (styles)))
background-color: #ddddff;
}
body {
width: 40%;
margin-left: auto;
margin-right: auto;
font-size: 200%;
font-family: sans;
}
"
)))
(def PORT 9090) (def PORT 9090)
(serve PORT) (serve PORT)

View File

@ -729,6 +729,7 @@ void repl(struct Environment* env)
add_history(buf); add_history(buf);
Object o = parseEval(buf, env); Object o = parseEval(buf, env);
printObj(&o); printObj(&o);
cleanObject(&o);
free(buf); free(buf);
} }
} }

View File

@ -353,4 +353,42 @@ Object help(Object symbol, Object ignore, struct Environment* ignore2)
return helpText; 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 #endif // STANDALONE

View File

@ -177,6 +177,10 @@ fn(help,
"(? \"+\") => \"(+ 1 2) => 3\"" "(? \"+\") => \"(+ 1 2) => 3\""
)(Object symbol, Object ignore, struct Environment* ignore2); )(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 // STANDALONE
#endif // PEBBLISP_PLFUNC_H #endif // PEBBLISP_PLFUNC_H