From 36dcd4675e24764f80148c19397f82e4ad440602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kochma=C5=84ski?= Date: Wed, 21 May 2025 09:31:07 +0200 Subject: [PATCH] [nucl] add a mock stream --- src/c/Makefile.in | 3 +- src/c/nucl.c | 21 ++++++ src/c/streams/strm_nucl.c | 141 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/c/streams/strm_nucl.c diff --git a/src/c/Makefile.in b/src/c/Makefile.in index 05d653ea5..16547299a 100644 --- a/src/c/Makefile.in +++ b/src/c/Makefile.in @@ -55,7 +55,8 @@ NUCL_CFLG = -DECL_NUCL -DECL_BUILD -DGC_NO_THREAD_REDIRECTS \ -I$(builddir) -I$(srcdir) -g3 -rdynamic NUCL_SRCS = boot.c escape.c module.c stacks.c eql.c \ - memory.c atomic.c process.c apply.c interpreter.c + memory.c atomic.c process.c apply.c interpreter.c stream.c \ + streams/strm_nucl.c BOOT_OBJS = boot.o escape.o eql.o module.o stacks.o diff --git a/src/c/nucl.c b/src/c/nucl.c index ca546d03a..63971523d 100644 --- a/src/c/nucl.c +++ b/src/c/nucl.c @@ -80,6 +80,18 @@ void smoke_bytecodes (void) ecl_stack_frame_close(f); } +cl_object ecl_make_stub_stream(void); +cl_object ecl_make_nucl_stream(void); + +void +smoke_stream (void) +{ + cl_object strm = ecl_make_nucl_stream(); + printf(">>> smoke_stream: stream is %p\n", strm); + si_read_char(strm, ECL_NIL, ECL_NIL); + si_write_char(ECL_CODE_CHAR('c'), strm); +} + int main() { cl_env_ptr the_env = ecl_core.first_env; ecl_boot(); @@ -102,10 +114,19 @@ int main() { nucl_flamethrower(0); printf("-----------------------------------------------\n\n"); + /* Just install the handler. */ + cl_object handlers = ecl_cons_stack(_nucl_extinguisher, ECL_NIL); + ECL_SETQ(the_env, ECL_SIGNAL_HANDLERS, handlers); + printf("\n[:bytecodes t] --------------------------------\n"); smoke_bytecodes(); printf("-----------------------------------------------\n\n"); + printf("\n[:stream t] --------------------------------\n"); + smoke_stream(); + printf("-----------------------------------------------\n\n"); + + printf("Good bye ECL! %p\n", the_env); ecl_halt(); diff --git a/src/c/streams/strm_nucl.c b/src/c/streams/strm_nucl.c new file mode 100644 index 000000000..7f07c7ab1 --- /dev/null +++ b/src/c/streams/strm_nucl.c @@ -0,0 +1,141 @@ +/* -- imports ------------------------------------------------------- */ + +#include +#include +#include +#include +#include + +cl_index +not_implemented_byte8(cl_object strm, unsigned char *c, cl_index n) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); + return 0; +} + +static cl_index +not_implemented_vector(cl_object strm, cl_object data, cl_index start, cl_index end) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); + return 0; +} + +static void +not_implemented_writer(cl_object strm, cl_object c) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); +} + +static void +not_implemented_option(cl_object strm) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); +} + +static cl_object +not_implemented_setter(cl_object strm, cl_object val) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); +} + +static cl_object +not_implemented_reader(cl_object strm) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); + return ECL_NIL; +} + +static int +not_implemented_reader_raw(cl_object strm) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); + return 0; +} + +static int +not_implemented_writer_raw(cl_object strm, int c) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); + return 0; +} + +static void +not_implemented_unread_raw(cl_object strm, int c) +{ + ecl_ferror(ECL_EX_NIY, ECL_NIL, ECL_NIL); +} + +struct ecl_file_ops stub_io_ops = { + /* Used to implement encodings */ + .write_byte8 = not_implemented_byte8, + .read_byte8 = not_implemented_byte8, + /* Binary I/O */ + .write_byte = not_implemented_writer, + .read_byte = not_implemented_reader, + /* String I/O */ + .read_char = not_implemented_reader_raw, + .write_char = not_implemented_writer_raw, + .unread_char = not_implemented_unread_raw, + .peek_char = not_implemented_reader_raw, + /* Used to implement r/w sequence */ + .read_vector = not_implemented_vector, + .write_vector = not_implemented_vector, + /* Stream operations */ + .listen = not_implemented_reader_raw, + .clear_input = not_implemented_option, + .clear_output = not_implemented_option, + .finish_output = not_implemented_option, + .force_output = not_implemented_option, + /* Stream appraisal */ + .input_p = not_implemented_reader_raw, + .output_p = not_implemented_reader_raw, + .interactive_p = not_implemented_reader_raw, + .element_type = not_implemented_reader, + /* Cursor operations */ + .length = not_implemented_reader, + .get_position = not_implemented_reader, + .set_position = not_implemented_setter, + .string_length = not_implemented_setter, + .column = not_implemented_reader_raw, + /* File stream readers */ + .pathname = not_implemented_reader, + .truename = not_implemented_reader, + /* Closing the stream (generic_close replaces the dispatch table) */ + .close = not_implemented_reader, +}; + +cl_object +ecl_make_stub_stream(void) +{ + cl_object strm = ecl_alloc_stream(); + strm->stream.ops = &stub_io_ops; + strm->stream.mode = ecl_smm_other; + return strm; +} + +static int +nucl_read_char(cl_object strm) +{ + return 'X'; +} + + +static int +nucl_write_char(cl_object strm, ecl_character c) +{ + printf("Character: %c\n", c); + return c; +} + +cl_object +ecl_make_nucl_stream(void) +{ + cl_object strm = ecl_alloc_stream(); + strm->d.t = t_stream; /* hum */ + struct ecl_file_ops *ops = ecl_duplicate_dispatch_table(&stub_io_ops); + ops->read_char = nucl_read_char; + ops->write_char = nucl_write_char; + strm->stream.ops = ops; + strm->stream.mode = ecl_smm_other; + return strm; +}