Make pointer-printing in (penv) optional.

Colorize (penv) output.
This commit is contained in:
Sage Vaillancourt 2022-03-31 13:23:15 -04:00 committed by Sage Vaillancourt
parent fb2e1811ff
commit ac04fa1a96
5 changed files with 39 additions and 14 deletions

View File

@ -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");
}
}

View File

@ -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*),

View File

@ -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

View File

@ -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);
}

View File

@ -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,8 +243,8 @@ tfn(getEnvVar, "env",
"(env HOME) => /home/sagevaillancourt"
);
/// () => struct Time
fn(getTime, "time",
tfn(getTime, "time",
({ isStruct }),
"Get a struct of the current time with fields (minute hour sec)."
);