From 96d27c2a3e1a0fa0878beb3f9cd02f4b4ed8fdbb Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 8 Oct 2021 10:25:59 +0200 Subject: Initial commit w/ small readline echo function --- src/bootstrap/main.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ src/bootstrap/shorthand.h | 37 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100755 src/bootstrap/main.c create mode 100755 src/bootstrap/shorthand.h (limited to 'src/bootstrap') 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 @@ +#include + +#include "shorthand.h" + +typedef struct StringView { + char *start; + size_t n; +} StringView; + +void +sv_write(StringView sv) { + for (size_t i = 0; i < sv.n; i++) { + putchar(sv.start[i]); + } +} + +StringView +read_line(void) { + #define RL_BUF_SIZE 1024 + static char readline_buf[RL_BUF_SIZE]; + + // Clear buffer. + for (size_t i = 0; i < RL_BUF_SIZE; i++) { + readline_buf[i] = 0; + } + + // Barebones readline implementation. + size_t n = 0; + char c; + while ((c = getchar()) != '\n') { + if (c == '\b') { + readline_buf[n] = '\0'; + n--; + } else if (((u8)c >= 0x20 && (u8)c <= 0x7F) && n < RL_BUF_SIZE) { + readline_buf[n] = c; + n++; + } + } + + return (StringView){.start = (char *)&readline_buf, .n = n}; +} + +void +display(StringView sv) { + if (sv.n != 0) { + sv_write(sv); + printf("\n"); + } +} + +#define REPL_PROMPT "bdl> " + +int +main(void) { + printf("BDL REPL (Press Ctrl-C to exit)\n"); + while (true) { + printf(REPL_PROMPT); + display(read_line()); + } + return 0; +} 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 @@ +#ifndef SHORTHAND_H +#define SHORTHAND_H + +#include +#include +#include +#include + +// +// This simple header just typedefs the basic C define types to a shorter name, +// loads the quality of life bool macro for _Bool and defines shorthand macros +// for byte sizes. +// + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef int8_t s8; +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; +typedef volatile u8 vu8; +typedef volatile u16 vu16; +typedef volatile u32 vu32; +typedef volatile u64 vu64; +typedef volatile s8 vs8; +typedef volatile s16 vs16; +typedef volatile s32 vs32; +typedef volatile s64 vs64; + +#define KB(N) ((u64)(N) * 1024) +#define MB(N) ((u64)KB(N) * 1024) +#define GB(N) ((u64)MB(N) * 1024) +#define TB(N) ((u64)GB(N) * 1024) + +#endif // SHORTHAND_H -- cgit v1.2.1