aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-11 11:20:38 +0200
committerBad Diode <bd@badd10de.dev>2021-10-11 11:20:38 +0200
commit6a833ddc268191bfaea94a1cd896684f1b834ef7 (patch)
treea3049a46a338c442f66ec94a36313145f57011d7
parentdc758810b463c1674991601edb0ba41d40831e7a (diff)
downloadbdl-6a833ddc268191bfaea94a1cd896684f1b834ef7.tar.gz
bdl-6a833ddc268191bfaea94a1cd896684f1b834ef7.zip
Modify main function to allow multiple file processing
-rwxr-xr-xsrc/bootstrap/main.c52
-rw-r--r--src/bootstrap/string_view.c43
2 files changed, 79 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
6void 8void
7process_input(FILE *file) { 9process_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
24void 25void
25run_file(char *file_name) { 26run_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}
diff --git a/src/bootstrap/string_view.c b/src/bootstrap/string_view.c
new file mode 100644
index 0000000..13ba9e0
--- /dev/null
+++ b/src/bootstrap/string_view.c
@@ -0,0 +1,43 @@
1typedef struct StringView {
2 char *start;
3 size_t n;
4} StringView;
5
6char
7sv_next(StringView *sv) {
8 if (sv->n == 0) {
9 return '\0';
10 }
11 char c = sv->start[0];
12 sv->start++;
13 sv->n--;
14 return c;
15}
16
17char
18sv_peek(const StringView *sv) {
19 if (sv->n == 0) {
20 return '\0';
21 }
22 return sv->start[0];
23}
24
25bool
26sv_equal(const StringView *a, const StringView *b) {
27 if (a->n != b->n) {
28 return false;
29 }
30 for (size_t i = 0; i < a->n; i++) {
31 if (a->start[i] != b->start[i]) {
32 return false;
33 }
34 }
35 return true;
36}
37
38void
39sv_write(const StringView *sv, FILE *file) {
40 for (size_t i = 0; i < sv->n; i++) {
41 putc(sv->start[i], file);
42 }
43}