pebblisp/src/pebblisp.c

509 lines
13 KiB
C
Raw Normal View History

2016-04-18 04:25:36 -04:00
#include "pebblisp.h"
#include "tokens.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2020-05-07 20:32:01 -04:00
#ifndef STANDALONE
#undef printf
#define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__)
#endif
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);
2020-05-04 18:14:41 -04:00
dest[(int)src->length] = '\0';
2016-04-18 04:25:36 -04:00
}
void debugSlice(struct Slice *s)
{
printd("Debug Slice\n text:'");
2016-04-18 04:25:36 -04:00
for(int i = 0; i < s->length; i++) {
printd("%c", s->text[i]);
2020-05-08 02:29:06 -04:00
if(s->text[i] == '\0')
printd("NULLCHAR\n");
2016-04-18 04:25:36 -04:00
}
printd("'\n");
printd(" length: %d\n", s->length);
2016-04-18 04:25:36 -04:00
}
Object fetchFromEnvironment(const char *name, struct Environment *env)
{
2020-05-05 13:42:28 -04:00
if(!env)
return errorObject(NULL_ENV);
if(env->size == 0)
return errorObject(EMPTY_ENV);
printd("Fetching '%s' from env\n", name);
// printEnv(env);
2020-05-07 20:32:01 -04:00
for(int i = 0; i < env->size; i++) {
printd("Try %d\n", i);
if(env->strings[i] == NULL)
break;
2020-05-07 20:32:01 -04:00
if(strcmp(name, env->strings[i]) == 0) {
2016-04-18 04:25:36 -04:00
return env->objects[i];
}
}
2020-05-07 20:32:01 -04:00
printd("Trying outer %p\n", env->outer);
2020-05-05 13:42:28 -04:00
if(env->outer) {
// printEnv(env->outer);
2020-05-05 13:42:28 -04:00
return fetchFromEnvironment(name, env->outer);
}
return errorObject(DID_NOT_FIND_SYMBOL);
2016-04-18 04:25:36 -04:00
}
Result parse(struct Slice *slices)
{
struct Slice *token = slices;
struct Slice *rest;
if(token->text != NULL) {
rest = &slices[1];
} else {
2020-05-07 20:32:01 -04:00
return result(errorObject(NULL_PARSE), NULL);
2016-04-18 04:25:36 -04:00
}
if(token->text[0] == '(') {
// todo check for null rest
return readSeq(rest);
2020-05-08 02:29:06 -04:00
} else { // todo error on missing close paren
2020-05-07 20:32:01 -04:00
return result(parseAtom(token), rest);
2016-04-18 04:25:36 -04:00
}
}
Result readSeq(struct Slice *tokens)
{
2020-05-04 18:14:41 -04:00
Object res = listObject();
2016-04-18 04:25:36 -04:00
for(;;) {
struct Slice *next = tokens;
2016-04-18 04:25:36 -04:00
struct Slice *rest = next->text? &next[1] : NULL;
if(next->text[0] == ')') {
2020-05-07 20:32:01 -04:00
return result(res, rest);
2016-04-18 04:25:36 -04:00
}
Result r = parse(tokens);
2020-05-07 20:32:01 -04:00
nf_addToList(&res, r.obj);
2016-04-18 04:25:36 -04:00
tokens = r.slices;
}
}
Object parseAtom(struct Slice *s)
{
if(isDigit(s->text[0])) {
int num = 0;
2016-04-18 04:25:36 -04:00
for(int i = 0; i < s->length; i++) {
num *= 10;
num += s->text[i] - '0';
2016-04-18 04:25:36 -04:00
}
return numberObject(num);
} else if (s->text[0] == 'T' && s->length == 1) {
return boolObject(1);
} else if (s->text[0] == 'F' && s->length == 1) {
return boolObject(0);
2016-04-18 04:25:36 -04:00
} else {
Object o = symbolObject();
2016-04-18 04:25:36 -04:00
copySlice(o.name, s);
return o;
2016-04-18 04:25:36 -04:00
}
}
2020-05-04 18:14:41 -04:00
Object evalDefArgs(const Object *arg_forms, struct Environment *env)
2020-05-04 10:03:35 -04:00
{
const Object *first_form = arg_forms;
2020-05-04 18:14:41 -04:00
const char *name = first_form->name;
2020-05-04 10:03:35 -04:00
2020-05-08 02:29:06 -04:00
// Immediately adding the function to the env might allow recursion?
2020-05-04 18:14:41 -04:00
Object second_eval = eval(first_form->forward, env);
2020-05-04 10:03:35 -04:00
2020-05-04 18:14:41 -04:00
addToEnv(env, name, second_eval);
2020-05-04 10:03:35 -04:00
2020-05-04 18:14:41 -04:00
return *first_form;
}
2020-05-04 10:03:35 -04:00
2020-05-04 18:14:41 -04:00
Object evalIfArgs(const Object *arg_forms, struct Environment *env)
{
printd("eval(arg_forms):\n ");
Object o = eval(arg_forms, env);
debugObj(&o);
printd("eval(arg_forms->forward):\n ");
o = eval(arg_forms->forward, env);
debugObj(&o);
printd("eval(arg_forms->forward->forward):\n ");
o = eval(arg_forms->forward->forward, env);
debugObj(&o);
2020-05-04 18:14:41 -04:00
return eval(arg_forms, env).number?
eval(arg_forms->forward, env) :
eval(arg_forms->forward->forward, env);
2020-05-04 10:03:35 -04:00
}
2020-05-05 13:42:28 -04:00
Object evalLambdaArgs(const Object *arg_forms)
{
2020-05-08 02:29:06 -04:00
// params // body
return constructLambda(arg_forms, arg_forms? arg_forms->forward : NULL);
2020-05-05 13:42:28 -04:00
}
Object evalMapArgs(const Object *arg_forms, struct Environment *env)
2020-05-06 11:58:09 -04:00
{
2020-05-07 20:32:01 -04:00
if(!arg_forms)
return errorObject(NULL_MAP_ARGS);
2020-05-07 20:32:01 -04:00
const Object lambda = eval(arg_forms, env);
Object *oldList = arg_forms->forward;
2020-05-07 20:32:01 -04:00
2020-05-08 02:29:06 -04:00
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST)
return errorObject(BAD_TYPE);
2020-05-07 20:32:01 -04:00
Object list = listObject();
2020-05-07 20:32:01 -04:00
2020-05-08 02:29:06 -04:00
const Object *oldElement = oldList->list;
2020-05-07 20:32:01 -04:00
while(oldElement != NULL) {
// Create a new list for each element in the list,
// since lambda evaluation looks for a list
const Object tempList = startList(*oldElement);
2020-05-07 20:32:01 -04:00
struct Environment newEnv =
envForLambda(&lambda.lambda->params, &tempList, env);
const Object evalLambda = eval(&lambda.lambda->body, &newEnv);
nf_addToList(&list, evalLambda);
2020-05-07 20:32:01 -04:00
oldElement = oldElement->forward;
}
return list;
2020-05-06 11:58:09 -04:00
}
2020-05-05 13:42:28 -04:00
struct Environment envForLambda(const Object *params, const Object *arg_forms,
struct Environment *outer)
{
printd("envForLambda()\n");
debugObj(arg_forms);
2020-05-07 20:32:01 -04:00
int paramCount = listLength(params);
2020-05-07 20:32:01 -04:00
struct Environment env = {
.outer = outer,
.strings = NULL,
.objects = NULL,
.size = paramCount
};
2020-05-05 13:42:28 -04:00
if(paramCount == 0)
2020-05-05 13:42:28 -04:00
return env;
env.strings = calloc(sizeof(char*), paramCount + 1);
env.objects = malloc(sizeof(Object) * paramCount + 1);
const Object *march = arg_forms;
printd("Param count: %d\n", paramCount);
for(int i = 0; i < paramCount; i++) {
const char *newObjName = itemAt(params, i)->name;
const Object newEnvObj = eval(march, outer);
printd("Adding new object '%s' to lambda env: ", newObjName);
debugObj(&newEnvObj);
addToEnv(&env, newObjName, newEnvObj); // Could use eval_forms?
march = march->forward;
}
2020-05-05 13:42:28 -04:00
return env;
}
Object evalBuiltIns(const Object *first, const Object *rest,
2020-05-04 10:03:35 -04:00
struct Environment *env)
{
if(strcmp(first->name, "def") == 0) {
2020-05-04 18:14:41 -04:00
return evalDefArgs(rest, env);
2020-05-06 03:09:25 -04:00
} else if(strcmp(first->name, "if") == 0) {
// Seems `rest` is the next *input*, not the next list item
2020-05-04 10:03:35 -04:00
return evalIfArgs(rest, env);
2020-05-06 03:09:25 -04:00
} else if(strcmp(first->name, "fn") == 0) {
2020-05-05 13:42:28 -04:00
return evalLambdaArgs(rest);
2020-05-07 20:32:01 -04:00
} else if(strcmp(first->name, "map") == 0) {
return evalMapArgs(rest, env);
2020-05-04 10:03:35 -04:00
}
2020-05-05 13:42:28 -04:00
return errorObject(BUILT_IN_NOT_FOUND);
}
void eval_forms(Object *destList, const Object *src, struct Environment *env)
{
int length = listLength(src) - 1; // Not counting first_form
for(int i = 0; i < length; i++) { // Evaluates all in list
destList[i] = eval(itemAt(src, i + 1), env); // Skip the first
}
2020-05-04 10:03:35 -04:00
}
Object eval(const Object *obj, struct Environment *env)
2016-04-18 04:25:36 -04:00
{
printd("eval():\n");
debugObj(obj);
2016-04-18 04:25:36 -04:00
switch(obj->type) {
case TYPE_NUMBER:
2020-05-04 10:03:35 -04:00
case TYPE_BOOL:
2020-05-06 03:09:25 -04:00
return *obj; // Return as is
2020-05-04 10:03:35 -04:00
case TYPE_SYMBOL:
return fetchFromEnvironment(obj->name, env);
2020-05-04 10:03:35 -04:00
2016-04-18 04:25:36 -04:00
case TYPE_LIST:
{
2020-05-06 03:09:25 -04:00
if(listLength(obj) == 0)
return *obj;
2020-05-05 13:42:28 -04:00
if(listLength(obj) == 1)
return eval(obj->list, env);
Object *first_form = obj->list;
2020-05-04 10:03:35 -04:00
{ // Try to eval built-ins
Object built_in =
2020-05-05 13:42:28 -04:00
evalBuiltIns(first_form, first_form->forward, env);
2020-05-07 20:32:01 -04:00
2020-05-06 03:09:25 -04:00
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
2020-05-07 20:32:01 -04:00
if(built_in.type != TYPE_ERROR) {
2020-05-04 10:03:35 -04:00
return built_in;
2020-05-07 20:32:01 -04:00
}
2020-05-04 10:03:35 -04:00
}
2020-05-05 13:42:28 -04:00
Object first_eval = eval(first_form, env);
if(first_eval.type == TYPE_FUNC) {
int length = listLength(obj) - 1; // Not counting first_form
Object rest[length];
eval_forms(rest, obj, env);
Object func_eval = rest[0];
for(int i = 1; i < length; i++) {
func_eval = first_eval.func(func_eval, rest[i]);
}
2020-05-06 03:09:25 -04:00
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
2020-05-05 13:42:28 -04:00
return func_eval;
2020-05-05 13:42:28 -04:00
} else if (first_eval.type == TYPE_LAMBDA) {
struct Environment newEnv =
envForLambda(&first_eval.lambda->params, first_form->forward, env);
2020-05-07 20:32:01 -04:00
Object ret = eval(&first_eval.lambda->body, &newEnv);
//printEnv(&newEnv);
2020-05-07 20:32:01 -04:00
deleteEnv(&newEnv);
return ret;
2020-05-05 13:42:28 -04:00
} else {
2020-05-06 11:58:09 -04:00
return *obj;
2020-05-05 13:42:28 -04:00
}
}
case TYPE_LAMBDA:
return errorObject(UNEXPECTED_FORM);
2016-04-18 04:25:36 -04:00
default:
;
}
2020-05-05 13:42:28 -04:00
return *obj;
2016-04-18 04:25:36 -04:00
}
2020-05-07 20:32:01 -04:00
Result result(Object obj, struct Slice *slices)
2016-04-18 04:25:36 -04:00
{
2020-05-07 20:32:01 -04:00
return (Result) {
.obj = obj,
.slices = slices
};
2016-04-18 04:25:36 -04:00
}
2020-05-04 18:14:41 -04:00
// todo could include and return a starting index for faster multi-adds
2020-05-07 20:32:01 -04:00
// Maybe extend existing environment using a malloc'd outer, instead of realloc
2020-05-04 18:14:41 -04:00
void addToEnv(struct Environment *env, const char *name, const Object obj)
{
int i;
2020-05-07 20:32:01 -04:00
for(i = 0; i < env->size ; i++) {
2020-05-04 18:14:41 -04:00
if(env->strings[i] == NULL) {
env->strings[i] = calloc(sizeof(char), strlen(name) + 1);
2020-05-07 20:32:01 -04:00
strncpy(env->strings[i], name, strlen(name));
2020-05-04 18:14:41 -04:00
env->objects[i] = obj;
// if(obj.type == TYPE_LIST)
// copyList(&env->objects[i], &obj);
2020-05-07 20:32:01 -04:00
return;
2020-05-04 18:14:41 -04:00
}
2020-05-05 13:42:28 -04:00
if(strcmp(env->strings[i], name) == 0) {
2020-05-06 03:09:25 -04:00
cleanObject(&env->objects[i]);
2020-05-05 13:42:28 -04:00
env->objects[i] = obj;
// if(obj.type == TYPE_LIST)
// copyList(&env->objects[i], &obj);
2020-05-07 20:32:01 -04:00
return;
2020-05-05 13:42:28 -04:00
}
}
2020-05-07 20:32:01 -04:00
printd("Reallocating environment\n");
const int inc = 5;
env->size += inc;
env->strings = realloc(env->strings, sizeof(char*) * env->size);
env->objects = realloc(env->objects, sizeof(Object) * env->size);
for(int j = 0; j < inc; j++) {
env->strings[i + j] = NULL;
}
env->strings[i] = malloc(strlen(name) + 1);
strncpy(env->strings[i], name, strlen(name) + 1);
2020-05-07 20:32:01 -04:00
env->objects[i] = obj;
2020-05-05 13:42:28 -04:00
}
void printEnv(struct Environment *env)
{
2020-05-06 03:09:25 -04:00
if(!env) {
printf("NULL env\n");
2020-05-06 03:09:25 -04:00
return;
}
2020-05-07 20:32:01 -04:00
for(int i = 0; i < env->size; i++) {
2020-05-05 13:42:28 -04:00
if(env->strings[i] == NULL)
return;
printf("env[%d]: '%s'\n ", i, env->strings[i]);
printObj(&env->objects[i]);
2020-05-04 18:14:41 -04:00
}
}
2016-04-18 04:25:36 -04:00
Object basicOp(Object *obj1, Object *obj2, const char op)
{
2020-05-04 18:14:41 -04:00
const int n1 = obj1->number;
const int n2 = obj2->number;
2016-04-18 04:25:36 -04:00
switch(op){
case '+':
return numberObject(n1 + n2);
2016-04-18 04:25:36 -04:00
case '-':
return numberObject(n1 - n2);
2016-04-18 04:25:36 -04:00
case '*':
return numberObject(n1 * n2);
2016-04-18 04:25:36 -04:00
case '/':
return numberObject(n1 / n2);
2016-04-18 04:25:36 -04:00
2020-05-04 10:03:35 -04:00
case '=':
return boolObject(n1 == n2);
2020-05-04 10:03:35 -04:00
case '>':
return boolObject(n1 > n2);
2020-05-04 10:03:35 -04:00
case '<':
return boolObject(n1 < n2);
2020-05-04 10:03:35 -04:00
}
2016-04-18 04:25:36 -04:00
return *obj1;
2020-05-04 10:03:35 -04:00
}
2016-04-18 04:25:36 -04:00
2020-05-04 10:03:35 -04:00
#define bopf(_name, _char) \
Object _name(Object obj1, Object obj2) \
{ return basicOp(&obj1, &obj2, _char); }
2016-04-18 04:25:36 -04:00
2020-05-04 10:03:35 -04:00
bopf(add, '+');
bopf(sub, '-');
bopf(mul, '*');
bopf(dvi, '/');
bopf(equ, '=');
2020-05-04 18:14:41 -04:00
bopf(gth, '>');
bopf(lth, '<');
2016-04-18 04:25:36 -04:00
void addFunc(const char *name, Object (*func)(Object, Object),
struct Environment *env)
{
Object o = newObject(TYPE_FUNC);
2020-05-04 18:14:41 -04:00
o.func = func;
addToEnv(env, name, o);
}
void deleteEnv(struct Environment *e)
{
int i = 0;
while(e->strings[i]) {
free(e->strings[i]);
2020-05-07 20:32:01 -04:00
cleanObject(&e->objects[i]);
2020-05-04 18:14:41 -04:00
i++;
2016-04-18 04:25:36 -04:00
}
2020-05-04 18:14:41 -04:00
free(e->strings);
e->strings = NULL;
free(e->objects);
e->objects = NULL;
2016-04-18 04:25:36 -04:00
}
struct Environment defaultEnv() {
struct Environment e;
2020-05-05 13:42:28 -04:00
e.outer = NULL;
2020-05-07 20:32:01 -04:00
e.strings = calloc(sizeof(char*), MAX_ENV_ELM);
e.size = MAX_ENV_ELM;
// for(int i = 0; i < MAX_ENV_ELM; i++) {
// e.strings[i] = NULL;
// }
2020-05-04 18:14:41 -04:00
2016-04-18 04:25:36 -04:00
e.objects = malloc(sizeof(Object) * MAX_ENV_ELM);
2020-05-04 18:14:41 -04:00
2016-04-18 04:25:36 -04:00
addFunc("+", &add, &e);
addFunc("-", &sub, &e);
addFunc("*", &mul, &e);
2020-05-04 10:03:35 -04:00
addFunc("/", &dvi, &e);
addFunc("=", &equ, &e);
2020-05-04 18:14:41 -04:00
addFunc(">", &gth, &e);
addFunc("<", &lth, &e);
addFunc("len", &len, &e);
parseEval("(def max (fn (a b) (if (> a b) a b)))", &e);
parseEval("(def min (fn (a b) (if (< a b) a b)))", &e);
2016-04-18 04:25:36 -04:00
return e;
}
Object parseEval(const char *input, struct Environment *env)
{
2020-05-04 18:14:41 -04:00
struct Slice *tokens = nf_tokenize(input);
2020-05-08 02:29:06 -04:00
if(!tokens)
return errorObject(MISMATCHED_PARENS);
#ifdef DEBUG
2020-05-04 18:14:41 -04:00
struct Slice *debug = tokens;
2016-04-18 04:25:36 -04:00
if(debug) {
while(debug->text) {
2020-05-03 17:00:45 -04:00
char tok[MAX_TOK_LEN];
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++;
}
}
2020-05-03 17:00:45 -04:00
#endif
2016-04-18 04:25:36 -04:00
Object parsed = parse(tokens).obj;
2020-05-03 21:24:36 -04:00
free(tokens);
2020-05-07 20:32:01 -04:00
Object ret = eval(&parsed, env);
cleanObject(&parsed);
return ret;
2016-04-18 04:25:36 -04:00
}
#ifdef STANDALONE
int repl(struct Environment *env)
{
char input[100] = "";
while(input[0] != 'q') {
printf("pebblisp>> ");
fgets(input, 100, stdin);
Object obj = parseEval(input, env);
2020-05-07 20:32:01 -04:00
// printAndClean(&obj);
2020-05-04 10:03:35 -04:00
printObj(&obj);
//printEnv(env);
2016-04-18 04:25:36 -04:00
}
}
int main(int argc, const char* argv[])
2016-04-18 04:25:36 -04:00
{
struct Environment env = defaultEnv();
if(argc == 2) {
Object r = parseEval(argv[1], &env);
printAndClean(&r);
} else {
2020-05-04 18:14:41 -04:00
#ifndef NO_REPL
repl(&env);
2020-05-04 18:14:41 -04:00
#else
Object r = parseEval("(def ad (fn (a) (+ 1 a)))", &env);
printAndClean(&r);
r = parseEval("(map ad (1 2 3))", &env);
printAndClean(&r);
2020-05-04 18:14:41 -04:00
#endif
}
2020-05-04 18:14:41 -04:00
deleteEnv(&env);
2016-04-18 04:25:36 -04:00
}
#endif