Some clean-up. Removed NO_REPL (redundant via cli)

This commit is contained in:
= 2020-05-11 06:15:44 +01:00
parent 4f6aedc4fa
commit 8a45444c52
3 changed files with 45 additions and 57 deletions

View File

@ -4,8 +4,5 @@ all:
debug: debug:
gcc -g -O0 -o pebblisp -D STANDALONE -D DEBUG pebblisp.c tokens.c object.c env.c && ./tests.sh gcc -g -O0 -o pebblisp -D STANDALONE -D DEBUG pebblisp.c tokens.c object.c env.c && ./tests.sh
phrase:
gcc -g -O0 -o pebblisp -D STANDALONE -D DEBUG -D NO_REPL pebblisp.c tokens.c object.c env.c && ./tests.sh
run: run:
./pebblisp ./pebblisp

View File

@ -126,7 +126,7 @@ void printEnv(struct Environment *env)
} }
void addFunc(const char *name, Object (*func)(Object, Object, struct Environment*), void addFunc(const char *name, Object (*func)(Object, Object, struct Environment*),
struct Environment *env) struct Environment *env)
{ {
Object o = newObject(TYPE_FUNC); Object o = newObject(TYPE_FUNC);
o.func = func; o.func = func;

View File

@ -19,6 +19,10 @@ void copySlice(char * dest, struct Slice *src)
void debugSlice(struct Slice *s) void debugSlice(struct Slice *s)
{ {
if(!s) {
printd("NULL SLICE\n");
return;
}
printd("Debug Slice\n text:'"); printd("Debug Slice\n text:'");
for(int i = 0; i < s->length; i++) { for(int i = 0; i < s->length; i++) {
printd("%c", s->text[i]); printd("%c", s->text[i]);
@ -32,19 +36,17 @@ void debugSlice(struct Slice *s)
Result parse(struct Slice *slices) Result parse(struct Slice *slices)
{ {
struct Slice *token = slices; struct Slice *token = slices;
struct Slice *rest; if(token && token->text) {
if(token->text != NULL) { struct Slice *rest = &slices[1];
rest = &slices[1]; if(token->text[0] == '(') {
// todo check for null rest
return readSeq(rest);
} else { // todo error on missing close paren
return result(parseAtom(token), rest);
}
} else { } else {
return result(errorObject(NULL_PARSE), NULL); return result(errorObject(NULL_PARSE), NULL);
} }
if(token->text[0] == '(') {
// todo check for null rest
return readSeq(rest);
} else { // todo error on missing close paren
return result(parseAtom(token), rest);
}
} }
Result readSeq(struct Slice *tokens) Result readSeq(struct Slice *tokens)
@ -89,14 +91,14 @@ Object parseAtom(struct Slice *s)
Object evalDefArgs(const Object *arg_forms, struct Environment *env) Object evalDefArgs(const Object *arg_forms, struct Environment *env)
{ {
const Object *first_form = arg_forms; const Object *newSymbol = arg_forms;
const char *name = first_form->name; const char *name = newSymbol->name;
Object second_eval = eval(first_form->forward, env); const Object newValue = eval(newSymbol->forward, env);
addToEnv(env, name, second_eval); addToEnv(env, name, newValue);
return *first_form; return *newSymbol;
} }
Object evalIfArgs(const Object *arg_forms, struct Environment *env) Object evalIfArgs(const Object *arg_forms, struct Environment *env)
@ -118,25 +120,22 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env)
return errorObject(NULL_MAP_ARGS); return errorObject(NULL_MAP_ARGS);
const Object lambda = eval(arg_forms, env); const Object lambda = eval(arg_forms, env);
Object *oldList = arg_forms->forward; const Object *inputList = arg_forms->forward;
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) if(lambda.type != TYPE_LAMBDA || inputList->type != TYPE_LIST)
return errorObject(BAD_TYPE); return errorObject(BAD_TYPE);
Object list = listObject(); Object list = listObject();
const Object *oldElement = oldList->list; FOR_POINTER_IN_LIST(inputList) {
while(oldElement != NULL) { // Create a new list for each element,
// Create a new list for each element in the list,
// since lambda evaluation looks for a list // since lambda evaluation looks for a list
const Object tempList = startList(*oldElement); const Object tempList = startList(*POINTER);
struct Environment newEnv = struct Environment newEnv =
envForLambda(&lambda.lambda->params, &tempList, env); envForLambda(&lambda.lambda->params, &tempList, env);
const Object evalLambda = eval(&lambda.lambda->body, &newEnv); // Add the lambda evaluation to the list
nf_addToList(&list, eval(&lambda.lambda->body, &newEnv));
nf_addToList(&list, evalLambda);
oldElement = oldElement->forward;
} }
return list; return list;
@ -209,6 +208,7 @@ Object eval(const Object *obj, struct Environment *env)
for(int i = 1; i < length; i++) { for(int i = 1; i < length; i++) {
func_eval = first_eval.func(func_eval, rest[i], env); func_eval = first_eval.func(func_eval, rest[i], env);
} }
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble // deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
return func_eval; return func_eval;
@ -216,12 +216,10 @@ Object eval(const Object *obj, struct Environment *env)
struct Environment newEnv = struct Environment newEnv =
envForLambda(&first_eval.lambda->params, first_form->forward, env); envForLambda(&first_eval.lambda->params, first_form->forward, env);
Object ret = eval(&first_eval.lambda->body, &newEnv); Object ret = eval(&first_eval.lambda->body, &newEnv);
//printEnv(&newEnv);
deleteEnv(&newEnv); deleteEnv(&newEnv);
return ret; return ret;
} else { } else {
// printf("Return list as passed");
return *obj; return *obj;
} }
} }
@ -229,9 +227,8 @@ Object eval(const Object *obj, struct Environment *env)
return errorObject(UNEXPECTED_FORM); return errorObject(UNEXPECTED_FORM);
default: default:
; return *obj;
} }
return *obj;
} }
Result result(Object obj, struct Slice *slices) Result result(Object obj, struct Slice *slices)
@ -295,21 +292,24 @@ Object basicOp(const Object *obj1, const Object *obj2, const char op,
int lists = (obj1->type == TYPE_LIST) + (obj2->type == TYPE_LIST); int lists = (obj1->type == TYPE_LIST) + (obj2->type == TYPE_LIST);
if(lists == 0) { if(lists == 0) {
return _basicOp(obj1, obj2, op); return _basicOp(obj1, obj2, op);
} else if(lists == 1) { // Single operand is applied to each element in list } else if(lists == 1) { // Single operand is applied to each element in list
const Object *listObj = (obj1->type == TYPE_LIST)? obj1 : obj2; const Object *listObj = (obj1->type == TYPE_LIST)? obj1 : obj2;
const Object *singleObj = (obj1->type == TYPE_LIST)? obj2 : obj1; const Object *singleObj = (obj1->type == TYPE_LIST)? obj2 : obj1;
Object newList = listObject(); Object newList = listObject();
FOR_POINTER_IN_LIST(listObj) { FOR_POINTER_IN_LIST(listObj) {
Object adding = eval(POINTER, env); Object adding = eval(POINTER, env);
nf_addToList(&newList, _basicOp(&adding, singleObj, op)); nf_addToList(&newList, _basicOp(&adding, singleObj, op));
} }
return newList; return newList;
} else { // 2 lists } else { // 2 lists
if(listLength(obj1) == listLength(obj2)) { if(listLength(obj1) == listLength(obj2)) {
Object newList = listObject(); Object newList = listObject();
FOR_POINTERS_IN_LISTS(obj1, obj2) { FOR_POINTERS_IN_LISTS(obj1, obj2) {
Object ev1 = eval(P1, env); const Object ev1 = eval(P1, env);
Object ev2 = eval(P2, env); const Object ev2 = eval(P2, env);
nf_addToList(&newList, _basicOp(&ev1, &ev2, op)); nf_addToList(&newList, _basicOp(&ev1, &ev2, op));
} }
return newList; return newList;
@ -319,37 +319,39 @@ Object basicOp(const Object *obj1, const Object *obj2, const char op,
} }
} }
#define bopf(_name, _char) \ #define BASIC_OP(_name, _char) \
Object _name(Object obj1, Object obj2, struct Environment *env) \ Object _name(Object obj1, Object obj2, struct Environment *env) \
{ return basicOp(&obj1, &obj2, _char, env); } { return basicOp(&obj1, &obj2, _char, env); }
bopf(add, '+'); BASIC_OP(add, '+');
bopf(sub, '-'); BASIC_OP(sub, '-');
bopf(mul, '*'); BASIC_OP(mul, '*');
bopf(dvi, '/'); BASIC_OP(dvi, '/');
bopf(mod, '%'); BASIC_OP(mod, '%');
bopf(equ, '='); BASIC_OP(equ, '=');
bopf(gth, '>'); BASIC_OP(gth, '>');
bopf(lth, '<'); BASIC_OP(lth, '<');
#undef bopf #undef BASIC_OP
Object parseEval(const char *input, struct Environment *env) Object parseEval(const char *input, struct Environment *env)
{ {
struct Slice *tokens = nf_tokenize(input); struct Slice *tokens = nf_tokenize(input);
if(!tokens) if(!tokens)
return errorObject(MISMATCHED_PARENS); return errorObject(MISMATCHED_PARENS);
#ifdef DEBUG #ifdef DEBUG
struct Slice *debug = tokens; struct Slice *debug = tokens;
if(debug) { if(debug) {
while(debug->text) { while(debug->text) {
char tok[MAX_TOK_LEN]; char tok[MAX_TOK_LEN];
copySlice(tok, debug); copySlice(tok, debug);
//printd("slice: '%s'\n", tok); printd("slice: '%s'\n", tok);
debug++; debug++;
} }
} }
#endif #endif
int i = 0; int i = 0;
int parens = 0; int parens = 0;
Object obj; Object obj;
@ -381,8 +383,6 @@ int repl(struct Environment *env)
fgets(input, 100, stdin); fgets(input, 100, stdin);
Object obj = parseEval(input, env); Object obj = parseEval(input, env);
printAndClean(&obj); printAndClean(&obj);
// printObj(&obj);
// printEnv(env);
} }
} }
@ -396,18 +396,9 @@ int main(int argc, const char* argv[])
r = parseEval(argv[i], &env); r = parseEval(argv[i], &env);
} }
printObj(&r); printObj(&r);
// Object r = parseEval(argv[1], &env);
// printAndClean(&r); // printAndClean(&r);
} else { } else {
#ifndef NO_REPL
repl(&env); repl(&env);
#else
Object r = parseEval("(def ad (fn (a) (+ 1 a)))", &env);
printAndClean(&r);
r = parseEval("(map ad (1 2 3))", &env);
printAndClean(&r);
#endif
} }
// deleteEnv(&env);
} }
#endif #endif