Fix no-param lambda environment bug.

Put allocation cap behind an ifdef
This commit is contained in:
Sage Vaillancourt 2022-03-19 21:28:17 -04:00
parent 9da4649a27
commit c9be701b19
3 changed files with 45 additions and 42 deletions

View File

@ -21,15 +21,13 @@ Object fetchFromEnvironment(const char* name, struct Environment* env)
} }
for (int i = 0; i < env->size; i++) { for (int i = 0; i < env->size; i++) {
if (env->strings[i] == NULL) { if (env->strings[i] == NULL) { printd("Try %d (NULL)\n", i);
printd("Try %d (NULL)\n", i);
break; break;
} }
printd("Try %d (%s)\n", i, env->strings[i]); printd("Try %d (%s)\n", i, env->strings[i]);
debugObj(&env->objects[i]); debugObj(&env->objects[i]);
if (strcmp(name, env->strings[i]) == 0) { if (strcmp(name, env->strings[i]) == 0) { printd("Returning!\n");
printd("Returning!\n");
return cloneObject(env->objects[i]); return cloneObject(env->objects[i]);
} }
} }
@ -47,6 +45,10 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
{ {
int paramCount = listLength(params); int paramCount = listLength(params);
if (outer) {
outer->refs += 1;
}
struct Environment env = { struct Environment env = {
.outer = outer, .outer = outer,
.strings = NULL, .strings = NULL,
@ -58,7 +60,7 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
}; };
if (paramCount == 0) { if (paramCount == 0) {
return env; return outer ? *outer : env;
} }
env.strings = calloc(sizeof(char*), paramCount + 1); env.strings = calloc(sizeof(char*), paramCount + 1);
@ -74,9 +76,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
march = march->forward; march = march->forward;
} }
if(outer) {
outer->refs += 1;
}
return env; return env;
} }
@ -225,12 +224,13 @@ struct symFunc {
}; };
struct Environment* _global; struct Environment* _global;
struct Environment* global() struct Environment* global()
{ {
return _global; return _global;
} }
void setGlobal(struct Environment *env) void setGlobal(struct Environment* env)
{ {
_global = env; _global = env;
} }

View File

