From 30e8c87e660a360c756c4663a2d527791dc2f139 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Sat, 19 Mar 2022 22:25:20 -0400 Subject: [PATCH] General code clean-up. --- src/env.h | 7 ++-- src/object.c | 9 +++-- src/object.h | 106 +++++++++++++++++++++++++------------------------ src/pebblisp.c | 30 ++++++++------ src/pebblisp.h | 9 ++--- src/tokens.h | 6 +-- src/web.c | 15 +++++-- 7 files changed, 100 insertions(+), 82 deletions(-) diff --git a/src/env.h b/src/env.h index 478f8ed..97d2b99 100644 --- a/src/env.h +++ b/src/env.h @@ -13,19 +13,20 @@ struct Environment { int structCount; int structCapacity; struct StructDef* structDefs; - + int refs; }; struct Environment* global(); -void setGlobal(struct Environment *env); + +void setGlobal(struct Environment* env); Object fetchFromEnvironment(const char* name, struct Environment* env); struct Environment envForLambda(const Object* params, const Object* arg_forms, struct Environment* outer); -void addToEnv(struct Environment* env, const char* name, const Object obj); +void addToEnv(struct Environment* env, const char* name, Object obj); void printEnv(struct Environment* env); diff --git a/src/object.c b/src/object.c index c00bcd3..37c2df1 100644 --- a/src/object.c +++ b/src/object.c @@ -6,7 +6,7 @@ size_t bytes = 0; -int getBytes() +size_t getBytes() { return bytes; } @@ -381,8 +381,10 @@ char* stringNObj(char* dest, const Object* obj, const size_t len) if (obj->error->context && obj->error->context[0] != '\0') { stringf(dest, len, "'%s': %s", errorText[code], obj->error->context); - } else { + } else if (code >= 0 && code <= INDEX_PAST_END) { stringf(dest, len, "%s", errorText[code]); + } else { + stringf(dest, len, "BROKEN ERROR CODE: %d", code); } #endif break; @@ -430,7 +432,6 @@ void debugObj(const Object* obj) #ifdef DEBUG printObj(obj); #endif - return; } #if defined(DEBUG) || defined(STANDALONE) @@ -786,7 +787,7 @@ inline Object cloneOther(const Object src) return src.other->clone ? src.other->clone(src.other) : src; } -Object cloneStruct(const Object src); +Object cloneStruct(Object src); inline Object cloneObject(const Object src) { diff --git a/src/object.h b/src/object.h index 53a84fb..4b4bbfb 100644 --- a/src/object.h +++ b/src/object.h @@ -89,29 +89,30 @@ struct Slice; struct Other; struct Error { enum errorCode code; - char *context; + char* context; }; struct Object { Type type; + union { int number; - Object *list; - char *string; + Object* list; + char* string; - Object (*func)(Object, Object, struct Environment *); + Object (* func)(Object, Object, struct Environment*); - struct StructObject *structObject; - struct Lambda *lambda; - struct Other *other; + struct StructObject* structObject; + struct Lambda* lambda; + struct Other* other; #ifdef SIMPLE_ERRORS enum errorCode error; #else - struct Error *error; + struct Error* error; #endif }; - Object *forward; + Object* forward; }; struct StructDef { @@ -132,88 +133,88 @@ struct Lambda { }; struct Other { - void (*cleanup)(Object *); + void (* cleanup)(Object*); - Object (*clone)(struct Other *); + Object (* clone)(struct Other*); - void *data; + void* data; }; -char *stringNObj(char *dest, const Object *obj, size_t len); +char* stringNObj(char* dest, const Object* obj, size_t len); -char *stringObj(char *dest, const Object *obj); +char* stringObj(char* dest, const Object* obj); -void printList(const Object *list); +void printList(const Object* list); -void printType(const Object *obj); +void printType(const Object* obj); -void printObj(const Object *obj); +void printObj(const Object* obj); -void _printObj(const Object *obj, int newline); +void _printObj(const Object* obj, int newline); -void debugObj(const Object *obj); +void debugObj(const Object* obj); -void printErr(const Object *obj); +void printErr(const Object* obj); -int isEmpty(const Object *obj); +int isEmpty(const Object* obj); -Object *tail(const Object *listObj); +Object* tail(const Object* listObj); -void insertIntoList(Object *dest, int ind, const Object src); +void insertIntoList(Object* dest, int ind, Object src); -void replaceListing(Object *list, int i, const Object src); +void replaceListing(Object* list, int i, Object src); -void nf_addToList(Object *dest, Object src); +void nf_addToList(Object* dest, Object src); -void deleteList(Object *dest); +void deleteList(Object* dest); -int listLength(const Object *listObj); +int listLength(const Object* listObj); -Object *itemAt(const Object *listObj, int n); +Object* itemAt(const Object* listObj, int n); -void copyList(Object *dest, const Object *src); +void copyList(Object* dest, const Object* src); -void cleanObject(Object *target); +void cleanObject(Object* target); -void printAndClean(Object *target); +void printAndClean(Object* target); -void allocObject(Object **spot, const Object src); +void allocObject(Object** spot, Object src); -void appendList(Object *dest, const Object *src); +void appendList(Object* dest, const Object* src); -int isListy(const Object test); +int isListy(Object test); -int isStringy(const Object test); +int isStringy(Object test); -int isValidType(const Object test); +int isValidType(Object test); -int isError(const Object obj, const enum errorCode err); +int isError(Object obj, enum errorCode err); -int bothAre(const enum Type type, const Object *obj1, const Object *obj2); +int bothAre(enum Type type, const Object* obj1, const Object* obj2); -int eitherIs(const enum Type type, const Object *obj1, const Object *obj2); +int eitherIs(enum Type type, const Object* obj1, const Object* obj2); -int areSameType(const Object *obj1, const Object *obj2); +int areSameType(const Object* obj1, const Object* obj2); -Object cloneList(const Object src); +Object cloneList(Object src); Object cloneString(Object obj); -Object cloneLambda(const Object old); +Object cloneLambda(Object old); -Object cloneObject(const Object src); +Object cloneObject(Object src); Object newObject(Type type); Object listObject(); -Object startList(const Object start); +Object startList(Object start); -Object objFromSlice(const char *string, int len); +Object objFromSlice(const char* string, int len); -Object stringFromSlice(const char *string, int len); +Object stringFromSlice(const char* string, int len); -Object symFromSlice(const char *string, int len); +Object symFromSlice(const char* string, int len); Object boolObject(int b); @@ -225,7 +226,7 @@ Object otherObject(); Object errorObject(enum errorCode err); -enum errorCode getErrorCode(const Object obj); +enum errorCode getErrorCode(Object obj); Object errorWithContextLineNo(enum errorCode code, const char* context, int lineNo, const char* fileName); @@ -234,17 +235,20 @@ Object errorWithContextLineNo(enum errorCode code, const char* context, int line #define errorAddContext(x, y, z, a) ; #else #define errorWithContext(_code, _context) errorWithContextLineNo(_code, _context, __LINE__, __FILE__) + void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName); + #endif struct Error noError(); -Object constructLambda(const Object *params, const Object *body, struct Environment* env); +Object constructLambda(const Object* params, const Object* body, struct Environment* env); // Object version of listLength() -Object len(Object obj1, Object, struct Environment *); +Object len(Object obj1, Object, struct Environment*); int getAllocations(); -int getBytes(); + +size_t getBytes(); #endif diff --git a/src/pebblisp.c b/src/pebblisp.c index d43f57d..1de7a6e 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -8,8 +8,11 @@ #include #include "tokens.h" + #ifdef STANDALONE + #include "web.h" + #endif /** @@ -47,7 +50,7 @@ Object evalDefArgs(const Object* symbol, const Object* value, return cloneObject(*symbol); } -void printStructDef(const struct StructDef *def) +void printStructDef(const struct StructDef* def) { printf("%s: {\n", def->name); for (int i = 0; i < def->fieldCount; i++) { @@ -208,7 +211,8 @@ void evalForms(Object* destArr, const Object* start, struct Environment* env) } Object listEvalFunc(const Object* list, const Object* function, - const int length, struct Environment* env); + int length, struct Environment* env); + Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Environment* env) { Object funcList = startList(func); @@ -220,6 +224,7 @@ Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Enviro cleanObject(&funcList); return arg1; } + /** * Evaluates a list whose first element is a function, applying that function * @@ -429,6 +434,7 @@ Object reduce(const Object listInitial, const Object func, struct Environment* e } #define CAT_MAX 1024 + Object catObjects(const Object obj1, const Object obj2, struct Environment* env) { Object evalObj1 = eval(&obj1, env); @@ -443,7 +449,7 @@ Object catObjects(const Object obj1, const Object obj2, struct Environment* env) stringObj(str2, &evalObj2); cleanObject(&evalObj1); cleanObject(&evalObj2); - int length = strlen(str1) + strlen(str2) + 1; + size_t length = strlen(str1) + strlen(str2) + 1; Object o = newObject(TYPE_STRING); o.string = calloc(sizeof(char), length); @@ -501,9 +507,9 @@ Object _basicOp(const Object* obj1, const Object* obj2, const char op, return boolObject(n1 > n2); case '<': return boolObject(n1 < n2); + default: + return *obj1; } - - return *obj1; } Object basicOp(const Object* obj1, const Object* obj2, const char op, @@ -922,17 +928,17 @@ Result parseAtom(struct Slice* s) } else if (c == '0' && s->text[1] == 'x') { return (Result) {parseHex(s), s}; } else if (c == '0' && s->text[1] == 'b') { - return (Result) { parseBin(s), s }; + return (Result) {parseBin(s), s}; #endif } else { - return (Result) { errorObject(UNSUPPORTED_NUMBER_TYPE), s}; + return (Result) {errorObject(UNSUPPORTED_NUMBER_TYPE), s}; } } else if (s->length == 1 && (c == 'T' || c == 't')) { - return (Result) { boolObject(1), s}; + return (Result) {boolObject(1), s}; } else if (s->length == 1 && (c == 'F' || c == 'f')) { - return (Result) { boolObject(0), s}; + return (Result) {boolObject(0), s}; } else if (c == '"'/* || c == '\''*/) { - return (Result) { objFromSlice(s->text, s->length), s}; + return (Result) {objFromSlice(s->text, s->length), s}; } else { if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') { Object possessiveFunc = newObject(TYPE_FUNC); @@ -943,9 +949,9 @@ Result parseAtom(struct Slice* s) struct Slice* next = s + 3; Object possessed = objFromSlice(&next->text[-1], next->length + 1); nf_addToList(&list, possessed); - return (Result) { list, next }; + return (Result) {list, next}; } - return (Result) { symFromSlice(s->text, s->length), s}; + return (Result) {symFromSlice(s->text, s->length), s}; } } diff --git a/src/pebblisp.h b/src/pebblisp.h index 4d42dbf..1a95238 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -29,12 +29,10 @@ void evalForms(Object* destList, const Object* src, struct Environment* env); void copySlice(char* dest, struct Slice* src); Object evalLambdaArgs(const Object* arg_forms, struct Environment* env); + Object listEvalLambda(Object* lambda, const Object* remaining, struct Environment* env); -// Slices -void copySlice(char* dest, struct Slice* src); - void debugSlice(struct Slice* s); #define BASIC_OP(_name) \ @@ -61,8 +59,7 @@ BASIC_OP(and); BASIC_OP(or); #undef BASIC_OP -Object catObjects(const Object obj1, const Object obj2, - struct Environment* env); +Object catObjects(Object obj1, Object obj2, struct Environment* env); Object filter(Object obj1, Object obj2, struct Environment* env); @@ -109,7 +106,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 addGetRoute(Object path, Object textFunc, struct Environment* env); + Object addPostRoute(Object path, Object textFunc, struct Environment* env); #endif diff --git a/src/tokens.h b/src/tokens.h index 248b0e1..fba01c5 100644 --- a/src/tokens.h +++ b/src/tokens.h @@ -3,11 +3,11 @@ #include "pebblisp.h" -int isSingle(const char c); +int isSingle(char c); -int isDigit(const char c); +int isDigit(char c); -int isHex(const char c); +int isHex(char c); struct Slice* nf_tokenize(const char* input, struct Error* err); diff --git a/src/web.c b/src/web.c index 5ef6885..fb86158 100644 --- a/src/web.c +++ b/src/web.c @@ -14,6 +14,7 @@ #include "web.h" #include "pebblisp.h" +#include "tokens.h" #ifdef _MHD_FLAGS_ENUM typedef enum MHD_Result HttpResult; @@ -46,14 +47,20 @@ int methodMatches(const char* method, struct Route* route) } static HttpResult -print_out_key(void* queryParamsV, enum MHD_ValueKind kind, const char* key, - const char* value) +add_query_param(void* queryParamsV, enum MHD_ValueKind kind, const char* key, + const char* value) { (void) kind; /* Unused. Silent compiler warning. */ Object* queryParams = queryParamsV; Object pair = startList(stringFromSlice(key, strlen(key))); - nf_addToList(&pair, parseEval(value, NULL)); + Object parsed; + if (isDigit(value[0])) { + parsed = parseEval(value, NULL); + } else { + parsed = stringFromSlice(value, strlen(value)); + } + nf_addToList(&pair, parsed); nf_addToList(queryParams, pair); return MHD_YES; @@ -72,7 +79,7 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, for (int i = 0; i < routeCount; i++) { if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) { Object queryParams = listObject(); - MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, print_out_key, &queryParams); + MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND, add_query_param, &queryParams); Object res = structObject(requestDefinition); res.structObject->fields[0] = queryParams;