Forth Haiku

January 30, 2018

Brad Nelson / @flagxor

Summary

  • Intro to Forth
  • Forth Haiku

Forth

  • Created by Charles "Chuck" Moore growing out personal programming tools
  • Named an an aspirationally Fo(u)rth generation programming language
  • Shortened to FORTH due to the 5 character filenames on the IBM 1130

Forth

  • EXTREME focus on minimalism (programs can always be smaller)
  • Stack based, RPN, concatenative
  • Focus on the 'word' as the core unit of programming
  • Low level, but with patterns for building high level DSLs
  • User modifiable syntax, identifiers include all characters
  • Sort of compiled, interpreted, and JITed
  • Diverse and divergent ecosystem
  • If you've seen one Forth, you've seen one Forth

Forth

  • EasyWriter - first wordprocessor for Apple II (John Draper aka Captain Crunch)
  • Starflight - early generative world game
  • OpenBoot - Bootloader / BIOS on Sun, IBM, and Apple PowerPC Mac
  • FedEx package trackers
  • Atari Arcade games, slot machines
  • Jupiter Ace
  • Canada Arm, various satellites and probes
  • Computer controlled cameras

Forthisms

  • 0=F -1=T, allowing AND, OR, XOR to work for booleans and bitwise
  • Source as 64x16 1KB virtual memory "blocks"
  • Dual "data" + "return" stacks.
  • Prefer edit time to compile time, compile time to run time.

Disfavored Practices

  • PICK and ROLL
  • Locals
  • Floating point

Standards

  • Forth79
  • Forth83
  • AnsForth '94
  • Forth20xx

Pitfalls

  • Potentially "write only", can look like line noise
  • Easy to end up writing C/C++ in Forth
  • Forth wants to be the OS

Definitions

  • : WASHER WASH SPIN RINSE SPIN ;

Comments

  • ( this is a
    multiline comment )
  • \ this is a single line comment

Stack Diagrams

  • ( n n -- n )
  • n = single-number
  • d = double-length number
  • a = address

Arithmetic

  • + ( a b -- a+b )
  • - ( a b -- a-b )
  • * ( a b -- a*b )
  • 2* ( n -- n*2 )
  • 2/ ( n -- n/2 )
  • */ ( a b c -- a*b/c )
  • /mod ( a b -- a/b a%b )
  • NEGATE ( n -- -n )

Floating / Fixed Point

  • F+ ( a b -- a+b )
  • Fixed point by default
  • Use "appropriate" units usec vs seconds
  • : msecs 1000 * ;
  • : seconds 1000 msecs * ;

Logical and Compare

  • 0< ( n -- n<0 )
  • 0= ( a -- a=0 )
  • < ( a b -- a<b )
  • AND ( a b -- a&b )
  • OR ( a b -- a|b )
  • XOR ( a b -- a^b )
  • INVERT ( a --- ~a )

Stack Ops

  • DUP ( n -- n n )
  • SWAP ( a b -- b a )
  • DROP ( n -- )
  • OVER ( a b -- a b a )
  • ROT ( a b c -- b c a )
  • -ROT ( a b c -- c a b )
  • 2DUP ( a b -- a b a b )

Return Stack

  • >R ( n -- r:n )
  • R> ( r:n -- n )
  • R@ ( r:n -- r:n | n )

Memory Ops

  • @ ( a -- n )
  • ! ( n a -- )
  • +! ( n a -- )
  • C@ ( a -- ch )
  • C! ( ch a -- )

Numbers

  • DECIMAL
  • HEX
  • 36 BASE !

Flow Control

  • (cond) IF (true) ELSE (false) THEN
  • BEGIN (body) (cond) UNTIL
  • BEGIN (cond) WHILE (body) REPEAT
  • (top) (start) DO (body) LOOP
  • (top) (start) DO (body) (step) +LOOP
  • (count) FOR (body) NEXT

Immediate Words

  • : unless postpone 0= postpone if ; immediate
  • (cond) UNLESS (false) ELSE (true) THEN

Data Words

  • VARIABLE (name)
  • (value) CONSTANT (name)
  • width height * constant area
  • CREATE TABLE 10 , 20 , 30 ,
  • CREATE IMAGE AREA ALLOT

ColorForth

DSSP

  • Setun - Soviet Ternary Computer
  • Avoid immediate words
  • DSSP            Forth
    [n] IF+ A       [n] 0> IF A THEN
    [n] IF0 A       [n] 0= IF A THEN
    [n] IF- A       [n] 0< IF A THEN
    [n] BR+ A B     [n] 0> IF A ELSE B THEN
    [n] BR- A B     [n] 0< NEG IF A ELSE B THEN
    [n] BR0 A B     [n] 0= IF A ELSE B THEN
              

Forth Haiku

  • Tiny programs → Beauty
  • Forth → JavaScript → GLSL (WebGL)
  • Share + Evolve on the Web

Haiku Basics

  • Typical Forth-y words, but with floating point
  • Program runs once per pixel
  • Words x, y provide position
  • Final stack holds red, green, blue, alpha

Primrose Flower - adg


: i 2dup z* log
; x .5 - y .5 -
i i i log over
          

Primrose Flower - adg

Server and Security

  • App Engine
  • Python API
  • Limit number of steps
  • No loops
  • : 10times x x x x x x x x x x x ;

Expanding Forth Haiku

  • Thoughtfully grow a small core word set
  • Work within the limits of WebGL
  • Add a few powerful words

Animation

  • Introduce t, adding time
  • Time in seconds since midnight
  • Allows for clocks!

Clock Cutout - Anonymous

PACMAN - www.manwe.ru

: d dup ;
: m 1 min ;
: f d floor - ;
: c cos abs ;
: j t 4 + 2 * x 8 * floor 8 / + 4 * c 2 / t 4 + 2 / c 4 ** * - ;
: a 1 x x 8 * floor 0.5 + 8 / - d * y ;
: b - d * + sqrt 50 * 8 ** ;
: p x t 4 + pi / f 1.6 * - 0.2 + ;
: v t 4 + pi 2 * / f ;
a j 0.5 b -
v d 0.5 < * 4 * m *
1 p d * y 0.5 - d * + 36 * 30 ** m -
y 0.5 - p atan2 abs t 10 * c 0.8 * - 16 * m * 0 max
a 0.5 b - 0 max d p 16 * < * +
p d * y 0.58 b m *
v 0.5 >= *
+ d 0.2
          

PACMAN - www.manwe.ru

Interactivity

  • Introduce mx, my, buttons, button, adding mouse and keyboard

TFKP 4 Flower Redux - Anonymous

Audio

  • Audio is HARD!
  • Audio from corners of image (pentatonic scale)
  • Introduced audio, adding an audio waveform
  • Sample 1
  • Sample 2

Video Input

  • Limited by WebGL
  • Video feed becomes texture source

x y sample
          

x y bwsample
          

Compilation

  • Forth -> JavaScript
  • Stack converted to locals
  • JavaScript -> WebGL fragment shader
http://developer.download.nvidia.com/CgTutorial/cg_tutorial_chapter01.html
http://www.cs.cornell.edu/courses/cs4620/2017sp/cs4621/lecture01/index.html

Users + Demo Sceners + Fun

  • Disqus for forum
  • Text search
  • Spam
  • Genealogy

forthsalon.appspot.com

Source and slides at: github.com/flagxor

Thank you