@ -5,6 +5,7 @@
#include <string.h> #include <string.h>
size_t bytes = 0; size_t bytes = 0;
int getBytes() int getBytes()
{ {
return bytes; return bytes;
@ -135,6 +136,7 @@ inline int isEmpty(const Object* obj)
} }
int allocations = 0; int allocations = 0;
int getAllocations() int getAllocations()
{ {
return allocations; return allocations;
@ -148,11 +150,13 @@ int getAllocations()
*/ */
void allocObject(Object** spot, const Object src) void allocObject(Object** spot, const Object src)
{ {
#ifdef ALLOCATION_CAP
if (allocations >= 10000) { if (allocations >= 10000) {
printf("MAX ALLOCATIONS EXCEEDED\n"); printf("MAX ALLOCATIONS EXCEEDED\n");
*spot = NULL; *spot = NULL;
return; return;
} }
#endif
allocations++; allocations++;
*spot = malloc(sizeof(struct Object)); *spot = malloc(sizeof(struct Object));
**spot = src; **spot = src;
@ -341,6 +345,7 @@ void stringStruct(char* dest, const Object* obj)
#else #else
#define stringf(_dest, _len, _format, args...) snprintf(_dest, _len, _format, ## args) #define stringf(_dest, _len, _format, args...) snprintf(_dest, _len, _format, ## args)
#endif #endif
char* stringNObj(char* dest, const Object* obj, const size_t len) char* stringNObj(char* dest, const Object* obj, const size_t len)
{ {
if (!dest || !obj) { if (!dest || !obj) {
@ -375,7 +380,7 @@ char* stringNObj(char* dest, const Object* obj, const size_t len)
#else #else
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 {
stringf(dest, len, "%s", errorText[code]); stringf(dest, len, "%s", errorText[code]);
} }
@ -630,12 +635,10 @@ void deleteList(Object* dest)
void _copyList(Object* dest, const Object* src, int delete) void _copyList(Object* dest, const Object* src, int delete)
{ {
if (!dest || !src) { if (!dest || !src) { printd("NULL\n");
printd("NULL\n");
return; return;
} }
if (!isListy(*dest) || !isListy(*src)) { if (!isListy(*dest) || !isListy(*src)) { printd("NOT A LIST\n");
printd("NOT A LIST\n");
return; return;
} }
@ -873,7 +876,7 @@ inline Object constructLambda(const Object* params, const Object* body, struct E
copyList(&o.lambda->body, body); copyList(&o.lambda->body, body);
if (env) { if (env) {
Object *dest = &o.lambda->body; Object* dest = &o.lambda->body;
FOR_POINTER_IN_LIST(dest) { FOR_POINTER_IN_LIST(dest) {
if (POINTER->type == TYPE_SYMBOL) { if (POINTER->type == TYPE_SYMBOL) {
Object fetched = fetchFromEnvironment(POINTER->string, env); Object fetched = fetchFromEnvironment(POINTER->string, env);

View File

@ -26,27 +26,28 @@ int requestDefinition = -1;
int routeCount = 0; int routeCount = 0;
struct Route routes[10]; struct Route routes[10];
int addRoute(struct Route route) { int addRoute(struct Route route)
routes[routeCount] = route; {
routeCount += 1; routes[routeCount] = route;
return 0; routeCount += 1;
return 0;
} }
int methodMatches(const char* method, struct Route* route) int methodMatches(const char* method, struct Route* route)
{ {
switch (route->type) { switch (route->type) {
case GET: case GET:
return method[0] == 'G' || method[0] == 'g'; return method[0] == 'G' || method[0] == 'g';
case POST: case POST:
return method[0] == 'P' || method[0] == 'p'; return method[0] == 'P' || method[0] == 'p';
default: default:
return 0; return 0;
} }
} }
static HttpResult static HttpResult
print_out_key(void *queryParamsV, enum MHD_ValueKind kind, const char *key, print_out_key(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;
@ -59,12 +60,12 @@ print_out_key(void *queryParamsV, enum MHD_ValueKind kind, const char *key,
} }
static HttpResult static HttpResult
answer_to_connection(void *cls, struct MHD_Connection *connection, answer_to_connection(void* cls, struct MHD_Connection* connection,
const char *url, const char *method, const char* url, const char* method,
const char *version, const char *upload_data, const char* version, const char* upload_data,
size_t *upload_data_size, void **con_cls) size_t* upload_data_size, void** con_cls)
{ {
char *page = NULL; char* page = NULL;
printf("%s URL: '%s' :: ", method, url); printf("%s URL: '%s' :: ", method, url);
printf("version: %s\n", version); printf("version: %s\n", version);
printf("upload_data: %s\n", upload_data); printf("upload_data: %s\n", upload_data);
@ -89,8 +90,6 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
page = "<html><body><h1>404, Dumbass.</h1></body></html>"; page = "<html><body><h1>404, Dumbass.</h1></body></html>";
} }
struct MHD_Response *response;
HttpResult ret;
(void) cls; /* Unused. Silent compiler warning. */ (void) cls; /* Unused. Silent compiler warning. */
(void) url; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */
(void) method; /* Unused. Silent compiler warning. */ (void) method; /* Unused. Silent compiler warning. */
@ -99,10 +98,9 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
(void) upload_data_size; /* Unused. Silent compiler warning. */ (void) upload_data_size; /* Unused. Silent compiler warning. */
(void) con_cls; /* Unused. Silent compiler warning. */ (void) con_cls; /* Unused. Silent compiler warning. */
response = struct MHD_Response* response = MHD_create_response_from_buffer(
MHD_create_response_from_buffer(strlen(page), (void *) page, strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT);
MHD_RESPMEM_PERSISTENT); HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response); MHD_destroy_response(response);
// if (needsFree) { // if (needsFree) {
@ -113,6 +111,7 @@ answer_to_connection(void *cls, struct MHD_Connection *connection,
} }
int initialized = 0; int initialized = 0;
void initialize() void initialize()
{ {
if (!initialized) { if (!initialized) {
@ -126,12 +125,13 @@ void initialize()
int start(int port) int start(int port)
{ {
initialize(); initialize();
struct MHD_Daemon *daemon = MHD_start_daemon( struct MHD_Daemon* daemon = MHD_start_daemon(
MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END); port, NULL, NULL, &answer_to_connection, NULL, MHD_OPTION_END);
if (NULL == daemon) if (NULL == daemon) {
return 1; return 1;
}
// (void) getchar(); // (void) getchar();