2018-06-07 19:10:53 -04:00
|
|
|
#include <stdio.h>
|
2018-06-09 21:22:14 -04:00
|
|
|
#include "environment.h"
|
2018-06-07 19:10:53 -04:00
|
|
|
#include "io.h"
|
|
|
|
|
|
|
|
void flval_expr_print(FILE* stream, lval* v, char open, char close) {
|
|
|
|
putchar(open);
|
|
|
|
for (int i = 0; i < v->count; i++) {
|
|
|
|
// Print value contained within
|
|
|
|
flval_print(stream, v->cell[i]);
|
|
|
|
|
|
|
|
// Put a trailing whitespace unless its the last element
|
|
|
|
if (i != (v->count - 1)) {
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putchar(close);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flval_print(FILE* stream, lval* v) {
|
|
|
|
switch (v->type) {
|
|
|
|
// If it's an integer, then print it out
|
|
|
|
case LVAL_LONG: fprintf(stream, "%li", v->data.num); break;
|
|
|
|
|
|
|
|
case LVAL_DOUBLE: fprintf(stream, "%lf", v->data.dec); break;
|
|
|
|
|
2018-06-17 11:10:57 -04:00
|
|
|
case LVAL_ERR: fprintf(stream, "Error: %s", v->data.err); break;
|
2018-06-07 19:10:53 -04:00
|
|
|
|
2018-06-17 11:10:57 -04:00
|
|
|
case LVAL_SYM: fprintf(stream, "%s", v->data.sym); break;
|
2018-06-07 19:10:53 -04:00
|
|
|
|
|
|
|
case LVAL_SEXPR: flval_expr_print(stream, v, '(', ')'); break;
|
|
|
|
|
|
|
|
case LVAL_QEXPR: flval_expr_print(stream, v, '{', '}'); break;
|
2018-06-09 18:40:32 -04:00
|
|
|
|
2018-06-10 10:11:41 -04:00
|
|
|
case LVAL_FUN:
|
|
|
|
if (v->builtin) {
|
|
|
|
fprintf(stream, "<function>");
|
|
|
|
} else {
|
|
|
|
fprintf(stream, "(\\ "); flval_print(stream, v->formals);
|
|
|
|
fprintf(stream, " "); flval_print(stream, v->body); fprintf(stream, ")");
|
|
|
|
} break;
|
2018-06-07 19:10:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lval_print(lval* v) { flval_print(stdout, v); }
|
|
|
|
|
|
|
|
void lval_println(lval* v) { lval_print(v); putchar('\n'); }
|