aboutsummaryrefslogtreecommitdiffstats
path: root/src/bootstrap/main.c
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 /src/bootstrap/main.c
parentdc758810b463c1674991601edb0ba41d40831e7a (diff)
downloadbdl-6a833ddc268191bfaea94a1cd896684f1b834ef7.tar.gz
bdl-6a833ddc268191bfaea94a1cd896684f1b834ef7.zip
Modify main function to allow multiple file processing
Diffstat (limited to 'src/bootstrap/main.c')
-rwxr-xr-xsrc/bootstrap/main.c52
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
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}