Some stringing cleanup and unused code removal.
This commit is contained in:
parent
502b9349fe
commit
5226640fb6
26
src/env.c
26
src/env.c
|
@ -52,7 +52,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
|
|||
}
|
||||
|
||||
struct Environment env = {
|
||||
.name = "lambdaEnv",
|
||||
.outer = outer,
|
||||
.strings = NULL,
|
||||
.objects = NULL,
|
||||
|
@ -82,30 +81,6 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms,
|
|||
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
|
||||
void addToEnv(struct Environment* env, const char* name, const Object obj)
|
||||
{
|
||||
|
@ -401,7 +376,6 @@ struct Environment defaultEnv()
|
|||
char size = MAX_ENV_ELM;
|
||||
|
||||
struct Environment e = {
|
||||
.name = "defaultEnv",
|
||||
.outer = NULL,
|
||||
.strings = strings,
|
||||
.objects = objects,
|
||||
|
|
|
@ -15,7 +15,6 @@ struct Environment {
|
|||
struct StructDef* structDefs;
|
||||
|
||||
int refs;
|
||||
const char* name;
|
||||
};
|
||||
|
||||
struct Environment* global();
|
||||
|
|
195
src/object.c
195
src/object.c
|
@ -155,59 +155,6 @@ void allocObject(Object** spot, const Object src)
|
|||
(*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
|
||||
* 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
|
||||
static const char* errorText[] = {"MISMATCHED_PARENS",
|
||||
"BAD_LIST_OF_SYMBOL_STRINGS",
|
||||
"TYPE_LIST_NOT_CAUGHT",
|
||||
"NULL_ENV",
|
||||
"EMPTY_ENV",
|
||||
"BUILT_IN_NOT_FOUND",
|
||||
|
@ -241,7 +186,6 @@ static const char* errorText[] = {"MISMATCHED_PARENS",
|
|||
"LAMBDA_ARGS_NOT_LIST",
|
||||
"DID_NOT_FIND_SYMBOL",
|
||||
"BAD_TYPE",
|
||||
"UNEXPECTED_FORM",
|
||||
"LISTS_NOT_SAME_SIZE",
|
||||
"BAD_NUMBER",
|
||||
"UNSUPPORTED_NUMBER_TYPE",
|
||||
|
@ -267,21 +211,17 @@ int stringList(char* dest, const Object* obj)
|
|||
if (!dest || !isListy(*obj)) {
|
||||
return 0;
|
||||
}
|
||||
const char* initial = dest;
|
||||
|
||||
dest[0] = '(';
|
||||
dest[1] = '\0';
|
||||
int length = 1;
|
||||
dest += sprintf(dest, "(");
|
||||
|
||||
FOR_POINTER_IN_LIST(obj) {
|
||||
strcat(dest, " ");
|
||||
length += 1;
|
||||
char tok[90] = "";
|
||||
length += stringObj(tok, POINTER);
|
||||
strcat(dest, tok);
|
||||
stringObj(tok, POINTER);
|
||||
dest += sprintf(dest, " %s", tok);
|
||||
}
|
||||
strcat(dest, " )");
|
||||
length += 2;
|
||||
return length;
|
||||
dest += sprintf(dest, " )");
|
||||
return dest - initial;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -291,40 +231,28 @@ int stringList(char* dest, const Object* obj)
|
|||
int stringStruct(char* dest, const Object* obj)
|
||||
{
|
||||
struct StructObject* so = obj->structObject;
|
||||
dest[0] = '{';
|
||||
dest[1] = '\0';
|
||||
int length = 1;
|
||||
const char* initial = dest;
|
||||
dest += sprintf(dest, "{");
|
||||
|
||||
for (int i = 0; i < global()->structDefs[so->definition].fieldCount; i++) {
|
||||
strcat(dest, " ");
|
||||
strcat(dest, global()->structDefs[so->definition].names[i]);
|
||||
strcat(dest, ": ");
|
||||
length += 3;
|
||||
dest += sprintf(dest, " %s: ", global()->structDefs[so->definition].names[i]);
|
||||
int isString = so->fields[i].type == TYPE_STRING;
|
||||
if (isString) {
|
||||
strcat(dest, "\"");
|
||||
dest += sprintf(dest, "\"");
|
||||
}
|
||||
|
||||
char tok[90] = "";
|
||||
length += stringObj(tok, &so->fields[i]);
|
||||
strcat(dest, tok);
|
||||
stringObj(tok, &so->fields[i]);
|
||||
dest += sprintf(dest, "%s", tok);
|
||||
|
||||
if (isString) {
|
||||
strcat(dest, "\"");
|
||||
length += 1;
|
||||
dest += sprintf(dest, "\"");
|
||||
}
|
||||
strcat(dest, ",");
|
||||
length += 1;
|
||||
dest += sprintf(dest, ",");
|
||||
}
|
||||
int i = 0;
|
||||
while (dest[i]) {
|
||||
i++;
|
||||
}
|
||||
dest[i - 1] = ' ';
|
||||
dest[i + 0] = '}';
|
||||
length += 2;
|
||||
dest[i + 1] = '\0';
|
||||
return length;
|
||||
dest--;
|
||||
dest += sprintf(dest, " }");
|
||||
return dest - initial;
|
||||
}
|
||||
/**
|
||||
* 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 size = 0;
|
||||
const char* initial = dest;
|
||||
if (!dest || !obj) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (obj->type) {
|
||||
case TYPE_NUMBER:
|
||||
size += stringf(dest, len, "%d", obj->number);
|
||||
dest += stringf(dest, len, "%d", obj->number);
|
||||
break;
|
||||
case TYPE_BOOL:
|
||||
size += stringf(dest, len, "%s", obj->number ? "T" : "F");
|
||||
dest += stringf(dest, len, "%s", obj->number ? "T" : "F");
|
||||
break;
|
||||
case TYPE_STRING:
|
||||
size += stringf(dest, len, "%s", obj->string);
|
||||
dest += stringf(dest, len, "%s", obj->string);
|
||||
break;
|
||||
case TYPE_SYMBOL:
|
||||
size += stringf(dest, len, "`%s`", obj->string);
|
||||
dest += stringf(dest, len, "`%s`", obj->string);
|
||||
break;
|
||||
case TYPE_STRUCT:
|
||||
//snprintf(dest, len, "{%s}", obj->structObject->definition->names[0]);
|
||||
size += stringStruct(dest, obj);
|
||||
dest += stringStruct(dest, obj);
|
||||
break;
|
||||
case TYPE_LIST:
|
||||
case TYPE_SLIST:
|
||||
size += stringList(dest, obj);
|
||||
dest += stringList(dest, obj);
|
||||
break;
|
||||
case TYPE_ERROR: {
|
||||
int code = getErrorCode(*obj);
|
||||
#ifdef SIMPLE_ERRORS
|
||||
size += stringf(dest, len, "E[%d]", (int)code);
|
||||
dest += stringf(dest, len, "E[%d]", (int)code);
|
||||
#else
|
||||
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);
|
||||
} else if (code >= 0 && code <= INDEX_PAST_END) {
|
||||
size += stringf(dest, len, "%s", errorText[code]);
|
||||
dest += stringf(dest, len, "%s", errorText[code]);
|
||||
} else {
|
||||
size += stringf(dest, len, "BROKEN ERROR CODE: %d", code);
|
||||
dest += stringf(dest, len, "BROKEN ERROR CODE: %d", code);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case TYPE_FUNC:
|
||||
size += stringf(dest, len, "F%d", obj->number);
|
||||
dest += stringf(dest, len, "F%d", obj->number);
|
||||
break;
|
||||
case TYPE_LAMBDA: {
|
||||
#ifdef STANDALONE
|
||||
int n = stringf(dest, len, "\\x%d", obj->number);
|
||||
dest += n;
|
||||
size += n;
|
||||
n = stringNObj(dest, &obj->lambda->params, len);
|
||||
dest += n;
|
||||
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;
|
||||
dest += stringf(dest, len, "\\x%d", obj->number);
|
||||
dest += stringNObj(dest, &obj->lambda->params, len);
|
||||
dest += sprintf(dest, " -> ");
|
||||
dest += stringNObj(dest, &obj->lambda->body, len);
|
||||
dest += sprintf(dest, ">");
|
||||
#else
|
||||
size += stringf(dest, len, "\\x%d", obj->number);
|
||||
dest += stringf(dest, len, "\\x%d", obj->number);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case TYPE_OTHER:
|
||||
size += stringf(dest, len, "%p", obj->other->data);
|
||||
dest += stringf(dest, len, "%p", obj->other->data);
|
||||
break;
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
@ -632,10 +541,6 @@ void printAndClean(Object* target)
|
|||
*/
|
||||
void deleteList(Object* dest)
|
||||
{
|
||||
if (!dest) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isListy(*dest)) {
|
||||
printf("Tried to delete something other than a list\n");
|
||||
return;
|
||||
|
@ -821,10 +726,11 @@ inline Object cloneObject(const Object src)
|
|||
return cloneString(src);
|
||||
case TYPE_ERROR:
|
||||
return errorWithContext(getErrorCode(src), src.error->context);
|
||||
case TYPE_OTHER:
|
||||
return cloneObject(src);
|
||||
case TYPE_BOOL:
|
||||
case TYPE_NUMBER:
|
||||
case TYPE_FUNC:
|
||||
case TYPE_OTHER:
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// printf("o: %p\n", o);
|
||||
// printf("o->error: %s\n", o->error);
|
||||
o->error->context = calloc(sizeof(char), RESULT_LENGTH);
|
||||
// printf("context: %p\n", context);
|
||||
sprintf(o->error->context, "%s [33m%s:%d[0m", context, fileName, lineNo);
|
||||
}
|
||||
|
||||
|
@ -996,11 +889,3 @@ struct Error noError()
|
|||
err.context = NULL;
|
||||
return err;
|
||||
}
|
||||
|
||||
inline Object toBool(const Object test)
|
||||
{
|
||||
if (test.number == 0) {
|
||||
return boolObject(0);
|
||||
}
|
||||
return boolObject(1);
|
||||
}
|
||||
|
|
15
src/object.h
15
src/object.h
|
@ -42,8 +42,6 @@
|
|||
|
||||
enum errorCode {
|
||||
MISMATCHED_PARENS,
|
||||
BAD_LIST_OF_SYMBOL_STRINGS,
|
||||
TYPE_LIST_NOT_CAUGHT,
|
||||
NULL_ENV,
|
||||
EMPTY_ENV,
|
||||
BUILT_IN_NOT_FOUND,
|
||||
|
@ -53,7 +51,6 @@ enum errorCode {
|
|||
LAMBDA_ARGS_NOT_LIST,
|
||||
DID_NOT_FIND_SYMBOL,
|
||||
BAD_TYPE,
|
||||
UNEXPECTED_FORM,
|
||||
LISTS_NOT_SAME_SIZE,
|
||||
BAD_NUMBER,
|
||||
UNSUPPORTED_NUMBER_TYPE,
|
||||
|
@ -144,8 +141,6 @@ int stringNObj(char* dest, const Object* obj, size_t len);
|
|||
|
||||
int stringObj(char* dest, const Object* obj);
|
||||
|
||||
void printList(const Object* list);
|
||||
|
||||
void printType(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 printErr(const Object* obj);
|
||||
|
||||
int isEmpty(const Object* obj);
|
||||
|
||||
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 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 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);
|
||||
|
||||
Object cloneList(Object src);
|
||||
|
|
|
@ -58,15 +58,6 @@ Object evalDefArgs(const Object* symbol, const Object* value,
|
|||
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.
|
||||
*
|
||||
|
@ -107,7 +98,6 @@ Object evalStructArgs(const Object* symbol, const Object* fields, struct Environ
|
|||
int prevCapacity = env->structCapacity;
|
||||
env->structCapacity *= 2;
|
||||
env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity);
|
||||
printf("Can malloc\n");
|
||||
for (int i = 0; i < prevCapacity; 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,
|
||||
const int length, struct Environment* env)
|
||||
{
|
||||
//printf("Using new funcEval.\n");
|
||||
|
||||
//printf("list: ");
|
||||
//printObj(list);
|
||||
//printf("\nfunction:");
|
||||
//printObj(function);
|
||||
//printf("\n");
|
||||
Object rest[length];
|
||||
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);
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
@ -408,25 +386,6 @@ void copySlice(char* dest, struct Slice* src)
|
|||
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 structo = params[0];
|
||||
|
@ -693,7 +652,7 @@ int _readFile(FILE* input, struct Environment* env)
|
|||
if (line[i] == ';') {
|
||||
break;
|
||||
} else {
|
||||
int j = 0;
|
||||
int j;
|
||||
for (j = i; j < LINE_MAX; j++) {
|
||||
if (line[j] == '"') {
|
||||
isQuote = !isQuote;
|
||||
|
|
|
@ -58,8 +58,6 @@ Object evalLambdaArgs(const Object* arg_forms, struct Environment* env);
|
|||
Object listEvalLambda(Object* lambda, const Object* remaining,
|
||||
struct Environment* env);
|
||||
|
||||
void debugSlice(struct Slice* s);
|
||||
|
||||
Object simpleFuncEval(Object func, Object arg1, Object arg2, struct Environment* env);
|
||||
|
||||
#ifdef STANDALONE
|
||||
|
|
|
@ -43,11 +43,6 @@ int isWhitespace(const char c)
|
|||
return c == ' ' || c == '\t' || c == '\n';
|
||||
}
|
||||
|
||||
int notWhitespace(const char c)
|
||||
{
|
||||
return !isWhitespace(c);
|
||||
}
|
||||
|
||||
struct Slice* nf_tokenize(const char* input, struct Error* err)
|
||||
{
|
||||
if (!input) {
|
||||
|
|
Loading…
Reference in New Issue