Add simple webserver functionality.

This commit is contained in:
Sage Vaillancourt 2022-03-14 23:46:20 -04:00
parent 6c8ff04711
commit 11f39bf136
5 changed files with 54 additions and 7 deletions

View File

@ -1,4 +1,4 @@
files = pebblisp.c tokens.c object.c env.c files = pebblisp.c tokens.c object.c env.c web.c
exe = pl exe = pl
BINPREFIX ?= /usr/bin BINPREFIX ?= /usr/bin
@ -6,7 +6,7 @@ SCRIPTDIR ?= /usr/local/share/pebblisp
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(dir $(mkfile_path)) mkfile_dir := $(dir $(mkfile_path))
GCC_COM ?= gcc -g -O0 -Wall -o $(exe) -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\" GCC_COM ?= gcc -lmicrohttpd -g -O0 -Wall -o $(exe) -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\"
all: all:
$(GCC_COM) $(files) && echo && ./tests.sh $(GCC_COM) $(files) && echo && ./tests.sh

View File

@ -47,11 +47,14 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
{ {
int paramCount = listLength(params); int paramCount = listLength(params);
struct Environment env = {.outer = outer, struct Environment env = {
.outer = outer,
.strings = NULL, .strings = NULL,
.objects = NULL, .objects = NULL,
.structCount = 0, .structCount = 0,
.size = paramCount}; .size = paramCount,
.refs = 1,
};
if (paramCount == 0) { if (paramCount == 0) {
return env; return env;
@ -70,6 +73,10 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
march = march->forward; march = march->forward;
} }
while(outer) {
outer->refs += 1;
outer = outer->outer;
}
return env; return env;
} }
@ -179,6 +186,13 @@ void addFunc(const char* name,
void deleteEnv(struct Environment* e) void deleteEnv(struct Environment* e)
{ {
e->refs -= 1;
if (e->refs) {
return;
}
if (e->outer) {
deleteEnv(e->outer);
}
if (e->strings) { if (e->strings) {
int i = 0; int i = 0;
while (e->strings[i]) { while (e->strings[i]) {
@ -212,6 +226,7 @@ struct Environment defaultEnv()
.objects = objects, .objects = objects,
.size = size, .size = size,
.structCount = 0, .structCount = 0,
.refs = 1,
}; };
struct symFunc symFuncs[] = { struct symFunc symFuncs[] = {
@ -248,6 +263,8 @@ struct Environment defaultEnv()
//{"sys", &systemCall}, //{"sys", &systemCall},
{"loadfile", &loadFile}, {"loadfile", &loadFile},
{"inp", &takeInput}, {"inp", &takeInput},
{"addroute", &addRouteO},
{"serve", &startServer}
#endif #endif
}; };

View File

@ -12,6 +12,8 @@ struct Environment {
int structCount; int structCount;
struct StructDef* structDefs; struct StructDef* structDefs;
int refs;
}; };
Object fetchFromEnvironment(const char* name, struct Environment* env); Object fetchFromEnvironment(const char* name, struct Environment* env);

View File

@ -7,6 +7,9 @@
#include <string.h> #include <string.h>
#include "tokens.h" #include "tokens.h"
#ifdef STANDALONE
#include "web.h"
#endif
/** /**
* Inserts a variable into the environment with a given name and value. * Inserts a variable into the environment with a given name and value.
@ -350,9 +353,6 @@ Object evalList(const Object* obj, struct Environment* env)
nf_addToList(&list, first_eval); nf_addToList(&list, first_eval);
FOR_POINTER_IN_LIST(obj) { FOR_POINTER_IN_LIST(obj) {
if (i != 0) { if (i != 0) {
if (isStringy(*POINTER)) {
printf("evalList elementName: %s\n", POINTER->string);
}
nf_addToList(&list, eval(POINTER, env)); nf_addToList(&list, eval(POINTER, env));
} }
i++; i++;
@ -647,6 +647,29 @@ Object print(Object p, Object ignore, struct Environment* env)
return numberObject(0); return numberObject(0);
} }
Object addRouteO(Object path, Object textFunc, struct Environment* env)
{
const char* p = malloc(sizeof(char) * strlen(path.string) + 1);
strcpy(p, path.string);
//const char* t = malloc(sizeof(char) * strlen(textFunc.string) + 1);
//strcpy(t, textFunc.string);
struct Route r;
r.path = p;
r.routeAction = cloneObject(textFunc);
r.env = env;
env->refs += 1;
//r.text = t;
addRoute(r);
return numberObject(1);
}
Object startServer(Object path, Object textFunc, struct Environment* env)
{
return numberObject(start(8888));
}
Object pChar(Object c, Object i1, struct Environment* i2) Object pChar(Object c, Object i1, struct Environment* i2)
{ {
if (c.type != TYPE_NUMBER) { if (c.type != TYPE_NUMBER) {

View File

@ -29,6 +29,8 @@ void evalForms(Object* destList, const Object* src, struct Environment* env);
void copySlice(char* dest, struct Slice* src); void copySlice(char* dest, struct Slice* src);
Object evalLambdaArgs(const Object* arg_forms); Object evalLambdaArgs(const Object* arg_forms);
Object listEvalLambda(Object* lambda, const Object* remaining,
struct Environment* env);
// Slices // Slices
void copySlice(char* dest, struct Slice* src); void copySlice(char* dest, struct Slice* src);
@ -100,6 +102,9 @@ Object systemCall(Object call, Object _, struct Environment* i3);
Object loadFile(Object filename, Object _, struct Environment* env); Object loadFile(Object filename, Object _, struct Environment* env);
Object startServer(Object path, Object textFunc, struct Environment* env);
Object addRouteO(Object path, Object textFunc, struct Environment* env);
#endif #endif
#endif #endif