spl

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8dc620cb218b1a272d5cd1f8a839c341dad06fbe
parent 1a20d3678d76dca73e53ac14f644a7e8e71ec982
Author: Brian Swetland <swetland@frotz.net>
Date:   Thu, 12 Oct 2023 14:38:08 -0700

compiler0: add a primitive io facility

readc(fd)
writec(fd, ch)
writes(fd, str)
writex(fd, num)

Diffstat:
Mcompiler0.c | 22++++++++++++++++++++--
Minc/builtin.type.h | 2++
Minc/library.impl.c | 27+++++++++++++++++++++++++++
Minc/library.impl.h | 5+++++
4 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/compiler0.c b/compiler0.c @@ -386,6 +386,23 @@ void emit_impl(const char *fmt, ...) { } } +void emit_impl_str(void) { + u32 n = 0; + emit_impl("(void*) \""); + while (n < 256) { + u32 ch = ctx.tmp[n]; + if (ch == 0) { + break; + } else if ((ch < ' ') || (ch > '~') || (ch == '"') || (ch == '\\')) { + emit_impl("\\x%02x", ch); + } else { + emit_impl("%c", ch); + } + n++; + } + emit_impl("\""); +} + #define emit_decl(fmt...) emit(DECL, fmt) #define emit_type(fmt...) emit(TYPE, fmt) @@ -606,6 +623,7 @@ token_t scan_string(u32 cc, u32 nc) { u32 n = 0; while (true) { if (nc == '"') { + nc = scan(); break; } else if (nc == 0) { error("unterminated string"); @@ -909,7 +927,7 @@ void parse_primary_expr(void) { if (ctx.tok == tNUM) { emit_impl("0x%x", ctx.num); } else if (ctx.tok == tSTR) { - error("<TODO> string const"); + emit_impl_str(); } else if (ctx.tok == tTRUE) { emit_impl("1"); } else if (ctx.tok == tFALSE) { @@ -1080,7 +1098,7 @@ Type *parse_array_type(void) { } // TODO: slices char tmp[256]; - sprintf(tmp, "array_of_%s", type->of->name->text); + sprintf(tmp, "%s_a", type->of->name->text); type->name = string_make(tmp, strlen(tmp)); emit_type("typedef %s_t %s_t[%u];\n", type->of->name->text, type->name->text, nelem); return type; diff --git a/inc/builtin.type.h b/inc/builtin.type.h @@ -9,3 +9,5 @@ typedef uint16_t u16_t; typedef int16_t i16_t; typedef uint8_t u8_t; typedef int8_t i8_t; + +typedef uint8_t *u8_a_t; diff --git a/inc/library.impl.c b/inc/library.impl.c @@ -1,8 +1,35 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + void fn__hexout_(int x) { printf("D %08x\n", x); } +void fn_writes(int fd, u8_a_t s) { + write(fd, (void*)s, strlen((void*) s)); +} +void fn_writex(int fd, int n) { + char tmp[64]; + sprintf(tmp, "0x%x", n); + write(fd, tmp, strlen(tmp)); +} +void fn_writec(int fd, int n) { + u8_t x = n; + if (write(fd, &x, 1) != 1) {} +} + +int fn_readc(int fd) { + u8_t x; + if (read(fd, &x, 1) == 1) { + return x; + } else { + return -1; + } +} + int main(int argc, char** argv) { int x = fn_start(); printf("X %08x\n", x); diff --git a/inc/library.impl.h b/inc/library.impl.h @@ -1,3 +1,8 @@ #include <stdio.h> void fn__hexout_(i32_t x); + +void fn_writes(i32_t fd, u8_a_t s); +void fn_writex(i32_t fd, i32_t n); +void fn_writec(i32_t fd, i32_t c); +i32_t fn_readc(i32_t fd);