aboutsummaryrefslogtreecommitdiff
path: root/src/driver.c
blob: 8902a55ed005669deca3a67f01f007d6254f1577 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "interpreter.h"
#include "repl.h"


static int interpret_file(const char *path) {
  context_t context = context_create();
  if (interp_file(context, path)) {
    fprintf(stderr, "Error interpreting file: %s\n", path);
    context_free(context);
    return -1;
  }
  context_print_var_table(context);
  context_free(context);
  return 0;
}

int main(int argc, char **argv) {
  int opt;
  while ((opt = getopt(argc, argv, "i:a:h")) != -1) {
    switch (opt) {
    case 'i': 
      interpret_file(optarg);
      return EXIT_SUCCESS;
    case 'a':
      print_ast_file(optarg);
      return EXIT_SUCCESS;
    case 'h':
    default:
      fprintf(stderr, 
        "Usage: %s [ARGS]\n"
        "  (no args)          start REPL\n"
        "  -i <program.imp>   interpret program and exit\n"
        "  -a <program.imp>   print ast\n"
        "  -h                 print this message\n",
        argv[0]);
      return (opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE;
    }
  }
  repl();
  return EXIT_SUCCESS;
}