From ba5c0a4579ece5d53c009a14d00e683e70b982f4 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 9 May 2025 18:29:52 +0100 Subject: Initial implementation --- main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..ab1864d --- /dev/null +++ b/main.c @@ -0,0 +1,92 @@ +/* Copyright (C) 2025 Aryadev Chavali + + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for + * details. + + * You may distribute and modify this code under the terms of the GNU General + * Public License Version 2, which you should have received a copy of along with + * this program. If not, please go to . + + * Created: 2025-04-05 + * Description: Entrypoint + */ + +#include "./base.h" +#include "./lisp.h" +#include "./memory.h" +#include "./reader.h" +#include "./sv.h" +#include "./vec.h" + +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + int exit = 0; + context_t ctx = {0}; + const char *filename = NULL; +#if 1 + if (argc > 1) + filename = argv[1]; + else + filename = "./r7rs-tests.scm"; + + FILE *fp = fopen(filename, "r"); + if (!fp) + { + fprintf(stderr, "[ERROR]: File `%s` does not exist\n", filename); + exit = 1; + goto end; + } + input_t inp = {0}; + input_from_fp(&ctx, &inp, filename, fp); + fclose(fp); +#else + filename = ""; + char inp_data[] = "(print (+ 34 35))"; + input_t inp = {0}; + sv_t inp_sv = SV(inp_data, sizeof(inp_data)); + input_from_sv(&ctx, &inp, filename, inp_sv); +#endif + + if (inp.str.size == 0) + { + info("[WARNING] `%s` is empty.\n", filename); + goto end; + } + debug("[file read]: %luB read from `%s`\n", inp.str.size, filename); + + // Setup a vector to hold all the lisps + vec_t results = {0}; + perr_t perr = parse_all(&ctx, &inp, &results); + if (perr) + { + exit = perr; + print_perror(stderr, &inp, perr); + goto end; + } + context_reset_read(&ctx); + + lisp_t **lisps = (lisp_t **)results.data; + u64 size = results.size / sizeof(*lisps); + for (u64 i = 0; i < size; ++i) + { + lisp_t *lisp = lisps[i]; + // printf("tag=%x\n", tag_get(lisp)); + sv_t serialised = serialise(&ctx, lisp); + info("lisp[%lu]: %p => " PR_SV "\n", i, lisp, SV_FMT(serialised)); + } + context_report(&ctx); + +end: + + context_cleanup(&ctx); + return exit; +} -- cgit v1.2.3-13-gbd6f