diff options
author | Bad Diode <bd@badd10de.dev> | 2021-10-29 15:37:28 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-10-29 15:37:28 +0200 |
commit | e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de (patch) | |
tree | c44721b005b7a0623e7acc7103ca8e21a25ff422 /src/main.c | |
parent | fcc131afdd029c606ea39f3557bc3d33a075b1de (diff) | |
download | bdl-e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de.tar.gz bdl-e73a4c16a2269cdb2f5e7d66fb9839e4c44e14de.zip |
Prepare third compiler implementation
Diffstat (limited to 'src/main.c')
-rwxr-xr-x | src/main.c | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100755 index 0000000..90860e8 --- /dev/null +++ b/src/main.c | |||
@@ -0,0 +1,137 @@ | |||
1 | #include <getopt.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdlib.h> | ||
4 | |||
5 | #include "common.h" | ||
6 | #include "darray.h" | ||
7 | #include "string_view.c" | ||
8 | #include "errors.c" | ||
9 | #include "lexer.c" | ||
10 | |||
11 | void | ||
12 | init(void) { | ||
13 | // STUB | ||
14 | } | ||
15 | |||
16 | void | ||
17 | halt(void) { | ||
18 | // STUB | ||
19 | } | ||
20 | |||
21 | void | ||
22 | process_source(const StringView *source, const char *file_name) { | ||
23 | // Read tokens. | ||
24 | Tokens tokens = tokenize(source); | ||
25 | if (tokens.errors.n != 0) { | ||
26 | report_errors(&tokens.errors, file_name); | ||
27 | exit(EXIT_FAILURE); | ||
28 | } | ||
29 | |||
30 | // TODO: Parser. | ||
31 | // TODO: Semantic analysis. | ||
32 | // TODO: Optimization. | ||
33 | // TODO: Compilation. | ||
34 | |||
35 | // Free resources. | ||
36 | array_free(tokens.tokens); | ||
37 | } | ||
38 | |||
39 | void | ||
40 | run_file(char *file_name) { | ||
41 | FILE *file = fopen(file_name, "r"); | ||
42 | if (!file) { | ||
43 | fprintf(stderr, "error: couldn't open input file: %s\n", file_name); | ||
44 | exit(EXIT_FAILURE); | ||
45 | } | ||
46 | |||
47 | // Read entire file into memory. | ||
48 | fseek(file, 0, SEEK_END); | ||
49 | size_t file_size = ftell(file); | ||
50 | fseek(file, 0, SEEK_SET); | ||
51 | |||
52 | char *source = malloc(file_size + 1); | ||
53 | fread(source, 1, file_size, file); | ||
54 | source[file_size] = 0; | ||
55 | |||
56 | StringView sv = (StringView){ | ||
57 | .start = source, | ||
58 | .n = file_size, | ||
59 | }; | ||
60 | |||
61 | process_source(&sv, file_name); | ||
62 | |||
63 | free(source); | ||
64 | fclose(file); | ||
65 | } | ||
66 | |||
67 | #define STDIN_BUF_CAP 16 | ||
68 | |||
69 | void | ||
70 | run_stdin(void) { | ||
71 | size_t buf_size = 0; | ||
72 | char *source = NULL; | ||
73 | array_init(source, STDIN_BUF_CAP); | ||
74 | |||
75 | char c; | ||
76 | while ((c = getchar()) != EOF) { | ||
77 | array_push(source, c); | ||
78 | buf_size++; | ||
79 | } | ||
80 | |||
81 | StringView sv = (StringView){ | ||
82 | .start = source, | ||
83 | .n = buf_size, | ||
84 | }; | ||
85 | |||
86 | process_source(&sv, "stdin"); | ||
87 | |||
88 | array_free(source); | ||
89 | } | ||
90 | |||
91 | #ifndef BIN_NAME | ||
92 | #define BIN_NAME "bdl" | ||
93 | #endif | ||
94 | |||
95 | void | ||
96 | print_usage(void) { | ||
97 | printf("Usage: %s [options] <filename filename ...>\n", BIN_NAME); | ||
98 | printf("\n"); | ||
99 | printf("\t-h \tShow usage.\n"); | ||
100 | printf("\n"); | ||
101 | } | ||
102 | |||
103 | int | ||
104 | main(int argc, char *argv[]) { | ||
105 | init(); | ||
106 | |||
107 | int option; | ||
108 | while ((option = getopt(argc, argv, "h")) != -1) { | ||
109 | switch (option) { | ||
110 | case 'h': { | ||
111 | print_usage(); | ||
112 | goto exit_success; | ||
113 | } break; | ||
114 | default: { | ||
115 | print_usage(); | ||
116 | return EXIT_FAILURE; | ||
117 | } break; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | // Run from stdin. | ||
122 | if (optind == argc) { | ||
123 | run_stdin(); | ||
124 | goto exit_success; | ||
125 | } | ||
126 | |||
127 | // Run from file. | ||
128 | while (optind < argc) { | ||
129 | char *file_name = argv[optind]; | ||
130 | run_file(file_name); | ||
131 | optind++; | ||
132 | } | ||
133 | |||
134 | exit_success: | ||
135 | halt(); | ||
136 | return EXIT_SUCCESS; | ||
137 | } | ||