Eine kleine Forth machine

I’ve written an (incomplete) Forth interpreter on top of kakscript. This was a rather complex effort, which still needs more documenting, and I would like some feedback as time permits (@mawww ?).

To use, clone and symlink under autoload (or use plug https://gitlab.com/kstr0k/kak-stkmach)

After loading as above, select a name and boot a stack machine (multiple machines, with different prefixes, can boot):

require-module stkmach
stkmach-boot ff   # create stack machine 'ff'
buffer *debug*    # output shown in *debug* by default
ff-eval 3 2 + .   # a simple op
echo -debug %opt{ff_builtins}  # show available words
ff-interact       # interactive Forth-ish buffer, use <a-ret>

Forth is a stack-oriented language. This implementation can do

  • RPN arithmetic: ff-eval 3 2 + 4 * . # =20; '.' means print
  • loops: ff-eval 10 0 do i . loop # i = current counter
  • define “words”: ff-eval : sqr dup * \; # duplicate, square, terminate def with \;
    • 3 sqr sqr . # 81 # use def to square repeatedly
  • drive Kakoune (extension): ff-eval 10 0 do i '' %{echo -debug} .kak-eval-top loop

It also showcases kakscript intended and un-intended computability, and is a good way to benchmark the kakoune C++ implentation. I have found some expected, but also some surprising slowdowns.

As an extension to Forth, strings can be placed on the stack using an empty-string ('') command, and be operated on (.str+, .str=/.str<>)

Forth resources

5 Likes