Easy Forth By Nick Morgan GitHub Repository --- Introduction Forth is a minimalist programming language from the 1970s that is neither functional nor object-oriented and has almost no syntax. It revolves entirely around using a stack for computations and operations. This tutorial uses a simple JavaScript-based Forth interpreter to introduce key concepts, aiming to broaden your programming perspective. --- Basics: Stack and Reverse Polish Notation Numbers push onto the stack. Operators (e.g., +) pop off the top operands, compute, and push the result back. Operators follow operands, i.e., Reverse Polish Notation (RPN). For example, 5 2 + 10 computes (5 + 2) 10. Stack underflow errors occur if there aren’t enough operands for an operation. Stack effects are documented as ( before -- after ) to describe how words affect the stack (e.g., + ( n1 n2 -- sum )). --- Defining Words (Functions) Use : to start a definition and ; to end it. The first word after : is the name, followed by the body. Words not found in the dictionary are treated as numbers or cause errors. Example: --- Stack Manipulation Words dup ( n -- n n ): Duplicates the top stack item. drop ( n -- ): Removes the top stack item. swap ( n1 n2 -- n2 n1 ): Swaps the top two stack items. over ( n1 n2 -- n1 n2 n1 ): Copies the second item to the top. rot ( n1 n2 n3 -- n2 n3 n1 ): Rotates the top three items. --- Generating Output . (period): Prints and removes the top of the stack. emit: Prints the top of the stack as an ASCII character. cr: Outputs a newline. ." … ": Prints a string literal. Example combined usage to print stack and ASCII characters. --- Conditionals and Loops Booleans No dedicated boolean type; 0 is false, -1 is canonical true, others are treated as true logically. Comparisons: =, <, >, returning 0 or -1. Boolean operations: and, or, invert (bitwise operations, best used with 0 or -1). Conditionals if then: Executes code between if and then if top stack value is true. if else then: Standard if-else block. Conditionals only used inside definitions. Loops do loop: For-loop style; top two stack values are limit and start values. In loops, i pushes current loop index. Example: Fizz Buzz Defined words fizz?, buzz? check divisibility and print "Fizz" or "Buzz". fizz-buzz? returns true if number is NOT divisible by 3 or 5. do-fizz-buzz loops from 1 to 25 to run the game logic. The example unpacks the stack manipulation to explain flow. --- Variables and Constants Variables Defined with variable name. Variables push their memory address. ! stores a value at a variable's address; @ fetches it. ? defined as @ . prints variable value. +! increments variable value. Constants Defined in one line: <value> constant <name>. Constants push their value directly. Example: 42 constant answer, then 2 answer pushes 84 on the stack. --- Arrays Memory can be allocated with allot after a variable. Elements accessed by offset, multiplied by cells (cell width). Example defines an array numbers of 4 cells. Helper word: Use number to get address and store/fetch values. --- Keyboard Input key pauses execution until keypress, pushes keycode. Example: key . key . key . waits for three keys and prints their codes. begin until loops like a "while" loop, looping until top of stack is true. Example prints keys until spacebar (ASCII 32) is pressed. --- Snake! Game Overview Non-Standard Additions Canvas Drawing: 24x24 pixel black/white canvas mapped to memory starting at graphics. Non-blocking