From 20c6e495b354457031ffe7a60dc674b2a6a69fa1 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 13 Oct 2026 16:54:12 -0700 Subject: add proper functionality for opening files for reading the overall file API is not yet stable, this is subject to change, but it felt like the time had come for this first step Force-Push: yes Change-Id: Idad271b975748b67b5bb9a82fe22337080447e97 --- language/dynamic/files.e | 31 ++++++++++++++++++++++++++++++- language/dynamic/input.e | 1 - 2 files changed, 30 insertions(+), 2 deletions(-) (limited to 'language/dynamic') diff --git a/language/dynamic/files.e b/language/dynamic/files.e index 4822c2f..e0ef533 100644 --- a/language/dynamic/files.e +++ b/language/dynamic/files.e @@ -150,7 +150,7 @@ ~ (filename, file descriptor) ~ Notice that we deferred this allocation until after we know that opening - ~ the file succeeded. We can't leave it any longer thaan this; + ~ the file succeeded. We can't leave it any longer than this; ~ push-input-buffer will allocate memory, which will overwrite the scratch ~ space. swap allocate-string-if-temporary swap @@ -176,3 +176,32 @@ : current-input-file-name main-input-buffer buffer-filename @ ; + +: open-file-for-reading +~ (filename -- metadata pointer or 0) + dup 0 0 sys-open + dup 0 > { + ." Couldn't open " swap emitstring space + ." because " space . ." ." newline + drop 0 exit + } if + ~ (filename, file descriptor) + + ~ As with include, we deferred this allocation until after successful open. + swap allocate-string-if-temporary swap + + allocate-input-buffer-metadata dup zero-input-buffer-metadata + ~ (filename, file descriptor) + + dup 3unroll swap attach-input-buffer-to-file-descriptor ; + + +~ Note that this doesn't clear data that's already in the buffer. +~ +~ (metadata pointer --) +: close-file + dup buffer-file-descriptor @ sys-close drop + ~ Ignore a failed close. + + buffer-file-descriptor -1 swap ! ; + diff --git a/language/dynamic/input.e b/language/dynamic/input.e index ccff6ca..d3d094e 100644 --- a/language/dynamic/input.e +++ b/language/dynamic/input.e @@ -267,7 +267,6 @@ { drop drop drop } if-else ; - ~ Here we have some imperative code that runs immediately, to initialize ~ some runtime data structures. ~ -- cgit 1.4.1