From 39e5b76f8ba41771e1ddbadb088cc85db4eae00c Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 11 Feb 2026 07:48:49 +0000 Subject: [PATCH] lisp: lisp_print --- include/alisp/lisp.h | 4 +++ src/lisp.c | 71 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/include/alisp/lisp.h b/include/alisp/lisp.h index db9c35e..52b29a1 100644 --- a/include/alisp/lisp.h +++ b/include/alisp/lisp.h @@ -8,6 +8,8 @@ #ifndef LISP_H #define LISP_H +#include + #include #include @@ -52,6 +54,8 @@ lisp_t *cdr(lisp_t *); void lisp_free(lisp_t *); void lisp_free_rec(lisp_t *); +void lisp_print(FILE *, lisp_t *); + #endif /* Copyright (C) 2026 Aryadev Chavali diff --git a/src/lisp.c b/src/lisp.c index 922b164..42395fb 100644 --- a/src/lisp.c +++ b/src/lisp.c @@ -147,6 +147,77 @@ void lisp_free_rec(lisp_t *item) } } +void lisp_print(FILE *fp, lisp_t *lisp) +{ + if (!fp) + return; + switch (get_tag(lisp)) + { + case TAG_NIL: + fprintf(fp, "NIL"); + break; + case TAG_INT: +#if VERBOSE_LOGS + fprintf(fp, "INT["); +#endif + fprintf(fp, "%ld", as_int(lisp)); +#if VERBOSE_LOGS + fprintf(fp, "]"); +#endif + break; + case TAG_SYM: +#if VERBOSE_LOGS + fprintf(fp, "SYM["); +#endif + fprintf(fp, "%s", as_sym(lisp)); +#if VERBOSE_LOGS + fprintf(fp, "]"); +#endif + break; + case TAG_CONS: + { +#if VERBOSE_LOGS + fprintf(fp, "LIST["); +#else + fprintf(fp, "("); +#endif + for (; lisp; lisp = CDR(lisp)) + { + if (IS_TAG(lisp, CONS)) + { + lisp_t *car = CAR(lisp); + lisp_t *cdr = CDR(lisp); + + lisp_print(fp, car); + if (cdr && !IS_TAG(cdr, CONS)) + { + fprintf(fp, " . "); + } + else if (cdr) + { + fprintf(fp, " "); + } + } + else + { + lisp_print(fp, lisp); + break; + } + } +#if VERBOSE_LOGS + fprintf(fp, "]"); +#else + fprintf(fp, ")"); +#endif + break; + } + case TAG_VEC: + break; + case NUM_TAGS: + break; + } +} + /* Copyright (C) 2025, 2026 Aryadev Chavali * This program is distributed in the hope that it will be useful, but WITHOUT