简体   繁体   中英

FORTH writing to a “forth disk”

You have a word in forth called USE which will create a file.

  • USE xxx ( -- )

    Designate OS text file xxx as the "Forth disk."

However, it's not clear how you can write to that FORTH Disk from within an interactive session. There are verbs such as FLUSH and UPDATE but neither of them see to do anything. I'm using gforth. I'm creating words in the session, and using them. I do not understand how a FORTH disk works in this context. It sounds like R's save.image() , except I can't get anything to save. Could you supply a sequence of commands that result in something being written to the argument of USE ?

FORTH was originally designed around the idea of a low-level system with a raw persistent storage system (a 'disk') and NO filesystem -- so no concept of files or folders or anything like that. Instead, you read and write fixed size blocks on the disk, by block number.

Modern FORTH systems (like gforth) have support for filesystems, but ALSO still have support for the low-level raw 'disk' that is accessed by block number. Since gforth usually runs on an OS with a filesystem and no low-level disk access (without superuser permissions), to use the low-level disk block words, you need to give a file to use as the underlying storage for the raw disk blocks -- and that is what the USE word does.


If you want to understand how to use the low-level block I/O words in FORTH, you need to read a forth book about it, but basically, you use BLOCK to read a block into a buffer, UPDATE to mark a buffer as modified, and FLUSH to flush modified buffers to the disk. From the ANSI forth spec, you find:

7.6.1.0800 BLOCK ( u -- a-addr )
a-addr is the address of the first character of the block buffer assigned to mass-storage block u.
An ambiguous condition exists if u is not an available block number.
If block u is already in a block buffer, a-addr is the address of that block buffer.
If block u is not already in memory and there is an unassigned block buffer, transfer block u from mass storage to an unassigned block buffer. a-addr is the address of that block buffer.
If block u is not already in memory and there are no unassigned block buffers, unassign a block buffer.
If the block in that buffer has been UPDATEd, transfer the block to mass storage and transfer block u from mass storage into that buffer. a-addr is the address of that block buffer.
At the conclusion of the operation, the block buffer pointed to by a-addr is the current block buffer and is assigned to u.

7.6.1.2400 UPDATE ( -- )
Mark the current block buffer as modified. An ambiguous condition exists if there is no current block buffer.
UPDATE does not immediately cause I/O.
See: 7.6.1.0800 BLOCK, 7.6.1.0820 BUFFER, 7.6.1.1559 FLUSH, 7.6.1.2180 SAVE-BUFFERS.

You can write your own words to manipulate blocks. But for the first time you can use simple block editor from the gforth ( https://github.com/forthy42/gforth/blob/master/blocked.fb ). I'm use it in the following way. First you need to load it:

use blocked.fb - this file is use file blocked.fb as forth disk;

1 load - load the vocabulary;

editor - this is change vocabulary to newly created.

Now you can modify file with words defined in vocabulary editor . Here example:

use tmp
0 l
0 t : one-plus-two   1 2 + .  ;
flush

Brief explanation of some words (from blocked.f):

a - goes to marked position

c - moves cursor by n chars

t - goes to line n and inserts

i - inserts

d - deletes marked area

r - replaces marked area

f - search and mark

il - insert a line

dl - delete a line

qx - gives a quick index

nx - gives next index

bx - gives previous index

n - goes to next screen

b - goes to previous screen

l - goes to screen n

v - goes to current screen

s - searches until screen n

y - yank deleted string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM