Some stringing cleanup and unused code removal.

This commit is contained in:
Sage Vaillancourt 2022-03-26 12:54:38 -04:00
parent 502b9349fe
commit 5226640fb6
8 changed files with 44 additions and 252 deletions

View File

@ -52,7 +52,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
} }
struct Environment env = { struct Environment env = {
.name = "lambdaEnv",
.outer = outer, .outer = outer,
.strings = NULL, .strings = NULL,
.objects = NULL, .objects = NULL,
@ -82,30 +81,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
return env; return env;
} }
// Determine if a given symbol exists in the given environment chain
//
// Modifies `env` to the environment that the symbol was found in, only if the
// symbol was found
//
// Returns the index of the symbol in the environment, if found. Otherwise, -1
int findInEnv(struct Environment** env, const char* name)
{
struct Environment* temp_env = *env;
while (temp_env) {
for (int i = 0; i < temp_env->size; i++) {
if (temp_env->strings[i] == NULL) {
break;
}
if (strcmp(temp_env->strings[i], name) == 0) {
*env = temp_env;
return i;
}
}
temp_env = temp_env->outer;
}
return -1;
}
// TODO Maybe extend environment using a new outer, instead of realloc // TODO Maybe extend environment using a new outer, instead of realloc
void addToEnv(struct Environment* env, const char* name, const Object obj) void addToEnv(struct Environment* env, const char* name, const Object obj)
{ {
@ -401,7 +376,6 @@ struct Environment defaultEnv()
char size = MAX_ENV_ELM; char size = MAX_ENV_ELM;
struct Environment e = { struct Environment e = {
.name = "defaultEnv",
.outer = NULL, .outer = NULL,
.strings = strings, .strings = strings,
.objects = objects, .objects = objects,

View File

@ -15,7 +15,6 @@ struct Environment {
struct StructDef* structDefs; struct StructDef* structDefs;
int refs; int refs;
const char* name;
}; };
struct Environment* global(); struct Environment* global();

View File

@ -155,59 +155,6 @@ void allocObject(Object** spot, const Object src)
(*spot)->forward = NULL; (*spot)->forward = NULL;
} }
/**
* Inserts a given Object into a list at the given index.
* Immediately returns if `dest` is NULL or not a list type
* Adds to the end of the list if the index is higher than the list length
* @param dest The list to insert into
* @param ind The index to insert at
* @param src The Object to add to the list
* @warning UNTESTED
*/
void insertIntoList(Object* dest, int ind, const Object src)
{
if (!dest || !isListy(*dest)) {
return;
}
// Merely append, when possible
if (ind >= listLength(dest)) {
nf_addToList(dest, src);
return;
}
// The Objects to preceed and follow the new one
Object* beforeNew = itemAt(dest, ind - 1);
Object* afterNew = beforeNew->forward;
// Replace the `before` Object's pointer
allocObject(&beforeNew->forward, src);
beforeNew->forward->forward = afterNew;
}
/**
* Replace an Object in a list at a given index with a given Object
* Attempts to clean the replaced Object before adding the new one
* Immediately returns if `list` is NULL or not a list type
*
* @param list The list Object to replace an element of
* @param i The index of the element to replace
* @param src The Object to copy into the list
*/
void replaceListing(Object* list, int i, const Object src)
{
if (!list || isListy(*list)) {
return;
}
Object* replace = itemAt(list, i);
Object* oldForward = replace->forward;
cleanObject(replace);
*replace = src;
replace->forward = oldForward;
}
/** /**
* Adds an Object to the end of a list * Adds an Object to the end of a list
* Does nothing if `dest` is NULL or not a list type * Does nothing if `dest` is NULL or not a list type
@ -230,8 +177,6 @@ void nf_addToList(Object* dest, const Object src)
#ifndef SIMPLE_ERRORS #ifndef SIMPLE_ERRORS
static const char* errorText[] = {"MISMATCHED_PARENS", static const char* errorText[] = {"MISMATCHED_PARENS",
"BAD_LIST_OF_SYMBOL_STRINGS",
"TYPE_LIST_NOT_CAUGHT",
"NULL_ENV", "NULL_ENV",
"EMPTY_ENV", "EMPTY_ENV",
"BUILT_IN_NOT_FOUND", "BUILT_IN_NOT_FOUND",
@ -241,7 +186,6 @@ static const char* errorText[] = {"MISMATCHED_PARENS",
"LAMBDA_ARGS_NOT_LIST", "LAMBDA_ARGS_NOT_LIST",
"DID_NOT_FIND_SYMBOL", "DID_NOT_FIND_SYMBOL",
"BAD_TYPE", "BAD_TYPE",
"UNEXPECTED_FORM",
"LISTS_NOT_SAME_SIZE", "LISTS_NOT_SAME_SIZE",
"BAD_NUMBER", "BAD_NUMBER",
"UNSUPPORTED_NUMBER_TYPE", "UNSUPPORTED_NUMBER_TYPE",
@ -267,21 +211,17 @@ int stringList(char* dest, const Object* obj)
if (!dest || !isListy(*obj)) { if (!dest || !isListy(*obj)) {
return 0; return 0;
} }
const char* initial = dest;
dest[0] = '('; dest += sprintf(dest, "(");
dest[1] = '\0';
int length = 1;
FOR_POINTER_IN_LIST(obj) { FOR_POINTER_IN_LIST(obj) {
strcat(dest, " ");
length += 1;
char tok[90] = ""; char tok[90] = "";
length += stringObj(tok, POINTER); stringObj(tok, POINTER);
strcat(dest, tok); dest += sprintf(dest, " %s", tok);
} }
strcat(dest, " )"); dest += sprintf(dest, " )");
length += 2; return dest - initial;
return length;
} }
/** /**
@ -291,40 +231,28 @@ int stringList(char* dest, const Object* obj)
int stringStruct(char* dest, const Object* obj) int stringStruct(char* dest, const Object* obj)
{ {
struct StructObject* so = obj->structObject; struct StructObject* so = obj->structObject;
dest[0] = '{'; const char* initial = dest;
dest[1] = '\0'; dest += sprintf(dest, "{");
int length = 1;
for (int i = 0; i < global()->structDefs[so->definition].fieldCount; i++) { for (int i = 0; i < global()->structDefs[so->definition].fieldCount; i++) {
strcat(dest, " "); dest += sprintf(dest, " %s: ", global()->structDefs[so->definition].names[i]);
strcat(dest, global()->structDefs[so->definition].names[i]);
strcat(dest, ": ");
length += 3;
int isString = so->fields[i].type == TYPE_STRING; int isString = so->fields[i].type == TYPE_STRING;
if (isString) { if (isString) {
strcat(dest, "\""); dest += sprintf(dest, "\"");
} }
char tok[90] = ""; char tok[90] = "";
length += stringObj(tok, &so->fields[i]); stringObj(tok, &so->fields[i]);
strcat(dest, tok); dest += sprintf(dest, "%s", tok);
if (isString) { if (isString) {
strcat(dest, "\""); dest += sprintf(dest, "\"");
length += 1;
} }
strcat(dest, ","); dest += sprintf(dest, ",");
length += 1;
} }
int i = 0; dest--;
while (dest[i]) { dest += sprintf(dest, " }");
i++; return dest - initial;
}
dest[i - 1] = ' ';
dest[i + 0] = '}';
length += 2;
dest[i + 1] = '\0';
return length;
} }
/** /**
* Creates a string from a given Object * Creates a string from a given Object
@ -350,83 +278,73 @@ int stringStruct(char* dest, const Object* obj)
int stringNObj(char* dest, const Object* obj, const size_t len) int stringNObj(char* dest, const Object* obj, const size_t len)
{ {
int size = 0; const char* initial = dest;
if (!dest || !obj) { if (!dest || !obj) {
return 0; return 0;
} }
switch (obj->type) { switch (obj->type) {
case TYPE_NUMBER: case TYPE_NUMBER:
size += stringf(dest, len, "%d", obj->number); dest += stringf(dest, len, "%d", obj->number);
break; break;
case TYPE_BOOL: case TYPE_BOOL:
size += stringf(dest, len, "%s", obj->number ? "T" : "F"); dest += stringf(dest, len, "%s", obj->number ? "T" : "F");
break; break;
case TYPE_STRING: case TYPE_STRING:
size += stringf(dest, len, "%s", obj->string); dest += stringf(dest, len, "%s", obj->string);
break; break;
case TYPE_SYMBOL: case TYPE_SYMBOL:
size += stringf(dest, len, "`%s`", obj->string); dest += stringf(dest, len, "`%s`", obj->string);
break; break;
case TYPE_STRUCT: case TYPE_STRUCT:
//snprintf(dest, len, "{%s}", obj->structObject->definition->names[0]); //snprintf(dest, len, "{%s}", obj->structObject->definition->names[0]);
size += stringStruct(dest, obj); dest += stringStruct(dest, obj);
break; break;
case TYPE_LIST: case TYPE_LIST:
case TYPE_SLIST: case TYPE_SLIST:
size += stringList(dest, obj); dest += stringList(dest, obj);
break; break;
case TYPE_ERROR: { case TYPE_ERROR: {
int code = getErrorCode(*obj); int code = getErrorCode(*obj);
#ifdef SIMPLE_ERRORS #ifdef SIMPLE_ERRORS
size += stringf(dest, len, "E[%d]", (int)code); dest += stringf(dest, len, "E[%d]", (int)code);
#else #else
if (obj->error->context && obj->error->context[0] != '\0') { if (obj->error->context && obj->error->context[0] != '\0') {
size += stringf(dest, len, "'%s': %s", errorText[code], dest += stringf(dest, len, "'%s': %s", errorText[code],
obj->error->context); obj->error->context);
} else if (code >= 0 && code <= INDEX_PAST_END) { } else if (code >= 0 && code <= INDEX_PAST_END) {
size += stringf(dest, len, "%s", errorText[code]); dest += stringf(dest, len, "%s", errorText[code]);
} else { } else {
size += stringf(dest, len, "BROKEN ERROR CODE: %d", code); dest += stringf(dest, len, "BROKEN ERROR CODE: %d", code);
} }
#endif #endif
break; break;
} }
case TYPE_FUNC: case TYPE_FUNC:
size += stringf(dest, len, "F%d", obj->number); dest += stringf(dest, len, "F%d", obj->number);
break; break;
case TYPE_LAMBDA: { case TYPE_LAMBDA: {
#ifdef STANDALONE #ifdef STANDALONE
int n = stringf(dest, len, "\\x%d", obj->number); dest += stringf(dest, len, "\\x%d", obj->number);
dest += n; dest += stringNObj(dest, &obj->lambda->params, len);
size += n; dest += sprintf(dest, " -> ");
n = stringNObj(dest, &obj->lambda->params, len); dest += stringNObj(dest, &obj->lambda->body, len);
dest += n; dest += sprintf(dest, ">");
size += n;
strcat(dest, " -> ");
dest += 4;
size += 4;
n = stringNObj(dest, &obj->lambda->body, len);
size += n;
dest += n;
dest += strlen(dest);
strcat(dest, ">");
size += 1;
#else #else
size += stringf(dest, len, "\\x%d", obj->number); dest += stringf(dest, len, "\\x%d", obj->number);
#endif #endif
break; break;
} }
case TYPE_OTHER: case TYPE_OTHER:
size += stringf(dest, len, "%p", obj->other->data); dest += stringf(dest, len, "%p", obj->other->data);
break; break;
} }
if (!isValidType(*obj)) { if (!isValidType(*obj)) {
size += stringf(dest, len, "BAD_TYPE(%d) X%d", obj->type, obj->number); dest += stringf(dest, len, "BAD_TYPE(%d) X%d", obj->type, obj->number);
} }
return size; return dest - initial;
} }
int stringObj(char* dest, const Object* obj) int stringObj(char* dest, const Object* obj)
@ -538,15 +456,6 @@ void nestedPrintList(const Object* list, int newline)
} }
} }
/**
* Prints each Object in a given list, surrounded by parentheses
* @param list The list Object to print
*/
void printList(const Object* list)
{
nestedPrintList(list, 1);
}
#endif #endif
/** /**
@ -632,10 +541,6 @@ void printAndClean(Object* target)
*/ */
void deleteList(Object* dest) void deleteList(Object* dest)
{ {
if (!dest) {
return;
}
if (!isListy(*dest)) { if (!isListy(*dest)) {
printf("Tried to delete something other than a list\n"); printf("Tried to delete something other than a list\n");
return; return;
@ -821,10 +726,11 @@ inline Object cloneObject(const Object src)
return cloneString(src); return cloneString(src);
case TYPE_ERROR: case TYPE_ERROR:
return errorWithContext(getErrorCode(src), src.error->context); return errorWithContext(getErrorCode(src), src.error->context);
case TYPE_OTHER:
return cloneObject(src);
case TYPE_BOOL: case TYPE_BOOL:
case TYPE_NUMBER: case TYPE_NUMBER:
case TYPE_FUNC: case TYPE_FUNC:
case TYPE_OTHER:
return src; return src;
} }
@ -922,16 +828,6 @@ inline int bothAre(const enum Type type, const Object* obj1, const Object* obj2)
return (obj1->type == type) && (obj2->type == type); return (obj1->type == type) && (obj2->type == type);
} }
inline int eitherIs(const enum Type type, const Object* o1, const Object* o2)
{
return (o1->type == type) || (o2->type == type);
}
inline int eitherIsNot(const enum Type type, const Object* o1, const Object* o2)
{
return (o1->type != type) || (o2->type != type);
}
inline int areSameType(const Object* obj1, const Object* obj2) inline int areSameType(const Object* obj1, const Object* obj2)
{ {
return obj1->type == obj2->type; return obj1->type == obj2->type;
@ -972,10 +868,7 @@ inline enum errorCode getErrorCode(const Object obj)
inline void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName) inline void errorAddContext(Object* o, const char* context, int lineNo, const char* fileName)
{ {
// printf("o: %p\n", o);
// printf("o->error: %s\n", o->error);
o->error->context = calloc(sizeof(char), RESULT_LENGTH); o->error->context = calloc(sizeof(char), RESULT_LENGTH);
// printf("context: %p\n", context);
sprintf(o->error->context, "%s %s:%d", context, fileName, lineNo); sprintf(o->error->context, "%s %s:%d", context, fileName, lineNo);
} }
@ -996,11 +889,3 @@ struct Error noError()
err.context = NULL; err.context = NULL;
return err; return err;
} }
inline Object toBool(const Object test)
{
if (test.number == 0) {
return boolObject(0);
}
return boolObject(1);
}

