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

#include "interpreter_context.h"
#include "driver.h"
#include "repl.h"


static int interpret_file(const char *path) {
  IMP_InterpreterContext *context = imp_interpreter_context_create();
  if (imp_driver_interpret_file(context, path)) {
    fprintf(stderr, "Error interpreting file: %s\n", path);
    imp_interpreter_context_destroy(context);
    return -1;
  }
  imp_driver_print_var_table(context);
  imp_interpreter_context_destroy(context);
  return 0;
}

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