General code clean-up.

This commit is contained in:
Sage Vaillancourt 2022-03-19 22:25:20 -04:00
parent 4733e1172e
commit 30e8c87e66
7 changed files with 100 additions and 82 deletions

View File

@ -18,14 +18,15 @@ struct Environment {
}; };
struct Environment* global(); struct Environment* global();
void setGlobal(struct Environment *env);
void setGlobal(struct Environment* env);
Object fetchFromEnvironment(const char* name, struct Environment* env); Object fetchFromEnvironment(const char* name, struct Environment* env);
struct Environment envForLambda(const Object* params, const Object* arg_forms, struct Environment envForLambda(const Object* params, const Object* arg_forms,
struct Environment* outer); 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); void printEnv(struct Environment* env);

View File

@ -6,7 +6,7 @@
size_t bytes = 0; size_t bytes = 0;
int getBytes() size_t getBytes()
{ {
return bytes; 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') { if (obj->error->context && obj->error->context[0] != '\0') {
stringf(dest, len, "'%s': %s", errorText[code], stringf(dest, len, "'%s': %s", errorText[code],
obj->error->context); obj->error->context);
} else { } else if (code >= 0 && code <= INDEX_PAST_END) {
stringf(dest, len, "%s", errorText[code]); stringf(dest, len, "%s", errorText[code]);
} else {
stringf(dest, len, "BROKEN ERROR CODE: %d", code);
} }
#endif #endif
break; break;
@ -430,7 +432,6 @@ void debugObj(const Object* obj)
#ifdef DEBUG #ifdef DEBUG
printObj(obj); printObj(obj);
#endif #endif
return;
} }
#if defined(DEBUG) || defined(STANDALONE) #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; 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) inline Object cloneObject(const Object src)
{ {

View File

@ -89,29 +89,30 @@ struct Slice;
struct Other; struct Other;
struct Error { struct Error {
enum errorCode code; enum errorCode code;
char *context; char* context;
}; };
struct Object { struct Object {
Type type; Type type;
union { union {
int number; int number;
Object *list; Object* list;
char *string; char* string;
Object (*func)(Object, Object, struct Environment *); Object (* func)(Object, Object, struct Environment*);
struct StructObject *structObject; struct StructObject* structObject;
struct Lambda *lambda; struct Lambda* lambda;
struct Other *other; struct Other* other;
#ifdef SIMPLE_ERRORS #ifdef SIMPLE_ERRORS
enum errorCode error; enum errorCode error;
#else #else
struct Error *error; struct Error* error;
#endif #endif
}; };
Object *forward; Object* forward;
}; };
struct StructDef { struct StructDef {
@ -132,88 +133,88 @@ struct Lambda {
}; };
struct Other { 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 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 newObject(Type type);
Object listObject(); 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); Object boolObject(int b);
@ -225,7 +226,7 @@ Object otherObject();
Object errorObject(enum errorCode err); 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); 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) ; #define errorAddContext(x, y, z, a) ;
#else #else
#define errorWithContext(_code, _context) errorWithContextLineNo(_code, _context, __LINE__, __FILE__) #define errorWithContext(_code, _context) errorWithContextLineNo(_code, _context, __LINE__, __FILE__)
void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName); void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName);
#endif #endif
struct Error noError(); 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 version of listLength()
Object len(Object obj1, Object, struct Environment *); Object len(Object obj1, Object, struct Environment*);
int getAllocations(); int getAllocations();
int getBytes();
size_t getBytes();
#endif #endif

View File

