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:
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:
./pebblisp

View File

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