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
BINPREFIX ?= /usr/bin
@ -6,7 +6,7 @@ SCRIPTDIR ?= /usr/local/share/pebblisp
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
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:
$(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);
struct Environment env = {.outer = outer,
struct Environment env = {
.outer = outer,
.strings = NULL,
.objects = NULL,
.structCount = 0,
.size = paramCount};
.size = paramCount,
.refs = 1,
};
if (paramCount == 0) {
return env;
@ -70,6 +73,10 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
march = march->forward;
}
while(outer) {
outer->refs += 1;
outer = outer->outer;
}
return env;
}
@ -179,6 +186,13 @@ void addFunc(const char* name,
void deleteEnv(struct Environment* e)
{
e->refs -= 1;
if (e->refs) {
return;
}
if (e->outer) {
deleteEnv(e->outer);
}
if (e->strings) {
int i = 0;
while (e->strings[i]) {
@ -212,6 +226,7 @@ struct Environment defaultEnv()
.objects = objects,
.size = size,
.structCount = 0,
.refs = 1,
};
struct symFunc symFuncs[] = {
@ -248,6 +263,8 @@ struct Environment defaultEnv()
//{"sys", &systemCall},
{"loadfile", &loadFile},
{"inp", &takeInput},
{"addroute", &addRouteO},
{"serve", &startServer}
#endif
};

View File

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

View File

@ -7,6 +7,9 @@
#include <string.h>
#include "tokens.h"
#ifdef STANDALONE
#include "web.h"
#endif
/**
* 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);
FOR_POINTER_IN_LIST(obj) {
if (i != 0) {
if (isStringy(*POINTER)) {
printf("evalList elementName: %s\n", POINTER->string);
}
nf_addToList(&list, eval(POINTER, env));
}
i++;
@ -647,6 +647,29 @@ Object print(Object p, Object ignore, struct Environment* env)
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)
{
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);
Object evalLambdaArgs(const Object* arg_forms);
Object listEvalLambda(Object* lambda, const Object* remaining,
struct Environment* env);
// Slices
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 startServer(Object path, Object textFunc, struct Environment* env);
Object addRouteO(Object path, Object textFunc, struct Environment* env);
#endif
#endif