#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()\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); } } Result readSeq(struct Slice *tokens) { // printf("readSeq()\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); march->forward = malloc(sizeof(Object)); *march->forward = r.obj; // char out[MAX_TOK_LEN]; // printf("stringObj: %s\n", stringObj(out, &r.obj)); tokens = r.slices; march = march->forward; } } Object parseAtom(struct Slice *s) { // printf("parseAtom()\n"); 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 { o.type = TYPE_SYMBOL; copySlice(o.name, s); } return o; } Object eval(Object *obj, struct Environment *env) { Object o = *obj; switch(obj->type) { case TYPE_NUMBER: { return *obj; } case TYPE_LIST: { // printf("TYPE_LIST\n"); Object first_form = *obj->forward; Object first_eval = eval(&first_form, env); return first_eval.func(*first_form.forward, *first_form.forward->forward); } case TYPE_SYMBOL: { return fetchFromEnvironment(obj->name, env); } default: ; } return o; } char* stringObj(char *dest, Object *obj) { if(obj->type == TYPE_NUMBER) { snprintf(dest, MAX_TOK_LEN, "%d", obj->number); } else if(obj->type == TYPE_SYMBOL) { return obj->name; } return dest; } 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; break; case '-': o.number = obj1->number - obj2->number; break; case '*': o.number = obj1->number * obj2->number; break; case '/': o.number = obj1->number / obj2->number; break; default: o = *obj1; } return o; } Object add(Object obj1, Object obj2) { return basicOp(&obj1, &obj2, '+'); } Object sub(Object obj1, Object obj2) { return basicOp(&obj1, &obj2, '-'); } Object mul(Object obj1, Object obj2) { return basicOp(&obj1, &obj2, '*'); } Object divi(Object obj1, Object obj2) { return basicOp(&obj1, &obj2, '/'); } 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); e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); addFunc("+", &add, &e); addFunc("-", &sub, &e); addFunc("*", &mul, &e); addFunc("/", &divi, &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; 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); 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