aboutsummaryrefslogtreecommitdiffstats
path: root/src/bootstrap/main.c
blob: c98f60ca0ddf52f1fe9287a4a26f2963a6a5b392 (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
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

void
process_input(FILE *file) {
    // TODO: Implement.
    getchar();
    (void)file;
}

#define REPL_PROMPT "bdl> "

void
run_repl(void) {
    printf("BDL REPL (Press Ctrl-C to exit)\n");
    while (true) {
        printf(REPL_PROMPT);
        process_input(stdin);
    }
}

void
run_file(char *file_name) {
    FILE *fd = fopen(file_name, "r");
    if (!fd) {
        fprintf(stderr, "error: couldn't open input file: %s\n", file_name);
        exit(EXIT_FAILURE);
    }
    process_input(fd);
    fclose(fd);
}

#ifndef BIN_NAME
#define BIN_NAME "bdl"
#endif

void
print_usage(void) {
    printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME);
    printf("\n");
    printf("\t-i\tInteractive mode (REPL).\n");
    printf("\n");
}

int
main(int argc, char *argv[]) {
    int option;
    while ((option = getopt(argc, argv, "i")) != -1) {
        switch (option) {
            case 'i': {
                // Interactive mode.
                run_repl();
                return EXIT_SUCCESS;
            } break;
            default: {
                print_usage();
                return EXIT_FAILURE;
            } break;
        }
    }

    // TODO: Run from stdin if no file is given.

    // Run from file.
    if (optind != argc - 1) {
        fprintf(stderr, "%s: No input file given.\n", BIN_NAME);
        print_usage();
        return EXIT_FAILURE;
    }
    char *file_name = argv[optind];
    run_file(file_name);

    return EXIT_SUCCESS;
}