View File

@ -42,8 +42,6 @@
enum errorCode { enum errorCode {
MISMATCHED_PARENS, MISMATCHED_PARENS,
BAD_LIST_OF_SYMBOL_STRINGS,
TYPE_LIST_NOT_CAUGHT,
NULL_ENV, NULL_ENV,
EMPTY_ENV, EMPTY_ENV,
BUILT_IN_NOT_FOUND, BUILT_IN_NOT_FOUND,
@ -53,7 +51,6 @@ enum errorCode {
LAMBDA_ARGS_NOT_LIST, LAMBDA_ARGS_NOT_LIST,
DID_NOT_FIND_SYMBOL, DID_NOT_FIND_SYMBOL,
BAD_TYPE, BAD_TYPE,
UNEXPECTED_FORM,
LISTS_NOT_SAME_SIZE, LISTS_NOT_SAME_SIZE,
BAD_NUMBER, BAD_NUMBER,
UNSUPPORTED_NUMBER_TYPE, UNSUPPORTED_NUMBER_TYPE,
@ -144,8 +141,6 @@ int stringNObj(char* dest, const Object* obj, size_t len);
int stringObj(char* dest, const Object* obj); int stringObj(char* dest, const Object* obj);
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);
@ -154,16 +149,10 @@ void _printObj(const Object* obj, int newline);
void debugObj(const Object* obj); void debugObj(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, 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);
@ -192,10 +181,6 @@ int isError(Object obj, enum errorCode err);
int bothAre(enum Type type, const Object* obj1, const Object* obj2); int bothAre(enum Type type, const Object* obj1, const Object* obj2);
int eitherIsNot(const enum Type type, const Object* o1, const Object* o2);
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(Object src); Object cloneList(Object src);

View File

@ -58,15 +58,6 @@ Object evalDefArgs(const Object* symbol, const Object* value,
return cloneObject(*symbol); return cloneObject(*symbol);
} }
void printStructDef(const struct StructDef* def)
{
printf("%s: {\n", def->name);
for (int i = 0; i < def->fieldCount; i++) {
printf(" %s,\n", def->names[i]);
}
printf("}\n");
}
/** /**
* Add a struct to the environment with a given name and fields. * Add a struct to the environment with a given name and fields.
* *
@ -107,7 +98,6 @@ Object evalStructArgs(const Object* symbol, const Object* fields, struct Environ
int prevCapacity = env->structCapacity; int prevCapacity = env->structCapacity;
env->structCapacity *= 2; env->structCapacity *= 2;
env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity); env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity);
printf("Can malloc\n");
for (int i = 0; i < prevCapacity; i++) { for (int i = 0; i < prevCapacity; i++) {
env->structDefs[i] = prev[i]; env->structDefs[i] = prev[i];
} }
@ -245,20 +235,8 @@ Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Enviro
Object listEvalFunc(const Object* list, const Object* function, Object listEvalFunc(const Object* list, const Object* function,
const int length, struct Environment* env) const int length, struct Environment* env)
{ {
//printf("Using new funcEval.\n");
//printf("list: ");
//printObj(list);
//printf("\nfunction:");
//printObj(function);
//printf("\n");
Object rest[length]; Object rest[length];
evalForms(rest, list->list->forward, env); evalForms(rest, list->list->forward, env);
// for (int i = 0; i < length; i++) {
// printf("rest[%d]: ", i);
// printObj(&rest[i]);
// printf("\n");
// }
Object result = function->func(rest, length, env); Object result = function->func(rest, length, env);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
@ -408,25 +386,6 @@ void copySlice(char* dest, struct Slice* src)
dest[(int) src->length] = '\0'; dest[(int) src->length] = '\0';
} }
void debugSlice(struct Slice* s)
{
#ifdef DEBUG
if (!s) {
printf("NULL SLICE\n");
return;
}
printf("Debug Slice\n text:'");
for (int i = 0; i < s->length; i++) {
printf("%c", s->text[i]);
if (s->text[i] == '\0') {
printf("NULLCHAR\n");
}
}
printf("'\n");
printf(" length: %d\n", s->length);
#endif
}
Object possessive(Object* params, int length, struct Environment* env) Object possessive(Object* params, int length, struct Environment* env)
{ {
Object structo = params[0]; Object structo = params[0];
@ -693,7 +652,7 @@ int _readFile(FILE* input, struct Environment* env)
if (line[i] == ';') { if (line[i] == ';') {
break; break;
} else { } else {
int j = 0; int j;
for (j = i; j < LINE_MAX; j++) { for (j = i; j < LINE_MAX; j++) {
if (line[j] == '"') { if (line[j] == '"') {
isQuote = !isQuote; isQuote = !isQuote;

View File

@ -58,8 +58,6 @@ 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);
void debugSlice(struct Slice* s);
Object simpleFuncEval(Object func, Object arg1, Object arg2, struct Environment* env); Object simpleFuncEval(Object func, Object arg1, Object arg2, struct Environment* env);
#ifdef STANDALONE #ifdef STANDALONE

View File

@ -43,11 +43,6 @@ int isWhitespace(const char c)
return c == ' ' || c == '\t' || c == '\n'; return c == ' ' || c == '\t' || c == '\n';
} }
int notWhitespace(const char c)
{
return !isWhitespace(c);
}
struct Slice* nf_tokenize(const char* input, struct Error* err) struct Slice* nf_tokenize(const char* input, struct Error* err)
{ {
if (!input) { if (!input) {

View File

@ -135,9 +135,6 @@ int start(int port)
return 1; return 1;
} }
// (void) getchar();
// MHD_stop_daemon(daemon);
return 0; return 0;
} }