From a8406927ef63267d800b92bf817b8347aa185dde Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 19 Feb 2020 12:22:00 +1100 Subject: [PATCH] Add io_reader.is_closed --- example/jsonptr/jsonptr.cc | 15 +++++++++------ internal/cgen/builtin.go | 4 ++++ lang/builtin/builtin.go | 1 + lang/token/list.go | 16 +++++++++------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index 39100489..1b47bd2a 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -93,6 +93,9 @@ size_t indent; // ---- const char* read_src() { + if (src.meta.closed) { + return "main: read error: unexpected EOF"; + } src.compact(); if (src.meta.wi >= src.data.len) { return "main: src buffer is full"; @@ -100,12 +103,12 @@ const char* read_src() { size_t n = fread(src.data.ptr + src.meta.wi, sizeof(uint8_t), src.data.len - src.meta.wi, stdin); src.meta.wi += n; - if (n == 0) { - if (feof(stdin)) { - return "main: read error: unexpected EOF"; - } else { - return "main: read error"; - } + if (n > 0) { + // No-op. + } else if (feof(stdin)) { + src.meta.closed = true; + } else { + return "main: read error"; } return nullptr; } diff --git a/internal/cgen/builtin.go b/internal/cgen/builtin.go index fe0b7d5f..196ef8d9 100644 --- a/internal/cgen/builtin.go +++ b/internal/cgen/builtin.go @@ -160,6 +160,10 @@ func (g *gen) writeBuiltinIOReader(b *buffer, recv *a.Expr, method t.ID, args [] b.printf(", ((uint64_t)(%s%s - %s%s)))", iopPrefix, name, io0Prefix, name) return nil + case t.IDIsClosed: + b.printf("(%s && %s->meta.closed)", name, name) + return nil + case t.IDMark: b.printf("((uint64_t)(%s%s - %s%s))", iopPrefix, name, io0Prefix, name) return nil diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index b4e9d7eb..ac51c33c 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -249,6 +249,7 @@ var Funcs = []string{ "io_reader.available() u64", "io_reader.count_since(mark: u64) u64", + "io_reader.is_closed() bool", "io_reader.mark() u64", "io_reader.position() u64", "io_reader.since(mark: u64) slice u8", diff --git a/lang/token/list.go b/lang/token/list.go index 552e8f3b..d16773bd 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -491,13 +491,14 @@ const ( IDCanUndoByte = ID(0x160) IDCountSince = ID(0x161) IDHistoryAvailable = ID(0x162) - IDMark = ID(0x163) - IDPosition = ID(0x164) - IDSince = ID(0x165) - IDSkip = ID(0x166) - IDSkip32 = ID(0x167) - IDSkip32Fast = ID(0x168) - IDTake = ID(0x169) + IDIsClosed = ID(0x163) + IDMark = ID(0x164) + IDPosition = ID(0x165) + IDSince = ID(0x166) + IDSkip = ID(0x167) + IDSkip32 = ID(0x168) + IDSkip32Fast = ID(0x169) + IDTake = ID(0x16A) IDCopyFromSlice = ID(0x170) IDCopyN32FromHistory = ID(0x171) @@ -841,6 +842,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDCanUndoByte: "can_undo_byte", IDCountSince: "count_since", IDHistoryAvailable: "history_available", + IDIsClosed: "is_closed", IDMark: "mark", IDPosition: "position", IDSince: "since",