From ac04fa1a9679120ce60ae7432cd16a6373cc0edd Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 31 Mar 2022 13:23:15 -0400 Subject: [PATCH] Make pointer-printing in (penv) optional. Colorize (penv) output. --- src/env.c | 28 +++++++++++++++++++++++----- src/env.h | 2 +- src/pebblisp.h | 4 +--- src/plfunc.c | 6 +++++- src/plfunc.h | 13 +++++++++---- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/env.c b/src/env.c index fc4d980..c82eed1 100644 --- a/src/env.c +++ b/src/env.c @@ -137,7 +137,7 @@ void addToEnv(struct Environment* env, const char* name, const Object obj) addToEnvAt(old_size, env, name, obj); } -void printEnv(struct Environment* env) +void printEnv(struct Environment* env, int printPointers) { if (!env) { printf("NULL env\n"); @@ -145,14 +145,32 @@ void printEnv(struct Environment* env) } printf("env->capacity = %d\n", env->capacity); for (int i = 0; i < env->capacity; i++) { + printf(""); if (env->strings[i] == NULL) { - printf("env[%d]: NULL %p\n", i, env->strings[i]); + printf("[%d]: NULL - End of Environment\n", i); break; } - printf("env[%d]: `%s` %p :: ", i, env->strings[i], env->strings[i]); - printf(""); - printObj(&env->objects[i]); + if (printPointers) { + printf("[%d]: `%s` %p :: ", i, env->strings[i], env->strings[i]); + } else { + printf("[%d]: `%s` :: ", i, env->strings[i]); + } printf(""); + if (env->objects[i].type == TYPE_STRING) { + printf("\""); + } + if (env->objects[i].type == TYPE_FUNC && !printPointers) { + printf("Native"); + } else { + size_t length; + char* s = stringObj(&env->objects[i], &length); + printColored(s); + if (env->objects[i].type == TYPE_STRING) { + printf("\""); + } + free(s); + } + printf("\n"); } } diff --git a/src/env.h b/src/env.h index 93cb070..e92b4b4 100644 --- a/src/env.h +++ b/src/env.h @@ -30,7 +30,7 @@ struct Environment envForLambda(const Object* params, const Object* arg_forms, i void addToEnv(struct Environment* env, const char* name, Object obj); -void printEnv(struct Environment* env); +void printEnv(struct Environment* env, int printPointers); void addFunc(const char* name, Object (* func)(Object*, int, struct Environment*), diff --git a/src/pebblisp.h b/src/pebblisp.h index d57d54c..7cbae66 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -6,8 +6,6 @@ #include "env.h" #include "object.h" -#define static_assert _Static_assert - #define array_length(_array) (sizeof(_array) / sizeof((_array)[0])) #define UNPACK(...) __VA_ARGS__ @@ -15,7 +13,7 @@ #define fnn(_name, _docs, ...) \ static const char * const _name ## Doc = _docs; \ static const char * const _name ## Tests[] = {__VA_ARGS__}; \ -static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings must have exactly one expected result for each test."); \ +_Static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings must have exactly one expected result for each test."); \ Object _name(Object* params, int length, struct Environment* env) // GCC warns without the attribute, even when typeChecks are used diff --git a/src/plfunc.c b/src/plfunc.c index d2737ef..28234bf 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -358,7 +358,11 @@ Object numToChar(Object* params, int length, struct Environment* env) Object printEnvO(Object* params, int length, struct Environment* env) { - printEnv(global()); + int printPointers = 0; + if (length > 0 && params[0].type == TYPE_BOOL) { + printPointers = params[0].number; + } + printEnv(global(), printPointers); return numberObject(0); } diff --git a/src/plfunc.h b/src/plfunc.h index 186e48f..999e339 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -200,7 +200,12 @@ tfn(numToChar, "ch", "(ch 0x21)", "!", ); -fn(printEnvO, "penv", "Prints out the current scoped environment."); +fn(printEnvO, "penv", + "Prints out the current scoped environment.\n" + "(penv) prints a mostly human-readable list of env variables.\n" + "(penv T) prints a mostly list of env variables, including pointer addresses.\n" + "Calling (penv) with no argument is equivalent to (penv F)" +); tfn(systemCall, "sys", ({ isStringy, isNumber }), @@ -238,9 +243,9 @@ tfn(getEnvVar, "env", "(env HOME) => /home/sagevaillancourt" ); -/// () => struct Time -fn(getTime, "time", - "Get a struct of the current time with fields (minute hour sec)." +tfn(getTime, "time", + ({ isStruct }), + "Get a struct of the current time with fields (minute hour sec)." ); #endif // STANDALONE