Convex
CJam with input that arrives by itself when the stack runs dry, and a code page full of new one-character commands.
GamrCorps · Turing complete: yes · ●●○ moderate
Tested with 7 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Convex is a golfing language by GamrCorps, built on the source of CJam with aditsu's permission. A program is still a row of commands working on one stack, and most CJam programs mean the same. Two things change. Convex reads its input on demand, and it uses the Windows-1252 code page, so many of CJam's two-character commands get a single character of their own, such as ´ for the list 1 to n.
Read the story
The reference interpreter is written in Java, in the repository GamrCorps/Convex. This implementation follows it.
The machine
Values are integers, characters, lists and blocks. A string is a list of characters. At the end the whole stack is printed with nothing between the items.
When a command pops and the stack is empty, Convex reads the next line of input. A line that starts with a digit, [, " or { is run as code, so 7 becomes the number 7. Any other line is pushed as text.
| Command | Does |
|---|---|
"…" 'c {…} | A string, a character, a block; the last quote may be left off |
+ - * / % | Arithmetic; on lists join, remove, repeat or join with, split, map |
_ ; \ @ | Duplicate, drop, swap, rotate three |
, | The range 0 to n−1, the length of a list, or a filter with a block |
:+ :* | Fold a list with an operator |
´ ² ¥ Å | The list 1 to n, square, reverse, keep the first of each value |
l q | A line of input, all of the input |
T U V | The lowercase alphabet, the uppercase one, both |
Example
The string runs to the end of the program and the stack is printed.
The string runs to the end of the program and the stack is printed.
_ finds the stack empty, so the line 7 is read and run as code. It is duplicated and multiplied.
This implementation is a subset. It has CJam's core and a few of Convex's new commands. Floating point numbers, quaternions, regular expressions and the compressed strings stop the run with a reason.
Patterns it teaches
- Code golf: reading input on demand saves the command that would read it.
- Stack machines: an empty stack is a request for input, not an error.
- Concatenative: a program is commands applied to one stack in a row.
Try this
- Read a number and print its square without
_. - Print the uppercase alphabet backwards.
- Hard: read a line and print each word on a line of its own.
Further
- The reference interpreter, in Java
- Related: CJam, GolfScript
See also
Also in Golfing
Write your own in Run Convert a program from Convex
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: convex.
Command line
shib run convex -e 'q' --input 'a cat'shib manual convexSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/convex.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language