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,6 +18,7 @@ struct Environment {
};
struct Environment* global();
void setGlobal(struct Environment* env);
Object fetchFromEnvironment(const char* name, struct Environment* env);
@ -25,7 +26,7 @@ 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);

View File

@ -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)
{

View File

@ -94,6 +94,7 @@ struct Error {
struct Object {
Type type;
union {
int number;
Object* list;
@ -159,9 +160,9 @@ int isEmpty(const Object *obj);
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);
@ -177,37 +178,37 @@ void cleanObject(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);
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);
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);
@ -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,7 +235,9 @@ 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();
@ -245,6 +248,7 @@ Object constructLambda(const Object *params, const Object *body, struct Environm
Object len(Object obj1, Object, struct Environment*);
int getAllocations();
int getBytes();
size_t getBytes();
#endif

View File

@ -8,8 +8,11 @@
#include <readline/history.h>
#include "tokens.h"
#ifdef STANDALONE
#include "web.h"
#endif
/**
@ -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,10 +507,10 @@ Object _basicOp(const Object* obj1, const Object* obj2, const char op,
return boolObject(n1 > n2);
case '<':
return boolObject(n1 < n2);
}
default:
return *obj1;
}
}
Object basicOp(const Object* obj1, const Object* obj2, const char op,
struct Environment* env)

View File

@ -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

View File

@ -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);

View File

@ -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,
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;