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

View File

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

View File

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