make format

This commit is contained in:
NunoSempere 2023-05-01 21:00:20 -04:00
parent ff8b45489b
commit f9bfc8fb46

View File

@ -212,8 +212,8 @@ lispval* pop_lispval(lispval* v, int i)
{ {
lispval* r = v->cell[i]; lispval* r = v->cell[i];
/* Shift memory after the item at "i" over the top */ /* Shift memory after the item at "i" over the top */
memmove(&v->cell[i], &v->cell[i+1], memmove(&v->cell[i], &v->cell[i + 1],
sizeof(lispval*) * (v->count-i-1)); sizeof(lispval*) * (v->count - i - 1));
/* Decrease the count of items in the list */ /* Decrease the count of items in the list */
v->count--; v->count--;
@ -223,42 +223,51 @@ lispval* pop_lispval(lispval* v, int i)
return r; return r;
} }
lispval* take_lispval(lispval* v, int i){ // Unneeded. lispval* take_lispval(lispval* v, int i)
{ // Unneeded.
lispval* x = pop_lispval(v, i); lispval* x = pop_lispval(v, i);
delete_lispval(v); delete_lispval(v);
return x; return x;
} }
lispval* builtin_op(char* op, lispval* v){ lispval* builtin_op(char* op, lispval* v)
{
// For now, ensure all args are numbers // For now, ensure all args are numbers
for(int i=0; i<v->count; i++){ for (int i = 0; i < v->count; i++) {
if(v->cell[i]->type != LISPVAL_NUM){ if (v->cell[i]->type != LISPVAL_NUM) {
return lispval_err("Error: Operating on non-numbers. This can be caused by an input like (+ 1 2 (3 * 4)). Because the (3 * 4) doesn't have the correct operation order, it isn't simplified, and then + can't sum over it."); return lispval_err("Error: Operating on non-numbers. This can be caused by an input like (+ 1 2 (3 * 4)). Because the (3 * 4) doesn't have the correct operation order, it isn't simplified, and then + can't sum over it.");
} }
} }
// Check how many elements // Check how many elements
if(v->count == 0){ if (v->count == 0) {
return lispval_err("Error: No numbers on which to operate!"); return lispval_err("Error: No numbers on which to operate!");
} else if(v->count == 1) { } else if (v->count == 1) {
if(strcmp(op, "-") == 0){ if (strcmp(op, "-") == 0) {
return lispval_num(-v->cell[0]->num); return lispval_num(-v->cell[0]->num);
}else { } else {
return lispval_err("Error: Non minus unary operation"); return lispval_err("Error: Non minus unary operation");
} }
} else if (v->count >= 2){ } else if (v->count >= 2) {
lispval* x = pop_lispval(v,0); lispval* x = pop_lispval(v, 0);
while(v->count > 0){ while (v->count > 0) {
// Pop the next element // Pop the next element
lispval* y = pop_lispval(v, 0); lispval* y = pop_lispval(v, 0);
if (strcmp(op, "+") == 0) { x->num += y->num; } if (strcmp(op, "+") == 0) {
if (strcmp(op, "-") == 0) { x->num -= y->num; } x->num += y->num;
if (strcmp(op, "*") == 0) { x->num *= y->num; } }
if (strcmp(op, "-") == 0) {
x->num -= y->num;
}
if (strcmp(op, "*") == 0) {
x->num *= y->num;
}
if (strcmp(op, "/") == 0) { if (strcmp(op, "/") == 0) {
if (y->num == 0) { if (y->num == 0) {
delete_lispval(x); delete_lispval(y); delete_lispval(x);
delete_lispval(y);
return lispval_err("Error: Division By Zero!"); return lispval_err("Error: Division By Zero!");
} }
x->num /= y->num; x->num /= y->num;
@ -272,24 +281,23 @@ lispval* builtin_op(char* op, lispval* v){
} }
} }
lispval* evaluate_lispval(lispval* l) lispval* evaluate_lispval(lispval* l)
{ {
// Evaluate the children if needed // Evaluate the children if needed
for(int i=0; i<l->count; i++){ for (int i = 0; i < l->count; i++) {
if(l->cell[i]->type == LISPVAL_SEXPR){ if (l->cell[i]->type == LISPVAL_SEXPR) {
l->cell[i] = evaluate_lispval(l->cell[i]); l->cell[i] = evaluate_lispval(l->cell[i]);
} }
} }
// Check if any are errors. // Check if any are errors.
for(int i=0; i<l->count; i++){ for (int i = 0; i < l->count; i++) {
if(l->cell[i]->type == LISPVAL_ERR){ if (l->cell[i]->type == LISPVAL_ERR) {
return pop_lispval(l, i); return pop_lispval(l, i);
} }
} }
// Check if the first element is an operation. // Check if the first element is an operation.
if(l->count >=2 && ( (l->cell[0])->type == LISPVAL_SYM)){ if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_SYM)) {
lispval* op = pop_lispval(l, 0); lispval* op = pop_lispval(l, 0);
lispval* result = builtin_op(op->sym, l); lispval* result = builtin_op(op->sym, l);
delete_lispval(op); delete_lispval(op);
@ -338,7 +346,7 @@ int main(int argc, char** argv)
mpc_ast_t* ast = result.output; mpc_ast_t* ast = result.output;
// Print AST if VERBOSE // Print AST if VERBOSE
if (VERBOSE){ if (VERBOSE) {
printf("\nPrinting AST"); printf("\nPrinting AST");
print_ast(ast, 0); print_ast(ast, 0);
} }
@ -346,7 +354,7 @@ int main(int argc, char** argv)
// if(VERBOSE) printf("\n\nEvaluating the AST"); // if(VERBOSE) printf("\n\nEvaluating the AST");
// lispval result = evaluate_ast(ast); // lispval result = evaluate_ast(ast);
lispval* l = read_lispval(ast); lispval* l = read_lispval(ast);
if (VERBOSE){ if (VERBOSE) {
printf("\n\nPrinting initially parsed lispvalue"); printf("\n\nPrinting initially parsed lispvalue");
printf("\nTree printing: "); printf("\nTree printing: ");
print_lispval_tree(l, 2); print_lispval_tree(l, 2);