diff --git a/src/Makefile b/src/Makefile index ff3b590..538d925 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/src/env.c b/src/env.c index 5e53099..a1b0e65 100644 --- a/src/env.c +++ b/src/env.c @@ -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 }; diff --git a/src/env.h b/src/env.h index e7b661a..c3acbfc 100644 --- a/src/env.h +++ b/src/env.h @@ -12,6 +12,8 @@ struct Environment { int structCount; struct StructDef* structDefs; + + int refs; }; Object fetchFromEnvironment(const char* name, struct Environment* env); diff --git a/src/pebblisp.c b/src/pebblisp.c index 8b8caf1..4902033 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -7,6 +7,9 @@ #include #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) { diff --git a/src/pebblisp.h b/src/pebblisp.h index 4b097fd..f742d70 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -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