m4th

Forth dialect for text preprocessing

<h1>Welcome, ^
^ username
</h1> <p>The current time is ^
^ time
</p>

<h1>Welcome, kt.</h1>
<p>The current time is 10:10 PM.</p>
you can use m4th for web templating, if you want.

m4th /ˈɛmfɔɹθ/ is a Forth dialect and text preprocessor similar in goals to the m4 macro processor. Like m4, it is built around the concept of streams and, as such, it opts to not implement word sets for managing "string" data types or memory allocation. Unlike m4, it has a more straightforward syntax informing the interpreter what is and is not to be interpreted: any line not beginning with the control character ^ is always passed literally to the current output stream.

a newline follows,
yet one will not on this line. ^
here is a caret ^^
^^ and yet another caret
a few ^ more ^^^ for good ^ measure

a newline follows,
yet one will not on this line. here is a caret ^
^ and yet another caret
a few ^ more ^^^ for good ^ measure
a tanka

m4th's syntax makes it much more predictable; you will never have to worry about accidentally calling a word. The interpretation rules for the preprocessor are stated tersely as follows:

  1. If a line begins with a ^ not immediately followed by a second, pass the line to the Forth interpreter. If the ^ is followed by a second, discard it and pass the remaining line to the current output.
  2. If a line ends with a ^ not immediately preceded by a second, pass the line to the current output stream without any line-break characters. If the ^ is preceded by a second, discard it and pass the remaining line to current output (including any line-break characters).

These simple rules make it difficult to invoke the Forth interpreter in cases where the author doesn't explicitly want to.

Further discussions of m4th's functionality on this page will assume a basic understanding of Forth.

streams

^ 3 divert
world!
^ 0 divert
Hello, ^
^ 3 undivert

Hello, world!
TIP: Use diversions to make your code difficult to read!

m4th's streams are very similar to m4's. You may divert to any integer-numbered stream with divert and flush a stream to the current output stream with undivert. A fresh word, which returns a number corresponding to a stream which has never been used or previously returned by a call to fresh, is provided as a convenience.

definitions

^ def hello fresh ;

Definitions in m4th work very similarly to standard Forth; however, the definition word has been changed from : (colon) to def.

page work in progress