Staccato
Note: This article introduces a programming language, not a performance technique.
Staccato is a programming language designed by PSTF. It is a concise, stack-based language where every operation is a single character (or short literal) to minimize code length. It's Turing‑complete, supports I/O, and includes all the tools needed for code golf.
Overview
- Stack‑based: Most operations pop arguments from the stack and push results.
- Dynamic typing: Integers, strings, lists (arrays) are the primary types.
- Implicit I/O: The entire stack is printed at program end (newline‑separated). Explicit printing is also available.
- Unbounded memory: An expandable array (the “tape”) provides random access, making the language Turing‑complete.
Syntax
Literals
- Integers: 123 – pushes the number.
- Strings: "abc" – pushes the string (no escapes; use \ for quotes).
- Lists: [1 2 3] – pushes a list containing the three numbers.
Stack Manipulation
Command Effect _ duplicate top of stack : swap top two items ; pop and discard \ rotate the top three items (3rd → 2nd → 1st → 3rd) @ copy the third item to the top
Control Flow
Command Effect
? if‑then‑else: pop condition, if true execute block1, else block2. Syntax: (condition)?{true‑code}{false‑code}
w while loop: repeatedly execute block while top of stack is truthy (non‑zero, non‑empty). Syntax: {condition-code}w{body-code}
f for loop: iterate over a list. Syntax: (list)f{variable‑name}{body}. The loop variable is pushed onto the stack for each iteration.
I/O
Command Effect i read a line from stdin, push as a string I read a line, convert to integer, push p pop top and print it with a newline o pop top and print it as a character (or string) without newline
Array / Tape
Command Effect
[ start a list literal
] end a list literal
, range: {n m s,} → [n, m) with the step of s
@ when top is an index and second‑top is a list, push the element at that index
= when top is a value, second‑top is an index, third‑top is a list → assign value to list[index]
Miscallenous
Command Effect # comment to end of line n push a newline string "\n" s convert top to string d convert top to integer (if string, parse) ~ evaluate top as Staccato code (useful for metaprogramming)
Example
x' = x!
I1{_1-_:*}_p
FizzBuzz from 1 to 100
100 0 -1,
f n {
n 15%0={ "FizzBuzz"p }
n 3%0={ "Fizz"p }
n 5%0={ "Buzz"p }
n{ np }?
}?
Turing‑Completeness Proof
Staccato is Turing‑complete because it can simulate a Minsky machine (two counter machine), which is known to be Turing‑complete.
Simulating Counters
Use a list [c1, c2] to hold the two counters. We’ll keep it on the stack.
Increment counter i:
[c1,c2] (list) → duplicate list, index into it, add 1, store back.
Example for counter 0:
_ 0 @ 1 + 0 =
Decrement and test zero:
_ 0 @ 1 - (if result is negative, we treat it as zero).
Conditional branching
The ? command provides selection; loops provide unbounded iteration. Because we can create arbitrary integer values (by repeated addition) and access them via list indexing, we have a random‑access tape. This is sufficient to simulate a Turing machine’s tape.
So?
Thus Staccato can emulate any Turing machine.
Epilogue
Staccato balances minimal syntax with full power, making it ideal for code golf while remaining Turing‑complete and practical for I/O tasks.