From 4ec019ba90ff935ab8acc554cd5702c1b28e4272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kochma=C5=84ski?= Date: Thu, 22 May 2025 14:42:00 +0200 Subject: [PATCH] [nucl] add Lali-ho I/O --- src/c/nucl.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/src/c/nucl.c b/src/c/nucl.c index f330b7d79..5bcafa412 100644 --- a/src/c/nucl.c +++ b/src/c/nucl.c @@ -82,17 +82,81 @@ void smoke_bytecodes (void) cl_object ecl_make_nucl_stream(FILE *f); +/* -- Lali-ho I/O starts here ----------------------------------------------- */ +cl_object +nucl_alloc_base_string(cl_index s) +{ + int i; + cl_object x = ecl_alloc_compact_object(t_base_string, s+1); + x->base_string.self = ECL_COMPACT_OBJECT_EXTRA(x); + x->base_string.self[s] = '\0'; + x->base_string.elttype = ecl_aet_bc; + x->base_string.flags = 0; /* no fill pointer, not adjustable */ + x->base_string.displaced = ECL_NIL; + x->base_string.dim = x->base_string.fillp = s; + return x; +} + +cl_object +nucl_write(const char *s, cl_object strm) +{ + while(*s != '\0') + si_write_char(strm, ECL_CODE_CHAR(*s++)); +} + +cl_object +nucl_print(cl_object self, cl_object strm) +{ + cl_type t = ecl_t_of(self); + switch (t) { + case t_base_string: + nucl_write(self->base_string.self, strm); + break; + default: + { + const char *name = ecl_type_info[t].name; + nucl_write("#<", strm); + nucl_write(name, strm); + nucl_write(">", strm); + return ECL_NIL; + } + } +} + +cl_object +nucl_readl(cl_object self, cl_object strm) +{ + cl_object stack = ecl_make_stack(0), c=ECL_NIL; + cl_index idx, size; + while (!Null(c = si_read_char(strm, ECL_NIL))) { + ecl_stack_push(stack, c); + if(ECL_CHAR_CODE(c) == '\n') break; + } + size = stack->vector.fillp; + if(Null(self)) { + self = nucl_alloc_base_string(size); + } else { + self->base_string.self[size] = '\0'; + } + for(idx=0; idxbase_string.self[idx] = ECL_CHAR_CODE(stack->vector.self.t[idx]); + return self; +} +/* -- Lali-ho I/O ends here ------------------------------------------------- */ + void smoke_stream (void) { cl_object ostrm = ecl_make_nucl_stream(stdout); - char *string = "Hello World!\n", c; + cl_object istrm = ecl_make_nucl_stream(stdin); + cl_object line = ECL_NIL; + char *string = "Hello World> ", c; int i; printf(">>> smoke_stream: stream is %p\n", ostrm); - for (i = 0; i<13; i++) { - c = string[i]; - si_write_char(ECL_CODE_CHAR(c), ostrm); - } + nucl_write(string, ostrm); + line = nucl_readl(ECL_NIL, istrm); + nucl_print(line, ostrm); + ecl_dealloc(line); } int main() {