@ -8,8 +8,11 @@
#include <readline/history.h> #include <readline/history.h>
#include "tokens.h" #include "tokens.h"
#ifdef STANDALONE #ifdef STANDALONE
#include "web.h" #include "web.h"
#endif #endif
/** /**
@ -47,7 +50,7 @@ Object evalDefArgs(const Object* symbol, const Object* value,
return cloneObject(*symbol); return cloneObject(*symbol);
} }
void printStructDef(const struct StructDef *def) void printStructDef(const struct StructDef* def)
{ {
printf("%s: {\n", def->name); printf("%s: {\n", def->name);
for (int i = 0; i < def->fieldCount; i++) { 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, 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 simpleFuncEval(const Object func, Object arg1, Object arg2, struct Environment* env)
{ {
Object funcList = startList(func); Object funcList = startList(func);
@ -220,6 +224,7 @@ Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Enviro
cleanObject(&funcList); cleanObject(&funcList);
return arg1; return arg1;
} }
/** /**
* Evaluates a list whose first element is a function, applying that function * 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 #define CAT_MAX 1024
Object catObjects(const Object obj1, const Object obj2, struct Environment* env) Object catObjects(const Object obj1, const Object obj2, struct Environment* env)
{ {
Object evalObj1 = eval(&obj1, env); Object evalObj1 = eval(&obj1, env);
@ -443,7 +449,7 @@ Object catObjects(const Object obj1, const Object obj2, struct Environment* env)
stringObj(str2, &evalObj2); stringObj(str2, &evalObj2);
cleanObject(&evalObj1); cleanObject(&evalObj1);
cleanObject(&evalObj2); cleanObject(&evalObj2);
int length = strlen(str1) + strlen(str2) + 1; size_t length = strlen(str1) + strlen(str2) + 1;
Object o = newObject(TYPE_STRING); Object o = newObject(TYPE_STRING);
o.string = calloc(sizeof(char), length); 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); return boolObject(n1 > n2);
case '<': case '<':
return boolObject(n1 < n2); return boolObject(n1 < n2);
} default:
return *obj1; return *obj1;
}
} }
Object basicOp(const Object* obj1, const Object* obj2, const char op, 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') { } else if (c == '0' && s->text[1] == 'x') {
return (Result) {parseHex(s), s}; return (Result) {parseHex(s), s};
} else if (c == '0' && s->text[1] == 'b') { } else if (c == '0' && s->text[1] == 'b') {
return (Result) { parseBin(s), s }; return (Result) {parseBin(s), s};
#endif #endif
} else { } else {
return (Result) { errorObject(UNSUPPORTED_NUMBER_TYPE), s}; return (Result) {errorObject(UNSUPPORTED_NUMBER_TYPE), s};
} }
} else if (s->length == 1 && (c == 'T' || c == 't')) { } 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')) { } else if (s->length == 1 && (c == 'F' || c == 'f')) {
return (Result) { boolObject(0), s}; return (Result) {boolObject(0), s};
} else if (c == '"'/* || c == '\''*/) { } else if (c == '"'/* || c == '\''*/) {
return (Result) { objFromSlice(s->text, s->length), s}; return (Result) {objFromSlice(s->text, s->length), s};
} else { } else {
if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') { if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') {
Object possessiveFunc = newObject(TYPE_FUNC); Object possessiveFunc = newObject(TYPE_FUNC);
@ -943,9 +949,9 @@ Result parseAtom(struct Slice* s)
struct Slice* next = s + 3; struct Slice* next = s + 3;
Object possessed = objFromSlice(&next->text[-1], next->length + 1); Object possessed = objFromSlice(&next->text[-1], next->length + 1);
nf_addToList(&list, possessed); 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};
} }
} }

View File

@ -29,12 +29,10 @@ 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, struct Environment* env); Object evalLambdaArgs(const Object* arg_forms, struct Environment* env);
Object listEvalLambda(Object* lambda, const Object* remaining, Object listEvalLambda(Object* lambda, const Object* remaining,
struct Environment* env); struct Environment* env);
// Slices
void copySlice(char* dest, struct Slice* src);
void debugSlice(struct Slice* s); void debugSlice(struct Slice* s);
#define BASIC_OP(_name) \ #define BASIC_OP(_name) \
@ -61,8 +59,7 @@ BASIC_OP(and);
BASIC_OP(or); BASIC_OP(or);
#undef BASIC_OP #undef BASIC_OP
Object catObjects(const Object obj1, const Object obj2, Object catObjects(Object obj1, Object obj2, struct Environment* env);
struct Environment* env);
Object filter(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 loadFile(Object filename, Object _, struct Environment* env);
Object startServer(Object path, Object textFunc, struct Environment* env); Object startServer(Object path, Object textFunc, struct Environment* env);
Object addGetRoute(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); Object addPostRoute(Object path, Object textFunc, struct Environment* env);
#endif #endif

View File

@ -3,11 +3,11 @@
#include "pebblisp.h" #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); struct Slice* nf_tokenize(const char* input, struct Error* err);

View File

@ -14,6 +14,7 @@
#include "web.h" #include "web.h"
#include "pebblisp.h" #include "pebblisp.h"
#include "tokens.h"
#ifdef _MHD_FLAGS_ENUM #ifdef _MHD_FLAGS_ENUM
typedef enum MHD_Result HttpResult; typedef enum MHD_Result HttpResult;
@ -46,14 +47,20 @@ int methodMatches(const char* method, struct Route* route)
} }
static HttpResult static HttpResult
print_out_key(void* queryParamsV, enum MHD_ValueKind kind, const char* key, add_query_param(void* queryParamsV, enum MHD_ValueKind kind, const char* key,
const char* value) const char* value)
{ {
(void) kind; /* Unused. Silent compiler warning. */ (void) kind; /* Unused. Silent compiler warning. */
Object* queryParams = queryParamsV; Object* queryParams = queryParamsV;
Object pair = startList(stringFromSlice(key, strlen(key))); 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); nf_addToList(queryParams, pair);
return MHD_YES; return MHD_YES;
@ -72,7 +79,7 @@ answer_to_connection(void* cls, struct MHD_Connection* connection,
for (int i = 0; i < routeCount; i++) { for (int i = 0; i < routeCount; i++) {
if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) { if (methodMatches(method, &routes[i]) && strcmp(url, routes[i].path) == 0) {
Object queryParams = listObject(); 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); Object res = structObject(requestDefinition);
res.structObject->fields[0] = queryParams; res.structObject->fields[0] = queryParams;