aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: ab1864d4acad61c3bf1ae5e4401396b3581e5d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
}