aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-11 12:16:28 +0200
committerBad Diode <bd@badd10de.dev>2021-10-11 12:16:28 +0200
commit1d0ee825c8b70e9456bebc4bf2bc8366c2e89cbd (patch)
tree8f7d6c4faaba6f6003f4e43a6d316092cc118342
parent14c4d1583236480b9fa7f902e0eb24979549d21d (diff)
downloadbdl-1d0ee825c8b70e9456bebc4bf2bc8366c2e89cbd.tar.gz
bdl-1d0ee825c8b70e9456bebc4bf2bc8366c2e89cbd.zip
Add a minimal read_line implementationv0.1
-rwxr-xr-xsrc/bootstrap/main.c13
-rw-r--r--src/bootstrap/read_line.c32
2 files changed, 42 insertions, 3 deletions
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c
index b5fd821..662831e 100755
--- a/src/bootstrap/main.c
+++ b/src/bootstrap/main.c
@@ -4,6 +4,7 @@
4#include <stdlib.h> 4#include <stdlib.h>
5 5
6#include "string_view.c" 6#include "string_view.c"
7#include "read_line.c"
7 8
8void 9void
9process_source(const StringView *source) { 10process_source(const StringView *source) {
@@ -14,11 +15,17 @@ process_source(const StringView *source) {
14 15
15void 16void
16run_repl(void) { 17run_repl(void) {
17 printf("BDL REPL (Press Ctrl-C to exit)\n"); 18 printf("BDL REPL (Press Ctrl-D or Ctrl-C to exit)\n");
18 while (true) { 19 while (true) {
19 printf(REPL_PROMPT); 20 printf(REPL_PROMPT);
20 getchar(); 21 StringView sv = read_line();
21 process_source(NULL); 22 if (sv.start == NULL) {
23 return;
24 }
25 process_source(&sv);
26 if (sv.n != 0) {
27 printf("\n");
28 }
22 } 29 }
23} 30}
24 31
diff --git a/src/bootstrap/read_line.c b/src/bootstrap/read_line.c
new file mode 100644
index 0000000..7612d05
--- /dev/null
+++ b/src/bootstrap/read_line.c
@@ -0,0 +1,32 @@
1#define RL_BUF_SIZE 1024
2static char readline_buf[RL_BUF_SIZE];
3
4StringView
5read_line(void) {
6 // Clear buffer.
7 for (size_t i = 0; i < RL_BUF_SIZE; i++) {
8 readline_buf[i] = 0;
9 }
10
11 // Barebones readline implementation.
12 size_t n = 0;
13 char c;
14 while ((c = getchar()) != '\n') {
15 if (c == '\b') {
16 readline_buf[n] = '\0';
17 n--;
18 } else if (c == EOF || c == '\0') {
19 return (StringView){ .start = NULL, .n = 0 };
20 } else if ((c >= ' ' && c <= '~') && n < RL_BUF_SIZE) {
21 readline_buf[n] = c;
22 n++;
23 }
24 }
25
26 StringView sv = (StringView){
27 .start = (char *)&readline_buf,
28 .n = n,
29 };
30 return sv;
31}
32