XCODX |

Forth Online Compiler & Interpreter

Select Language
Online Code Compiler
Full HTML IDE
Py main.py
Program Output Ready
  Welcome to XCODX Online Compiler

  Quick Start:
  Ctrl+Enter  Run code
  Ctrl+S      Save / Download
  Ctrl+L      Clear output

  Select a language and start coding.
Success
Operation completed

About Forth

Forth is a stack-based, concatenative language and interactive environment created by Charles "Chuck" Moore, who first used it around 1970 after developing the ideas through the 1960s. It works in reverse Polish (postfix) notation: values are pushed onto a data stack, and "words" operate by consuming and producing stack items, so '2 3 +' leaves 5 on the stack. You program by defining new words from existing ones with ': name ... ;', effectively growing the language toward your problem. Forth is famously small and close to the hardware, which made it a mainstay of embedded systems, instrument control, and boot firmware, and the IEEE 1275 Open Firmware used on Sun SPARC and PowerPC Macs is a Forth system. On XCODX, Forth runs as GNU Forth (gforth), the GNU project's implementation.

Hello World in Forth

\ Forth is postfix and stack-based.
2 3 + .  cr            \ push 2 and 3, add, print -> 5

: square ( n -- n2 ) dup * ;
7 square .  cr         \ define a word, then use it -> 49

: greet ( -- )
  ." Name? "
  pad 80 accept        \ read a line from the Stdin Box into pad
  cr ." Hello, " pad swap type ;
greet

When to use Forth

Forth's strengths are a tiny footprint, directness, and interactivity: with almost no runtime it can bring up bare hardware, which is why it has been used in firmware, spacecraft and instrument controllers, and other embedded settings for decades. Writing Forth means building a vocabulary of small words and composing them, which rewards careful factoring and experimentation. In this sandbox it is an excellent place to get a feel for stack thinking and postfix arithmetic on short programs. It will not touch real hardware or peripherals here, and there is no persistent dictionary between runs; each program starts from the standard gforth words.

Common questions

What does stack-based or concatenative mean?

Instead of naming variables and using infix operators, Forth passes everything through a data stack, and you write operations after their operands, as in '3 4 *'. Concatenative means that placing two words in sequence composes their effects, so programs are built by juxtaposition rather than nesting.

How do I read input in gforth?

'KEY' waits for and returns one character's code, while 'ACCEPT ( addr n -- len )' reads a whole line into a buffer you supply, such as 'pad 80 accept', and returns how many characters were read. Type into the live terminal / Stdin Box, then use 'TYPE' to echo the buffer back.

How do I define my own words?

Use a colon definition: ': double ( n -- n2 ) 2 * ;' creates a new word 'double' from existing ones. After that, '5 double .' prints 10. Growing the dictionary of words this way is the normal way to program in Forth.

Can I install Forth libraries or load extra files?

No. There is no package system or network, and the filesystem is wiped each run, so you cannot 'INCLUDE' external files or fetch libraries. Your whole program is the single source you run, using gforth's built-in word set.

Why did my numbers print with trailing spaces?

The '.' word prints the top of the stack followed by a space, by design. Use 'CR' for a newline and the '."' word to print a literal string; 'TYPE' prints a string given its address and length.

Is Forth still used?

Yes, in niches: embedded controllers, some bootloaders and firmware such as Open Firmware and OpenBoot, and hobbyist and retro-computing projects. It is far from mainstream, but its ideas about minimalism and extensibility remain influential.

How Forth runs on XCODX

Sandbox filename
main.fth
Entry point
single main source file
Editor grammar
text/x-forth
Reading stdin
accept / key
Input delivery
live WebSocket stream
Prompt flushing
flush manually before reading input
Compile limit
10 s
Run limit
3 s batch · up to 3 min live
Memory
256 MB per stage
Max source
50,000 characters

Default program on this page

: greet ." Hello from Forth!" CR ;
: welcome ." Welcome to XCODX Compiler!" CR ;
greet
welcome
5 3 + ." Sum of 5+3 = " . CR