diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 92 |
1 files changed, 92 insertions, 0 deletions
@@ -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 <https://www.gnu.org/licenses/>. + + * 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 <assert.h> +#include <ctype.h> +#include <malloc.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +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 = "<example>"; + 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; +} |