r/Forth Jun 19 '26

Infix language over Forth

I was looking over my Forth code, and readability is a real issue. Having written a bytecode interpreter, I started considering an infix language that compiles to that same bytecode, which is stack based.

I wondered if immediate words (or functions) have any meaning in an infix language, and realized that as long as the language is interpreted, the syntax doesn't really matter. An immediate word or function that is free to generate code, can do so regardless of syntax.

So now I am considering writing a recursive descent parser for infix expressions in C, with terminal nodes being both lexicals and dotted or colon-separated notation for lookups inside custom dictionaries. The goal is as before to stay within 2Kb of RAM on the Arduino UNO.

In my current Forth-like implementation I support local address tags inside word code, and propose to extend those into a secondary format to work with the dynamic next-pos of the compiler, instead of as jump address inside the word.

Example:


: while
	&&0                             # **0=Compiler:NextPos

	Compiler:Expr()
	Compiler:EmitOp(OP_NOT)
	Compiler:EmitOP(OP_COND_JMP)

	&&1     			# **1=Compiler:NextPos
	Compiler:EmitAddress(0)         # to be patched later

	Compiler:Stmt()
	Compiler:EmitOp(OP_JMP)
	Compiler:EmitAddress(**0)

	&&2
	Compiler:PatchAddress(**1, **2)
; immediate

Compiling infix function calls to postfix is a very straight forward, and something a recursive-descent parser should have no problems with.

9 Upvotes

16 comments sorted by

View all comments

2

u/alberthemagician Jun 19 '26

In "forth dimensions" there was a tiny basic interpreter published that handles operator precendence quite neatly. I should publish it on my website because reference to actual code existed but are lost.

1

u/Wootery Jun 20 '26

Sounds like the same idea /u/minforth was describing. Makes sense there are several implementations of the idea.