diff options
Diffstat (limited to 'src/bootstrap')
-rwxr-xr-x | src/bootstrap/main.c | 61 | ||||
-rwxr-xr-x | src/bootstrap/shorthand.h | 37 |
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 | |||
5 | typedef struct StringView { | ||
6 | char *start; | ||
7 | size_t n; | ||
8 | } StringView; | ||
9 | |||
10 | void | ||
11 | sv_write(StringView sv) { | ||
12 | for (size_t i = 0; i < sv.n; i++) { | ||
13 | putchar(sv.start[i]); | ||
14 | } | ||
15 | } | ||
16 | |||
17 | StringView | ||
18 | read_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 | |||
43 | void | ||
44 | display(StringView sv) { | ||
45 | if (sv.n != 0) { | ||
46 | sv_write(sv); | ||
47 | printf("\n"); | ||
48 | } | ||
49 | } | ||
50 | |||
51 | #define REPL_PROMPT "bdl> " | ||
52 | |||
53 | int | ||
54 | main(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 | |||
15 | typedef uint8_t u8; | ||
16 | typedef uint16_t u16; | ||
17 | typedef uint32_t u32; | ||
18 | typedef uint64_t u64; | ||
19 | typedef int8_t s8; | ||
20 | typedef int16_t s16; | ||
21 | typedef int32_t s32; | ||
22 | typedef int64_t s64; | ||
23 | typedef volatile u8 vu8; | ||
24 | typedef volatile u16 vu16; | ||
25 | typedef volatile u32 vu32; | ||
26 | typedef volatile u64 vu64; | ||
27 | typedef volatile s8 vs8; | ||
28 | typedef volatile s16 vs16; | ||
29 | typedef volatile s32 vs32; | ||
30 | typedef 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 | ||