4s (Four Stacks)
4s
4s (Four Stacks) is an esoteric stack-based programming language featuring four distinct stacks used for data manipulation, temporary storage, output buffering, and control flow.
Overview
4s is designed to explore multi-stack computation. Programs consist of single-character instructions operating on different stacks.
The language separates concerns by using different stacks for different roles, allowing more explicit control over execution.
Stacks
4s uses four stacks:
- Main Stack (MS) - Primary data stack where most operations occur
- Scratch Stack (SS) - Temporary storage for intermediate values
- Output Stack (OS) - Stores values before printing
- Control Flow Stack (CFS) - Stores integers for loops and control flow
Data types
4s supports three data types:
- Integer - Whole numbers
- Float - Floating-point numbers
- String - Sequences of characters
Type conversion operations:
- `*` - Convert to string
- `==` - Convert to float
- `/` - Convert to integer
Arithmetic operations automatically promote integers to floats when needed.
Syntax
Programs are sequences of single-character commands. Whitespace is ignored.
Comments are written as: ! comment !
Commands
Input
- `_` - Read string and push to MS
- `&` - Read float and push to MS
- `%` - Read integer and push to MS
Stack operations
- `+` - Push literal
- `;` - Duplicate top of MS
- `~` - Swap top two values of MS
- `^` - Pop MS
- `,` - Pop SS
Movement
- `>` - Move value from MS to SS
- `<` - Move value from SS to MS
Output
- `-` - Move value from MS to OS
- `.` - Print OS (and clear it) (The Output Stack prints from top to bottom and is cleared after printing.)
- `$` - Pop OS
Arithmetic
Operations are written inside `{}`:
- `{+}` - Addition
- `{-}` - Subtraction
- `{*}` - Multiplication
- `{/}` - Division
Logic
- `?` - Equality comparison (pushes 1 if equal, 0 otherwise)
Increment / Decrement
- `@` - Increment
- `#` - Decrement
Control Flow
- `:` - Push integer from MS to CFS
- `(` - Increment top of CFS
- `)` - Decrement top of CFS
Loops
- `[ ... ]` executes repeatedly while the top value of the Control Flow Stack is non-zero.
If it becomes zero, the loop ends and the value is popped.
Literals
- `+"41.9"` - Float literal (string inside is always parsed as a float)
- `+'123'` - Integer literal
- `+text` - String literal
- Strings are only created via +text or operations that produce strings.
Quoted + "..." is always interpreted as a float, never a string.
Escape sequences are supported inside strings.
- Note: +"..." does not create a string. It always parses as a floating-point number.
Errors
- Stack underflow causes runtime error
- Invalid type operations terminate execution
- Division by zero is undefined
Example
Calculator
&&>>%>+'0'+'4':[@;)]^<;>?:[,<<{/}*+\n--.|]<;>?:[,<<{*}*+\n--.|]<;>?:[,<<{-}*+\n--.|]<;>?:[,<<{+}*+\n--.|]
Truth-machine
%:[+1-.]+0-.
Implementation
An interpreter exists in C (not published yet).
Notes
- 4s separates output from computation using a dedicated output stack
- Control flow is explicitly managed using the Control Flow Stack
- The language emphasizes clarity of stack behavior over brevity