UP | HOME

Support via Liberapay

Forth Code Blocks in Babel

Babel support for Forth

Introduction

Forth is a stack-based language that is able to extend itself during runtime. It is an interactive language with interactive compilation, during which the parser is visible to the programmer.

This Forth tutorial intends "that you work through it while sitting in front of the console, take a look at the examples and predict what they will do, then try them out; if the outcome is not as expected, find out why (e.g., by trying out variations of the example), so you understand what's going on."

Requirements and Setup

Babel support for Forth code blocks requires a working installation of the gforth compiler. Other Forth Systems are not supported by Babel.

The gforth compiler is distributed with the file gforth.el, which provides forth-mode, the official Emacs programming language mode for Forth. Note that Babel does not support other implementations of forth-mode, such as this implementation distributed via MELPA.

To configure Forth code blocks in Org mode, add the appropriate dotted pair to org-babel-load-languages:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((forth . t))) ; this line activates Forth

Babel evaluates Forth code blocks in an inferior Forth process, which must be running when the code block is evaluated. You can start an inferior Forth process with M-x run-forth.

Babel Features for Forth Code Blocks

Babel supports Forth sessions, which run in a buffer named *forth* by default. Non-session evaluation is not supported by Babel, so :session none raises an error. Any other value assigned to :session is ignored.

There are no Forth-specific header arguments.

Examples of Use

Here is the obligatory Hello World! example in Forth.

#+begin_src forth 
." Hello World!"
#+end_src

#+RESULTS:
: Hello World!

Documentation from the orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.