aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-08 10:25:59 +0200
committerBad Diode <bd@badd10de.dev>2021-10-08 10:25:59 +0200
commit96d27c2a3e1a0fa0878beb3f9cd02f4b4ed8fdbb (patch)
tree9ec40c0c37630d1b6a7546cc8c9f09fe65809861 /src
downloadbdl-96d27c2a3e1a0fa0878beb3f9cd02f4b4ed8fdbb.tar.gz
bdl-96d27c2a3e1a0fa0878beb3f9cd02f4b4ed8fdbb.zip
Initial commit w/ small readline echo function
Diffstat (limited to 'src')
-rwxr-xr-xsrc/bootstrap/main.c61
-rwxr-xr-xsrc/bootstrap/shorthand.h37
2 files changed, 98 insertions, 0 deletions
diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c
new file mode 100755
index 0000000..861c206
--- /dev/null
+++ b/src/bootstrap/main.c
@@ -0,0 +1,61 @@
1#include <stdio.h>
2
3#include "shorthand.h"
4
5typedef struct StringView {
6 char *start;
7 size_t n;
8} StringView;
9
10void
11sv_write(StringView sv) {
12 for (size_t i = 0; i < sv.n; i++) {
13 putchar(sv.start[i]);
14 }
15}
16
17StringView
18read_line(void) {
19 #define RL_BUF_SIZE 1024
20 static char readline_buf[RL_BUF_SIZE];
21
22 // Clear buffer.
23 for (size_t i = 0; i < RL_BUF_SIZE; i++) {
24 readline_buf[i] = 0;
25 }
26
27 // Barebones readline implementation.
28 size_t n = 0;
29 char c;
30 while ((c = getchar()) != '\n') {
31 if (c == '\b') {
32 readline_buf[n] = '\0';
33 n--;
34 } else if (((u8)c >= 0x20 && (u8)c <= 0x7F) && n < RL_BUF_SIZE) {
35 readline_buf[n] = c;
36 n++;
37 }
38 }
39
40 return (StringView){.start = (char *)&readline_buf, .n = n};
41}
42
43void
44display(StringView sv) {
45 if (sv.n != 0) {
46 sv_write(sv);
47 printf("\n");
48 }
49}
50
51#define REPL_PROMPT "bdl> "
52
53int
54main(void) {
55 printf("BDL REPL (Press Ctrl-C to exit)\n");
56 while (true) {
57 printf(REPL_PROMPT);
58 display(read_line());
59 }
60 return 0;
61}
diff --git a/src/bootstrap/shorthand.h b/src/bootstrap/shorthand.h
new file mode 100755
index 0000000..6fcb82c
--- /dev/null
+++ b/src/bootstrap/shorthand.h
@@ -0,0 +1,37 @@
1#ifndef SHORTHAND_H
2#define SHORTHAND_H
3
4#include <assert.h>
5#include <stdbool.h>
6#include <stddef.h>
7#include <stdint.h>
8
9//
10// This simple header just typedefs the basic C define types to a shorter name,
11// loads the quality of life bool macro for _Bool and defines shorthand macros
12// for byte sizes.
13//
14
15typedef uint8_t u8;
16typedef uint16_t u16;
17typedef uint32_t u32;
18typedef uint64_t u64;
19typedef int8_t s8;
20typedef int16_t s16;
21typedef int32_t s32;
22typedef int64_t s64;
23typedef volatile u8 vu8;
24typedef volatile u16 vu16;
25typedef volatile u32 vu32;
26typedef volatile u64 vu64;
27typedef volatile s8 vs8;
28typedef volatile s16 vs16;
29typedef volatile s32 vs32;
30typedef volatile s64 vs64;
31
32#define KB(N) ((u64)(N) * 1024)
33#define MB(N) ((u64)KB(N) * 1024)
34#define GB(N) ((u64)MB(N) * 1024)
35#define TB(N) ((u64)GB(N) * 1024)
36
37#endif // SHORTHAND_H