#include "pebblisp.h" #include "tokens.h" #include #include #include #ifndef STANDALONE #define printf(...) copySlice(NULL, NULL) #else #endif void copySlice(char * dest, struct Slice *src) { if(!dest || !src) return; strncpy(dest, src->text, src->length); dest[src->length] = '\0'; } void debugSlice(struct Slice *s) { //printf("Debug Slice\n text:'"); for(int i = 0; i < s->length; i++) { //printf("%c", s->text[i]); } // printf("'\n"); // printf(" length: %d\n", s->length); } Object fetchFromEnvironment(const char *name, struct Environment *env) { int i = 0; const char *next = env->strings[i]; while(next != NULL) { // printf("fetching '%s' against '%s'\n", name, next); if(strcmp(name, next) == 0) { return env->objects[i]; } next = env->strings[++i]; } printf("DID NOT FIND SYMBOL\n"); Object o; o.type = TYPE_ERROR; return o; } Result parse(struct Slice *slices) { // printf("parse() START\n"); struct Slice *token = slices; struct Slice *rest; if(token->text != NULL) { rest = &slices[1]; } else { // printf("Assigning null...\n"); rest = NULL; } if(token->text[0] == '(') { // todo check for null rest return readSeq(rest); } else { // todo error on closed paren return R(parseAtom(token), rest); } // printf("parse() END\n"); } Result readSeq(struct Slice *tokens) { // printf("readSeq() START\n"); Object res; res.forward = NULL; res.type = TYPE_LIST; //res.list = malloc(sizeof(Object)); Object *march = &res; for(;;) { struct Slice *next = &tokens[0]; struct Slice *rest = next->text? &next[1] : NULL; if(next->text[0] == ')') { return R(res, rest); } Result r = parse(tokens); // printf("readSeq() before malloc\n"); march->forward = malloc(sizeof(struct Object)); // printf("readSeq() after malloc\n"); *march->forward = r.obj; // char out[MAX_TOK_LEN]; // printf("stringObj: %s\n", stringObj(out, &r.obj)); tokens = r.slices; march = march->forward; } // printf("readSeq() END\n"); } Object parseAtom(struct Slice *s) { Object o; o.forward = NULL; if(isDigit(s->text[0])) { o.type = TYPE_NUMBER; o.number = 0; for(int i = 0; i < s->length; i++) { o.number *= 10; o.number += s->text[i] - '0'; } } else if (s->text[0] == 'T' && s->text[1] == '\0') { o.type = TYPE_BOOL; o.number = 1; } else if (s->text[0] == 'F' && s->text[1] == '\0') { o.type = TYPE_BOOL; o.number = 0; } else { o.type = TYPE_SYMBOL; copySlice(o.name, s); } return o; } Object evalDefArgs(const Object *first, const Object *rest, struct Environment *env) { printf("evalDefArgs()\n"); Object o; return o; } Object evalIfArgs(const Object *arg_forms, struct Environment *env) { printf("evalIfArgs()\n"); Object o; printf("test_form:\n"); const Object *test_form = arg_forms; printObj(test_form); printObj(test_form->list); //printObj(test_form->list->forward); printf("test_eval:\n"); Object test_eval = eval(test_form, env); printObj(&test_eval); printf("test_form END\n"); o.type = TYPE_NUMBER; o.number = 222; return o; } Object evalBuiltIns(const Object *first, const Object *rest, int *found, struct Environment *env) { if(strcmp(first->name, "def") == 0) { *found = 0; return evalDefArgs(first, rest, env); }else if(strcmp(first->name, "if") == 0) { *found = 0; return evalIfArgs(rest, env); } Object o; *found = -1; return o; } Object eval(const Object *obj, struct Environment *env) { printf("eval(): "); printObj(obj); Object o = *obj; switch(obj->type) { case TYPE_NUMBER: case TYPE_BOOL: return *obj; case TYPE_SYMBOL: return fetchFromEnvironment(obj->name, env); case TYPE_LIST: { Object first_form = *obj->forward; { // Try to eval built-ins int i = -1; Object built_in = evalBuiltIns(&first_form, first_form.forward, &i, env); if(i == 0) return built_in; } printf("\nNo built-ins found\n"); Object first_eval = eval(&first_form, env); Object arg1 = eval(first_form.forward, env); Object arg2 = eval(first_form.forward->forward, env); printf("Evaluating func\n"); return first_eval.func(arg1, arg2); } default: ; } return o; } char* stringObj(char *dest, const Object *obj) { if(obj->type == TYPE_NUMBER) { snprintf(dest, MAX_TOK_LEN, "%d", obj->number); } else if(obj->type == TYPE_SYMBOL) { snprintf(dest, MAX_TOK_LEN, "%s", obj->name); } else if(obj->type == TYPE_BOOL) { snprintf(dest, MAX_TOK_LEN, "%s", obj->number ? "T" : "F"); } return dest; } void printObj(const Object *obj) { if(obj->type == TYPE_NUMBER) { printf("TYPE_NUMBER"); } else if(obj->type == TYPE_LIST) { printf("TYPE_LIST"); } else if(obj->type == TYPE_SYMBOL) { printf("TYPE_SYMBOL"); } else if(obj->type == TYPE_BOOL) { printf("TYPE_BOOL"); } else { printf("TYPE_OTHER"); } char temp[20] = ""; stringObj(temp, obj); printf(": %s\n", temp); } Result resultFromObjAndSlices(Object obj, struct Slice *slices) { Result r; r.obj = obj; r.slices = slices; return r; } Object basicOp(Object *obj1, Object *obj2, const char op) { Object o; o.forward = NULL; o.type = TYPE_NUMBER; switch(op){ case '+': o.number = obj1->number + obj2->number; return o; case '-': o.number = obj1->number - obj2->number; return o; case '*': o.number = obj1->number * obj2->number; return o; case '/': o.number = obj1->number / obj2->number; return o; } o.type = TYPE_BOOL; switch(op) { case '=': o.number = obj1->number == obj2->number; return o; case '>': o.number = obj1->number > obj2->number; return o; case '<': o.number = obj1->number < obj2->number; return o; } o = *obj1; return o; } #define bopf(_name, _char) \ Object _name(Object obj1, Object obj2) \ { \ return basicOp(&obj1, &obj2, _char); \ } bopf(add, '+'); bopf(sub, '-'); bopf(mul, '*'); bopf(dvi, '/'); bopf(equ, '='); void addFunc(const char *name, Object (*func)(Object, Object), struct Environment *env) { int i; for(i = 0; i < MAX_ENV_ELM; i++) { if(env->strings[i] == NULL) { //printf("Adding at %d\n", i); env->strings[i] = malloc(sizeof(name)); strncpy(env->strings[i], name, MAX_TOK_LEN); Object o; o.type = TYPE_FUNC; o.forward = NULL; o.func = func; env->objects[i] = o; break; } } } struct Environment defaultEnv() { struct Environment e; e.strings = malloc(sizeof(char*) * MAX_ENV_ELM); for(int i = 0; i < MAX_ENV_ELM; i++) { e.strings[i] = NULL; } e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); addFunc("+", &add, &e); addFunc("-", &sub, &e); addFunc("*", &mul, &e); addFunc("/", &dvi, &e); addFunc("=", &equ, &e); /* e.strings[0] = malloc(sizeof("+")); strncpy(e.strings[0], "+", MAX_TOK_LEN); Object o; o.type = TYPE_FUNC; o.forward = NULL; o.func = &add; e.objects[0] = o; */ return e; } Object parseEval(const char *input, struct Environment *env) { struct Slice *tokens = tokenize(input); struct Slice *debug = tokens; #ifdef STANDALONE if(debug) { while(debug->text) { char tok[MAX_TOK_LEN]; copySlice(tok, debug); // printf("'%s', ", tok); debug++; } // printf("\n"); } else { // printf("parse error\n"); } // printf("parseEval() parse()\n"); #endif Object parsed = parse(tokens).obj; free(tokens); return eval(&parsed, env); } #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); printObj(&obj); //printf("eval: %d\n", obj.number); break; } } int main(void) { struct Environment env = defaultEnv(); if(1) { repl(&env); } else { struct Slice *tokens = tokenize("(+ 10 5)"); struct Slice *debug = tokens; if(debug) { while(debug->text) { char tok[10]; copySlice(tok, debug); printf("'%s', ", tok); debug++; } printf("\n"); } else { printf("parse error\n"); } parse(tokens); } } #endif