diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2025-08-28 22:55:41 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2025-08-28 22:55:41 +0100 |
commit | cc56a2ee2b5703f9ea5ac63a86870af188845c30 (patch) | |
tree | 2660c03c1c90ad069d44e29af4365bca572926aa /alisp.h | |
parent | 66c64007317cf5b646c1f816463f4bc730e3b99f (diff) | |
download | alisp-cc56a2ee2b5703f9ea5ac63a86870af188845c30.tar.gz alisp-cc56a2ee2b5703f9ea5ac63a86870af188845c30.tar.bz2 alisp-cc56a2ee2b5703f9ea5ac63a86870af188845c30.zip |
Still got some failures, but a basic stream implementation
Need to fix what's going on with the example in main.c using stdin.
Diffstat (limited to 'alisp.h')
-rw-r--r-- | alisp.h | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -17,14 +17,18 @@ #include <assert.h> #include <stdalign.h> +#include <stdbool.h> #include <stddef.h> #include <stdint.h> +#include <stdio.h> /// The bare fucking minimum #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define ARRSIZE(A) (sizeof(A) / sizeof((A)[0])) #define NTH_BYTE(X, N) (((X) >> (8 * N)) & ((1 << 8) - 1)) +#define FAIL(MSG) assert(false && "FAIL: " #MSG) +#define TODO(MSG) assert(false && "TODO: " #MSG) typedef uint8_t u8; typedef uint16_t u16; @@ -91,6 +95,61 @@ void sym_table_init(sym_table_t *); char *sym_table_find(sym_table_t *, sv_t); void sym_table_cleanup(sym_table_t *); +/// Streams +typedef enum +{ + STREAM_TYPE_STRING, + STREAM_TYPE_FILE, +} stream_type_t; + +typedef enum +{ + STREAM_ERR_INVALID_PTR = -4, + STREAM_ERR_FILE_NONEXISTENT = -3, + STREAM_ERR_FILE_READ = -2, + STREAM_ERR_PIPE_NONEXISTENT = -1, + STREAM_ERR_OK = 0, +} stream_err_t; + +typedef struct +{ + vec_t cache; + FILE *file; +} stream_pipe_t; + +typedef struct +{ + stream_type_t type; + char *name; + u64 position; + union + { + sv_t string; + stream_pipe_t pipe; + }; +} stream_t; + +#define STREAM_DEFAULT_CHUNK 64 + +stream_err_t stream_init_string(stream_t *, char *, sv_t); +stream_err_t stream_init_file(stream_t *, char *, FILE *); +void stream_stop(stream_t *); + +// end of stream +bool stream_eos(stream_t *); +// end of cache +bool stream_eoc(stream_t *); +u64 stream_size(stream_t *); + +bool stream_chunk(stream_t *); +char stream_next(stream_t *); +char stream_peek(stream_t *); +bool stream_seek(stream_t *, i64); +bool stream_seek_forward(stream_t *, u64); +bool stream_seek_backward(stream_t *, u64); +sv_t stream_substr(stream_t *, u64); +sv_t stream_substr_abs(stream_t *, u64, u64); + /// Basic defintions for a Lisp #define NIL 0 |