How can I create a linked list in gforth

29 Views Asked by At

I asked Bing how I could create a linked list in gforth and it gave me the following example:

Here is an example of how to create a linked list in Gforth:

: create-node ( value -- )
  HERE 8 ALLOT
  LITERAL
  DUP @ 2 + !
;

This code defines a new word called create-node that takes a value from the stack and creates a new node with that value. The HERE word allocates memory for the new node, and the 8 ALLOT word reserves space for the node’s value and reference. The LITERAL word stores the value in the node, and the DUP @ 2 + ! word stores the address of the new node in the reference of the previous node 1.

However, when I tried this out, I got the following:

in file included from OS command line:-1

linked.fs:5: unstructured

--> ; <--

Backtrace:

$7FDA17AAD988 throw

$7FDA17ABD478 c(abort")

$7FDA17ACA4A0 def?

$7FDA17AB6858 ;-hook

Testing showed that the word LITERAL was causing the error.

I know I am missing something, but as a relative newcomer to forth, I am at a loss to figure out what it is. I looked up Literal in the gforth manual, and it said:

Literal ( compilation n – ; run-time – n ) core “Literal” Compilation semantics: compile the run-time semantics. Run-time Semantics: push n. Interpretation semantics: undefined.

I'm afraid this explanation is opaque to me.

0

There are 0 best solutions below