Retro is a concatenative, stack based language with roots in Forth.

It is designed to be small, easily learned, and easily modified to meet specific needs, it has been developed and refined through continual use by a small community over the last decade.


This blog is written in Retro and has served as my primary means of posting things concerning Retro since 2010. The core code for Corpse is included in the Retro releases and can be freely studied and deployed.

The most recent posts are shown below. You can also view a list of all posts.

Post 138 of 215

2011-08-21

Another Alternative to CREATE/DOES>

Traditional Forth dialects provide a CREATE and DOES> pairing to create data structures with associated runtime actions. Retro does not.

Taking a simple case, the creation of a function that returns a specific value:

  : constant ( n"- )  CREATE , DOES> @ ;
  10 constant foo

Retro does not provide a function identical to this pairing. But it's easy to add one if needed. I had done this back in post 19, but will revisit this now. The original implementation used two functions, one to create the data structure and bind it to an action, and the other to name the resulting function.

The quotation system in Retro is pretty nice to use. In this case, we'll provide a function that takes two quotes: a builder and a consumer. The new approach shown below starts off the same as in post 19, but generates more optimal code.

  : does ( qq"- ) create here [ &do dip ] dip swap curry @last !d->xt &.word reclass ; 

Given this, we can do:

  : constant ( n"- ) [ , ] [ @ ] does ;

The function created by does will be bound to the d->xt field of a dictionary header. In my original take, the name function creates a new function which is used to call the function returned by bind. The new approach avoids this extra bit of code, saving two cells, and an extra call each time it is used.


View comments for this article