hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit f62b8cb02d3991e2bc51e8d064f3d85b2b8da6d4
parent af5f3cf5b78efefca4bc1ffe848fd2f9eb185605
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 13 Feb 2021 12:42:30 -0500

io: add io::empty

Also drops the temporary bufio implementation

Diffstat:
Dbufio/fixed.ha | 32--------------------------------
Mhare/lex/lex.ha | 5++---
Mio/stream.ha | 12++++++++++++
3 files changed, 14 insertions(+), 35 deletions(-)

diff --git a/bufio/fixed.ha b/bufio/fixed.ha @@ -1,32 +0,0 @@ -use io; -use rt; - -// XXX: All of this is temporary -export type fixed_stream = struct { - stream: io::stream, - buf: []u8, -}; - -export fn fixed(in: []u8) *io::stream = { - let s = alloc(*fixed_stream, fixed_stream { - stream = io::stream { - name = "<bufio::fixed>", - reader = &fixed_read, - ... - }, - buf = in, - }); - return &s.stream; -}; - -fn fixed_read(s: *io::stream, buf: []u8) (size | io::error | io::EOF) = { - let stream = s: *fixed_stream; - if (len(stream.buf) == 0) { - return io::EOF; - }; - const n = if (len(buf) > len(stream.buf)) len(stream.buf) else len(buf); - // TODO: Fix me up once slice copying is in - rt::memcpy(buf: *[*]u8, stream.buf: *[*]u8, n); - stream.buf = stream.buf[n..]; - return n; -}; diff --git a/hare/lex/lex.ha b/hare/lex/lex.ha @@ -1,5 +1,4 @@ // hare::lex provides a lexer for Hare source code. -use bufio; use io; use strings; use types; @@ -68,7 +67,7 @@ fn unget(lex: *lexer, r: (rune | io::EOF)) void = { }; @test fn unget() void = { - let lexer = lexer_init(bufio::fixed([]), "<test>"); + let lexer = lexer_init(io::empty, "<test>"); unget(&lexer, 'x'); unget(&lexer, 'y'); assert(next(&lexer) as rune == 'y'); @@ -77,7 +76,7 @@ fn unget(lex: *lexer, r: (rune | io::EOF)) void = { }; @test fn unlex() void = { - let lexer = lexer_init(bufio::fixed([]), "<test>"); + let lexer = lexer_init(io::empty, "<test>"); unlex(&lexer, (base_token::IF, location { path = "<test>", start = linecol { line = 1234, col = 1234 }, diff --git a/io/stream.ha b/io/stream.ha @@ -69,3 +69,15 @@ export fn tell(s: *stream) (off | error) = { sk: *seeker => sk(s, 0, whence::CUR), }; }; + +// A [stream] which always returns EOF. +export let empty: *io::stream = null: *io::stream; + +// TODO: This can be removed when globals can reference other globals +@init fn init() void = { + static let s = io::stream { ... }; + s.reader = &empty_read; + empty = &s; +}; + +fn empty_read(s: *stream, buf: []u8) (size | EOF | error) = EOF;