pebblisp/src/pebblisp.c

722 lines
19 KiB
C
Raw Normal View History

#ifdef STANDALONE
#define _GNU_SOURCE
#include <signal.h>
#include <ucontext.h>
#endif
2016-04-18 04:25:36 -04:00
#include "pebblisp.h"
2016-04-18 04:25:36 -04:00
#include <stdlib.h>
#include <string.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "tokens.h"
#include "plfunc.h"
2022-03-19 22:25:20 -04:00
2022-03-14 23:46:20 -04:00
#ifdef STANDALONE
2022-03-19 22:25:20 -04:00
2022-03-14 23:46:20 -04:00
#include "web.h"
2022-03-19 22:25:20 -04:00
2022-03-14 23:46:20 -04:00
#endif
2020-05-07 20:32:01 -04:00
2020-11-03 09:46:34 -05:00
/**
* Inserts a variable into the environment with a given name and value.
*
* If `argForms` (symbol) and `argForms->forward` (value) are lists of the same
* length, define each symbol element with the corresponding value element.
* I.e. `(def (a b) (5 20))` would store `a` as `5` and `b` as `20`.
*
* @param argForms The symbol(s) and value(s) to define in the environment
* @param env The environment to add the new definition to
* @return The symbol(s) defined
*/
Object evalDefArgs(const Object* symbol, const Object* value,
struct Environment* env)
{
const char* name = symbol->string;
2020-05-04 10:03:35 -04:00
Object finalValue = eval(value, env);
2020-05-04 10:03:35 -04:00
2020-11-03 09:46:34 -05:00
addToEnv(env, name, finalValue);
cleanObject(&finalValue);
2020-05-04 10:03:35 -04:00
return cloneObject(*symbol);
2020-05-04 18:14:41 -04:00
}
2020-05-04 10:03:35 -04:00
/**
* Add a struct to the environment with a given name and fields.
*
* (struct point (x y))
*/
Object evalStructArgs(const Object* symbol, const Object* fields, struct Environment* env)
{
const char* name = symbol->string;
if (!isListy(*fields)) {
return errorObject(NOT_A_LIST);
}
struct StructDef def;
def.name = malloc(sizeof(char) * (strlen(name) + 1));
strcpy(def.name, name);
def.fieldCount = listLength(fields);
def.names = malloc(sizeof(char*) * def.fieldCount);
2022-03-17 16:59:08 -04:00
{
int i = 0;
FOR_POINTER_IN_LIST(fields) {
def.names[i] = malloc(sizeof(char) * (strlen(POINTER->string) + 1));
strcpy(def.names[i], POINTER->string);
i++;
}
}
addStructDef(def);
return boolObject(1);
}
Object evalIfArgs(const Object* argForms, struct Environment* env)
2020-05-04 18:14:41 -04:00
{
2021-07-05 05:08:37 -04:00
Object condition = eval(argForms, env);
Object result = condition.number ? eval(argForms->forward, env)
: eval(argForms->forward->forward, env);
2021-07-05 05:08:37 -04:00
cleanObject(&condition);
return result;
2020-05-04 10:03:35 -04:00
}
Object evalLambdaArgs(const Object* argForms, struct Environment* env)
2020-05-05 13:42:28 -04:00
{
return constructLambda(argForms, argForms ? argForms->forward : NULL, env);
2020-05-05 13:42:28 -04:00
}
Object evalMapArgs(const Object* argForms, struct Environment* env)
2020-05-06 11:58:09 -04:00
{
if (!argForms) {
return errorObject(NULL_MAP_ARGS);
}
Object lambda = eval(argForms, env);
const Object* inputList = argForms->forward;
Object outputList = listObject();
2020-05-07 20:32:01 -04:00
if (lambda.type != TYPE_LAMBDA) {
return errorObject(BAD_TYPE);
}
2020-05-07 20:32:01 -04:00
if (inputList) {
FOR_POINTER_IN_LIST(inputList) {
// Create a new list for each element,
// since lambda evaluation looks for a list
Object tempInput = cloneObject(*POINTER);
Object* params = &lambda.lambda->params;
struct Environment newEnv = envForLambda(params, &tempInput, env);
2020-05-07 20:32:01 -04:00
// Add the lambda evaluation to the list
Object lambda_output = eval(&lambda.lambda->body, &newEnv);
nf_addToList(&outputList, lambda_output);
deleteEnv(&newEnv);
cleanObject(&tempInput);
}
2020-05-07 20:32:01 -04:00
}
cleanObject(&lambda);
2020-05-07 20:32:01 -04:00
return outputList;
2020-05-06 11:58:09 -04:00
}
Object evalBuiltIns(const Object* first, const Object* rest,
struct Environment* env, int* found)
2020-05-04 10:03:35 -04:00
{
*found = 1;
if (strcmp(first->string, "def") == 0) {
return evalDefArgs(rest, rest->forward, env);
#ifndef LOW_MEM
} else if (strcmp(first->string, "defe") == 0) {
Object symbol = eval(rest, env);
Object e = evalDefArgs(&symbol, rest->forward, env);
cleanObject(&symbol);
return e;
#endif
} else if (strcmp(first->string, "if") == 0) {
2020-05-04 10:03:35 -04:00
return evalIfArgs(rest, env);
} else if (strcmp(first->string, "fn") == 0) {
return evalLambdaArgs(rest, env);
} else if (strcmp(first->string, "map") == 0) {
return evalMapArgs(rest, env);
} else if (strcmp(first->string, "struct") == 0) {
return evalStructArgs(rest, rest->forward, env);
2020-05-04 10:03:35 -04:00
}
*found = 0;
return *first;
2020-05-05 13:42:28 -04:00
}
/**
* Evaluates a list whose first element is a function, applying that function
*
* Tries to either apply the function to its parameters, or create a partial
* function, if not enough parameters were passed.
*
* @param list The list being evaluated
* @param function First element of the list, already evaluated to be a function
* @param length Length of `list` - 1, to exclude the already-evaluated element
* @param env The environment to evaluate in
*/
Object listEvalFunc(const Object* list, const Object* function,
const int length, struct Environment* env)
{
Object rest[length];
2022-03-27 00:03:34 -04:00
const Object* start = list->list->forward;
for (int i = 0; i < length; i++) {
rest[i] = eval(start, env);
start = start->forward;
}
Object result = function->func(rest, length, env);
for (int i = 0; i < length; i++) {
cleanObject(&rest[i]);
}
return result;
}
2022-03-27 00:03:34 -04:00
Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Environment* env)
{
Object funcList = startList(func);
nf_addToList(&funcList, arg1);
Object current = cloneObject(arg2);
nf_addToList(&funcList, current);
Object first_eval = eval(funcList.list, env);
arg1 = listEvalFunc(&funcList, &first_eval, 2, env);
cleanObject(&funcList);
return arg1;
}
/**
* Evaluates a list whose first element is a lambda, applying that lambda
*
* Tries to apply the lambda to its parameters. Doesn't attempt partial
* application.
*
* @param lambda First element of the list, already evaluated to be a lambda
* @param remaining The first element after `lambda`
* @param env The environment to evaluate in
*/
Object listEvalLambda(Object* lambda, const Object* remaining,
struct Environment* env)
{
struct Environment newEnv =
envForLambda(&lambda->lambda->params, remaining, env);
Object ret = eval(&lambda->lambda->body, &newEnv);
deleteEnv(&newEnv);
cleanObject(lambda);
Object* t = tail(&ret);
if (t) {
2020-11-04 15:19:08 -05:00
Object o = cloneObject(*t);
cleanObject(&ret);
return o;
2020-05-05 13:42:28 -04:00
}
return ret;
2020-05-04 10:03:35 -04:00
}
/**
* Evaluates a given list, including the application of functions and lambdas
*
* Engages in several behaviors, depending on list contents:
* - () => ()
2020-11-03 14:44:37 -05:00
* - (x y z) => (eval_x eval_y eval_z)
* - (function x y) => evaluated function(x, y)
* - (function ...) => evaluated function(...) applied to each arg
* - (function x) => functionx() partial function
* - (lambda x) => evaluated lambda(x)
*
* @param obj The list to be evaluated
* @param env The environment to evaluate in
*/
Object evalList(const Object* obj, struct Environment* env)
{
const int evalLength = listLength(obj);
if (evalLength == 0) {
return cloneObject(*obj);
}
Object* first_form = obj->list;
if (first_form->type == TYPE_SYMBOL) {
int found;
Object builtIn = evalBuiltIns(first_form, first_form->forward, env, &found);
if (found) {
return builtIn;
}
int i = getStructIndex(first_form->string);
if (i >= 0) {
Object structo = structObject(i);
int s = 0;
FOR_POINTER_IN_LIST(obj) {
if (s != 0) { // Skip the field name
structo.structObject->fields[s - 1] = eval(POINTER, env);
}
s++;
}
return structo;
}
}
// Evaluate the list based on the first element's type
Object first_eval = eval(first_form, env);
switch (first_eval.type) {
case TYPE_FUNC:
// Uses evalLength - 1 because we skip the first form
return listEvalFunc(obj, &first_eval, evalLength - 1, env);
case TYPE_LAMBDA:
return listEvalLambda(&first_eval, first_form->forward, env);
default: { // Return list with each element evaluated
Object list = listObject();
2020-11-04 15:19:08 -05:00
int i = 0;
nf_addToList(&list, first_eval);
FOR_POINTER_IN_LIST(obj) {
if (i != 0) {
2020-11-04 15:19:08 -05:00
nf_addToList(&list, eval(POINTER, env));
}
i++;
}
return list;
}
}
}
Object eval(const Object* obj, struct Environment* env)
2016-04-18 04:25:36 -04:00
{
switch (obj->type) {
case TYPE_LAMBDA:
case TYPE_FUNC:
case TYPE_ERROR:
case TYPE_OTHER:
2016-04-18 04:25:36 -04:00
case TYPE_NUMBER:
2020-05-04 10:03:35 -04:00
case TYPE_BOOL:
case TYPE_STRING:
case TYPE_STRUCT:
case TYPE_SLIST:
return cloneObject(*obj);
2020-05-04 10:03:35 -04:00
case TYPE_SYMBOL:
return fetchFromEnvironment(obj->string, env);
2020-05-04 10:03:35 -04:00
2016-04-18 04:25:36 -04:00
case TYPE_LIST:
return evalList(obj, env);
2016-04-18 04:25:36 -04:00
}
return errorObject(BAD_TYPE);
2016-04-18 04:25:36 -04:00
}
void copySlice(char* dest, struct Slice* src)
{
if (!dest || !src) {
return;
}
strncpy(dest, src->text, src->length);
dest[(int) src->length] = '\0';
}
Object possessive(Object* params, int length, struct Environment* env)
{
Object structo = params[0];
Object field = params[1];
if (structo.type != TYPE_STRUCT) {
printf("`'s` must be used on a struct!\n");
return errorObject(NULL_PARSE);
}
if (!isStringy(field)) {
printf("`'s` field name must be stringy! Received a ");
printType(&field);
printObj(&field);
printf("\n");
return errorObject(NULL_PARSE);
}
struct StructDef* structDef = getStructAt(structo.structObject->definition);
for (int i = 0; i < structDef->fieldCount; i++) {
if (strcmp(field.string, structDef->names[i]) == 0) {
return cloneObject(structo.structObject->fields[i]);
}
}
printf("Could not find field name `%s`\n", field.string);
return errorObject(NULL_PARSE);
}
Result parse(struct Slice* slices)
{
struct Slice* token = slices;
if (token && token->text) {
struct Slice* rest = &slices[1];
if (token->text[0] == '\'' && token->text[1] == '(') {
Result r = readSeq(&slices[2]);
if (r.obj.type == TYPE_LIST) {
r.obj.type = TYPE_SLIST;
}
return r;
} else if (token->text[0] == '(') {
// todo check for null rest
return readSeq(rest);
} else { // todo error on missing close paren
Result r = parseAtom(token);
r.slices = &r.slices[1];
return r;
}
} else {
return (Result) {errorObject(NULL_PARSE), NULL};
}
}
#ifdef SUGAR
#define sugar(_desc, _code) ;
#else
#define sugar(_desc, _code) _code
#endif
Result readSeq(struct Slice* tokens)
{
Object res = listObject();
int isHelp = 0;
for (;;) {
struct Slice* next = tokens;
struct Slice* rest = next->text ? &next[1] : NULL;
if (next->text[0] == ')') {
return (Result) {res, rest};
}
Result r = parse(tokens);
sugar("(? fil) => (? 'fil')",
if (isHelp && r.obj.type == TYPE_SYMBOL) {
r.obj.type = TYPE_STRING;
}
)
if (r.obj.type == TYPE_ERROR) {
return r;
}
nf_addToList(&res, cloneObject(r.obj));
tokens = r.slices;
cleanObject(&r.obj);
isHelp = next->text[0] == '?';
}
}
Object parseDecimal(struct Slice* s)
{
int num = 0;
for (int i = 0; i < s->length; i++) {
if (!isDigit(s->text[i])) {
return errorObject(BAD_NUMBER);
}
num *= 10;
num += s->text[i] - '0';
}
return numberObject(num);
}
Object parseHex(struct Slice* s)
{
int num = 0;
for (int i = 2; i < s->length; i++) {
const char c = s->text[i];
if (!isHex(c)) {
return errorObject(BAD_NUMBER);
}
num *= 16;
if (isDigit(c)) {
num += c - '0';
} else /* is hex */ {
num += c - 'a' + 10;
}
}
return numberObject(num);
}
Object parseBin(struct Slice* s)
{
int num = 0;
for (int i = 2; i < s->length; i++) {
const char c = s->text[i];
if (c != '0' && c != '1') {
return errorObject(BAD_NUMBER);
}
num *= 2;
num += c - '0';
}
return numberObject(num);
}
Result parseAtom(struct Slice* s)
{
const char c = s->text[0];
if (isDigit(c)) {
if (c != '0' || s->length == 1) {
return (Result) {parseDecimal(s), s};
#ifndef LOW_MEM
} else if (s->text[1] == 'x') {
return (Result) {parseHex(s), s};
} else if (s->text[1] == 'b') {
2022-03-19 22:25:20 -04:00
return (Result) {parseBin(s), s};
#endif
} else {
2022-03-19 22:25:20 -04:00
return (Result) {errorObject(UNSUPPORTED_NUMBER_TYPE), s};
}
} else if (s->length == 1 && (c == 'T' || c == 't')) {
2022-03-19 22:25:20 -04:00
return (Result) {boolObject(1), s};
} else if (s->length == 1 && (c == 'F' || c == 'f')) {
2022-03-19 22:25:20 -04:00
return (Result) {boolObject(0), s};
} else if (c == '"'/* || c == '\''*/) {
2022-03-19 22:25:20 -04:00
return (Result) {objFromSlice(s->text, s->length), s};
} else {
if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') {
Object possessiveFunc = newObject(TYPE_FUNC);
possessiveFunc.func = &possessive;
Object list = startList(possessiveFunc);
Object possesser = symFromSlice(s->text, s->length);
nf_addToList(&list, possesser);
struct Slice* next = s + 3;
Object possessed = objFromSlice(&next->text[-1], next->length + 1);
nf_addToList(&list, possessed);
2022-03-19 22:25:20 -04:00
return (Result) {list, next};
}
2022-03-19 22:25:20 -04:00
return (Result) {symFromSlice(s->text, s->length), s};
}
}
struct Slice* lastOpen = NULL;
Object parseEval(const char* input, struct Environment* env)
2016-04-18 04:25:36 -04:00
{
struct Error err = noError();
struct Slice* tokens = nf_tokenize(input, &err);
if (err.context != NULL) {
Object o = errorWithContext(err.code, err.context);
free(err.context);
return o;
}
if (!tokens->text) {
return symFromSlice(" ", 1);
}
#ifdef DEBUG
2020-05-04 18:14:41 -04:00
struct Slice *debug = tokens;
printd("start slice\n");
if (debug) {
while (debug->text) {
char tok[100];
2016-04-18 04:25:36 -04:00
copySlice(tok, debug);
printd("slice: '%s'\n", tok);
2016-04-18 04:25:36 -04:00
debug++;
}
}
#endif
int i = 0;
int parens = 0;
Object obj = numberObject(0);
struct Slice* tok = tokens;
while (tok[i].text != NULL) {
if (tok[i].text[0] == '(') {
lastOpen = &tok[i];
parens++;
} else if (tok[i].text[0] == ')') {
parens--;
}
if (parens == 0) {
cleanObject(&obj);
Object parsed = parse(tok).obj;
if (parsed.type == TYPE_ERROR) {
obj = parsed; // TODO Check necessity
obj.error->plContext = malloc(sizeof(struct Slice));
*obj.error->plContext = *lastOpen;
2021-07-05 05:08:37 -04:00
break;
}
if (tok[i].text[0] == ')') {
// Skip `tok` past end of list that just closed
tok = &tok[i + 1];
i = -1;
}
if (parsed.type == TYPE_SLIST) {
obj = parsed;
} else {
obj = eval(&parsed, env);
cleanObject(&parsed);
}
}
i++;
}
2020-05-03 21:24:36 -04:00
free(tokens);
return obj;
2016-04-18 04:25:36 -04:00
}
#ifdef STANDALONE
int readFile(const char* filename, struct Environment* env)
{
FILE* input = fopen(filename, "r");
if (!input) {
return 1;
}
_readFile(input, env);
return 0;
}
int _readFile(FILE* input, struct Environment* env)
{
2020-10-29 11:18:06 -04:00
Object r = numberObject(0);
char page[4096] = "";
const int LINE_MAX = 256;
char line[LINE_MAX];
if (fgets(line, LINE_MAX, input)) {
if (line[0] != '#' || line[1] != '!') {
strcat(page, line);
2020-10-29 11:18:06 -04:00
}
}
int isQuote = 0;
while (fgets(line, LINE_MAX, input)) {
2020-11-02 07:57:13 -05:00
int i;
for (i = 0; i < LINE_MAX; i++) {
if (line[i] != ' ') {
if (line[i] == ';') {
2020-11-02 07:57:13 -05:00
break;
} else {
int j;
for (j = i; j < LINE_MAX; j++) {
if (line[j] == '"') {
isQuote = !isQuote;
} else if (line[j] == '\0' || (!isQuote && line[j] == ';')) {
break;
}
}
strncat(page, line, j);
strcat(page, " ");
2020-11-02 07:57:13 -05:00
break;
}
}
2020-10-29 11:18:06 -04:00
}
}
r = parseEval(page, env);
cleanObject(&r);
2020-10-29 11:18:06 -04:00
fclose(input);
return 0;
}
void repl(struct Environment* env)
{
char* buf;
2022-03-15 22:47:46 -04:00
using_history();
while ((buf = readline("pebblisp::> ")) != NULL) {
if (strcmp("q", buf) == 0) {
free(buf);
break;
}
2022-03-15 22:47:46 -04:00
add_history(buf);
Object o = parseEval(buf, env);
char output[1024];
stringNObj(output, &o, 1024);
printColored(output);
printf("\n");
cleanObject(&o);
free(buf);
}
2016-04-18 04:25:36 -04:00
}
void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)
{
Object args = listObject();
for (int i = 0; i < argc; i++) {
nf_addToList(&args, stringFromSlice(argv[i], strlen(argv[i])));
}
addToEnv(env, "args", args);
}
int nestedSegfault = 0;
void handler(int nSignum, siginfo_t* si, void* vcontext)
{
if (nestedSegfault) {
printf("Nested segfault!!!\n");
exit(139);
}
nestedSegfault = 1;
printf("Segfaulted!\n");
if (lastOpen) {
printf("line: %d\n%s\n", lastOpen->lineNumber, lastOpen->text);
} else {
printf("Happened before token processing.\n");
}
ucontext_t* context = vcontext;
context->uc_mcontext.gregs[REG_RIP]++;
exit(139);
}
int main(int argc, const char* argv[])
2016-04-18 04:25:36 -04:00
{
struct Environment env = defaultEnv();
setGlobal(&env);
int ret = -1;
if (argc == 2) {
if (strcmp(argv[1], "--run-tests") == 0) {
ret = runTests(0);
} else if (strcmp(argv[1], "--run-tests=detailed") == 0) {
ret = runTests(1);
}
}
if (ret != -1) {
shredDictionary();
deleteEnv(global());
return ret;
}
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_flags = SA_SIGINFO;
action.sa_sigaction = handler;
sigaction(SIGSEGV, &action, NULL);
readFile(SCRIPTDIR "/lib.pbl", &env);
if (argc >= 2) {
FILE* file = fopen(argv[1], "r");
if (file) {
// Executing a file
loadArgsIntoEnv(argc, argv, &env);
_readFile(file, &env);
} else {
// Running arguments directly as pl code
2020-10-29 11:18:06 -04:00
Object r = numberObject(0);
for (int i = 1; i < argc; i++) {
2020-10-29 11:18:06 -04:00
r = parseEval(argv[i], &env);
printAndClean(&r);
}
}
} else {
// Running a repl
loadArgsIntoEnv(argc, argv, &env);
repl(&env);
}
deleteEnv(&env);
shredDictionary();
// fprintf(stderr, "\nHEAP-ALLOCATED OBJECTS: %d\n", getAllocations());
// fprintf(stderr, "TOTAL OBJECT.C ALLOC: %zu\n", getBytes());
2016-04-18 04:25:36 -04:00
}
#endif