2020-05-10 13:51:33 -04:00
|
|
|
#ifndef ENVIRONMENT_H
|
|
|
|
#define ENVIRONMENT_H
|
|
|
|
|
|
|
|
#include "object.h"
|
2022-04-04 14:15:02 -04:00
|
|
|
#include "hash.h"
|
|
|
|
|
2022-04-08 11:49:02 -04:00
|
|
|
struct StrippedObject {
|
|
|
|
Type type;
|
|
|
|
void* data;
|
|
|
|
};
|
|
|
|
|
2022-04-04 14:15:02 -04:00
|
|
|
struct EnvElement {
|
|
|
|
char* symbol;
|
2022-04-08 11:49:02 -04:00
|
|
|
struct StrippedObject object;
|
2022-04-04 14:15:02 -04:00
|
|
|
};
|
2020-05-10 13:51:33 -04:00
|
|
|
|
|
|
|
struct Environment;
|
|
|
|
struct Environment {
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Environment* outer;
|
2022-04-04 14:15:02 -04:00
|
|
|
struct ObjectTable table;
|
|
|
|
// struct EnvElement* elements; // iterative
|
|
|
|
// int capacity; // iterative
|
2022-03-14 16:51:45 -04:00
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
int refs;
|
|
|
|
};
|
|
|
|
|
2022-04-08 11:49:02 -04:00
|
|
|
Object deStrip(struct StrippedObject object);
|
|
|
|
|
2022-03-15 21:43:53 -04:00
|
|
|
struct Environment* global();
|
2022-03-19 22:25:20 -04:00
|
|
|
|
|
|
|
void setGlobal(struct Environment* env);
|
2020-05-10 13:51:33 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object fetchFromEnvironment(const char* name, struct Environment* env);
|
|
|
|
|
2022-04-07 01:20:59 -04:00
|
|
|
struct Environment envForLambda(const Object* params, const Object* arguments, int paramCount,
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Environment* outer);
|
|
|
|
|
2022-03-19 22:25:20 -04:00
|
|
|
void addToEnv(struct Environment* env, const char* name, Object obj);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-31 13:23:15 -04:00
|
|
|
void printEnv(struct Environment* env, int printPointers);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
void addFunc(const char* name,
|
2022-03-24 16:56:52 -04:00
|
|
|
Object (* func)(Object*, int, struct Environment*),
|
2022-03-27 00:03:34 -04:00
|
|
|
struct Environment* env,
|
|
|
|
int i);
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
void deleteEnv(struct Environment* e);
|
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
void shredDictionary();
|
|
|
|
|
2020-05-10 13:51:33 -04:00
|
|
|
struct Environment defaultEnv();
|
|
|
|
|
2022-04-04 14:15:02 -04:00
|
|
|
struct Environment defaultEnvPreAllocated(struct EnvElement* elements);
|
2022-04-03 19:17:02 -04:00
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
int getStructIndex(const char* name);
|
|
|
|
|
|
|
|
struct StructDef* getStructAt(int i);
|
|
|
|
|
|
|
|
void addStructDef(struct StructDef def);
|
2022-03-18 11:23:19 -04:00
|
|
|
|
2022-03-25 13:33:26 -04:00
|
|
|
void printColored(const char* code);
|
|
|
|
|
2022-03-25 16:11:20 -04:00
|
|
|
int runTests(int detailed);
|
2022-03-21 12:33:35 -04:00
|
|
|
|
2022-04-04 14:15:02 -04:00
|
|
|
int getTotalSearchDepth();
|
2022-04-07 01:20:59 -04:00
|
|
|
|
2022-04-04 14:15:02 -04:00
|
|
|
int getTotalSearches();
|
|
|
|
|
|
|
|
#define unused __attribute__((unused))
|
|
|
|
|
|
|
|
#define array_length(_array) (sizeof(_array) / sizeof((_array)[0]))
|
|
|
|
|
|
|
|
#define fnn(_name, _docs, ...) \
|
|
|
|
static const char * const _name ## Doc = _docs; \
|
|
|
|
unused 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."); \
|
|
|
|
Object _name(Object* params, int length, struct Environment* env)
|
|
|
|
|
|
|
|
#define fn(_name, _symbol, _docs, ...) \
|
|
|
|
static const char * const _name ## Symbol = _symbol; \
|
|
|
|
fnn(_name, _docs, __VA_ARGS__)
|
|
|
|
|
|
|
|
fn(segfault, "seg",
|
2022-04-07 01:20:59 -04:00
|
|
|
"Induces a segfault."
|
2022-04-04 14:15:02 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn(help, "?",
|
|
|
|
"Gets a string with help text for the given function.\n"
|
|
|
|
"For example:\n"
|
|
|
|
" (? islist) => \"(islist (1 2 3)) => T\""
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2020-05-10 13:51:33 -04:00
|
|
|
#endif
|