diff options
Diffstat (limited to 'src/bootstrap/main.c')
-rwxr-xr-x | src/bootstrap/main.c | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index c98f60c..2847397 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c | |||
@@ -3,11 +3,11 @@ | |||
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <stdlib.h> | 4 | #include <stdlib.h> |
5 | 5 | ||
6 | #include "string_view.c" | ||
7 | |||
6 | void | 8 | void |
7 | process_input(FILE *file) { | 9 | process_source(const StringView *source) { |
8 | // TODO: Implement. | 10 | sv_write(source, stdout); |
9 | getchar(); | ||
10 | (void)file; | ||
11 | } | 11 | } |
12 | 12 | ||
13 | #define REPL_PROMPT "bdl> " | 13 | #define REPL_PROMPT "bdl> " |
@@ -17,19 +17,37 @@ run_repl(void) { | |||
17 | printf("BDL REPL (Press Ctrl-C to exit)\n"); | 17 | printf("BDL REPL (Press Ctrl-C to exit)\n"); |
18 | while (true) { | 18 | while (true) { |
19 | printf(REPL_PROMPT); | 19 | printf(REPL_PROMPT); |
20 | process_input(stdin); | 20 | getchar(); |
21 | process_source(NULL); | ||
21 | } | 22 | } |
22 | } | 23 | } |
23 | 24 | ||
24 | void | 25 | void |
25 | run_file(char *file_name) { | 26 | run_file(char *file_name) { |
26 | FILE *fd = fopen(file_name, "r"); | 27 | FILE *file = fopen(file_name, "r"); |
27 | if (!fd) { | 28 | if (!file) { |
28 | fprintf(stderr, "error: couldn't open input file: %s\n", file_name); | 29 | fprintf(stderr, "error: couldn't open input file: %s\n", file_name); |
29 | exit(EXIT_FAILURE); | 30 | exit(EXIT_FAILURE); |
30 | } | 31 | } |
31 | process_input(fd); | 32 | |
32 | fclose(fd); | 33 | // Read entire file into memory. |
34 | fseek(file, 0, SEEK_END); | ||
35 | size_t file_size = ftell(file); | ||
36 | fseek(file, 0, SEEK_SET); | ||
37 | |||
38 | char *source = malloc(file_size + 1); | ||
39 | fread(source, 1, file_size, file); | ||
40 | source[file_size] = 0; | ||
41 | |||
42 | StringView sv = (StringView){ | ||
43 | .start = source, | ||
44 | .n = file_size, | ||
45 | }; | ||
46 | |||
47 | process_source(&sv); | ||
48 | |||
49 | free(source); | ||
50 | fclose(file); | ||
33 | } | 51 | } |
34 | 52 | ||
35 | #ifndef BIN_NAME | 53 | #ifndef BIN_NAME |
@@ -61,16 +79,18 @@ main(int argc, char *argv[]) { | |||
61 | } | 79 | } |
62 | } | 80 | } |
63 | 81 | ||
64 | // TODO: Run from stdin if no file is given. | 82 | // Run from stdin. |
83 | if (optind == argc) { | ||
84 | // TODO: Run from stdin if no file is given. | ||
85 | return EXIT_SUCCESS; | ||
86 | } | ||
65 | 87 | ||
66 | // Run from file. | 88 | // Run from file. |
67 | if (optind != argc - 1) { | 89 | while (optind < argc) { |
68 | fprintf(stderr, "%s: No input file given.\n", BIN_NAME); | 90 | char *file_name = argv[optind]; |
69 | print_usage(); | 91 | run_file(file_name); |
70 | return EXIT_FAILURE; | 92 | optind++; |
71 | } | 93 | } |
72 | char *file_name = argv[optind]; | ||
73 | run_file(file_name); | ||
74 | 94 | ||
75 | return EXIT_SUCCESS; | 95 | return EXIT_SUCCESS; |
76 | } | 96 | } |