Arrays in Forth

Arrays in Forth Arrays in Forth A natural question that beginners often ask is: Why doesn’t Forth have features that are standard in other languages, for example, arrays? The answer is that Forth is so facile at creating new data types that it is usually easier to invent something that exactly suits your needs than it is to force your program to conform to an arbitrary standard. Indexed and unindexed arrays The term array in Forth has come to signify two different kinds of structures, which I distinguish by calling them unindexed and indexed. An unindexed array allots a specified number of bytes at compile-time and returns the address of its origin at run-time. It is not really an array, in my opinion, but a work area or buffer. The defining word ARRAY in F-PC is of this type. Here it is, suitably renamed, with an example: : unindexed-array ( n — ) ( — a) create allot ; 80 unindexed-array u-foo \ Make an 80-byte unindexed array u-foo \ Return the origin addr of u-foo An indexed array regards the area as divided into elements of equal length. It replaces an integer n at the top of the stack by the address of the nth element. “Variable-like” and other kinds of indexed arrays I call an indexed array variable-like if it returns an address. One can also have value-like arrays that return their contents, execution arrays (“vectors”) that perform the contained action, and mixed arrays composed of several of these types. Many Forth implementations provide a variable-like indexed array called simply array . It creates a 1-dimension variable-like array with elements one cell in length. Most programmers think of this when “array” is mentioned, and may even assume that it is standard. Its popularity is warranted, as it is quick and simple. Note that using 3 as the index returns the fourth element, because numbering conventionally starts with zero in Forth. If you wish, you can change the definition so that 1 becomes the first element, but it’s probably better if, for example, you want an array of size 100, to define it with 101 elements and ignore the first one. : array ( n — ) ( i — addr) create cells allot does> cells + ; 100 array foo \ Make an array with 100 cells 3 foo \ Return address of fourth element Remember, zero is the first element. Comment by Mike Condron – 08/07/2019: The example for : array I think needs a “SWAP” as the first word after DOES> : array ( n — ) ( i — addr) create cells allot does> swap cells + ; 100 array foo \ Make an array with 100 cells 3 foo \ Return address of fourth element Otherwise, as written without the swap, cells is getting the address of the data field of the array object, not the index. A flexible array The elements of array are limited to a single cell in length. This is not adequate even for our first adventure game, Game 0, which will need five cells to store data for each room: one for the room descriptor, and four for the destinations. The defining word long-element-array will create variable-like arrays

Source: Hacker News | Original Link