Cy
A postfix language that is not trying to be short. Words are spelled out, blocks are values, and Ruby does the arithmetic underneath.
cyoce · Turing complete: yes · ●●○ moderate
Tested with 8 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Cy was written by cyoce as a single Ruby file, cy.rb. Its README calls it a non-golfing postfix language, which is unusual among the languages Try It Online gathered from the code golf site. Values go on a stack and words take them off, as in Forth, but the words are readable, such as swap, while, each and times.
Read the story
Blocks in braces are values like any other. {...} pushes code without running it, and words such as if, while and each take blocks from the stack and call them. Every word has a second form with & in front that reads its arguments without removing them, so &dupe and &+ keep the stack as it was.
The machine
One stack, a table of variables, and the types of Ruby, which are big integers, floats, strings, arrays, true, false and nil. False, nil, the empty string, the empty array and zero count as false.
| Word | Does |
|---|---|
+ - * / % ^ | Arithmetic; / always gives a float, ^ is a power, strings and arrays join and repeat |
> < >= <= >< <> | Compare; >< is equal, <> not equal |
:< :<< | Print with or without a newline |
:> | Read a line |
[ x , y , ] | Build an array; , moves the top into it |
=name $name | Set and read a variable |
cond {body} if | Run the block when cond is true |
{cond} {body} while | Repeat while the first block leaves a true value |
list {body} each | Push each element and run the block |
n {body} times | Run the block n times |
Inside a "string", % takes the top of the stack into the text and & copies it.
Example
The string is pushed and :< prints it with Ruby's puts.
The string is pushed and :< prints it with Ruby's puts.
The condition block copies the counter for while to test. The body prints a copy and subtracts one.
Each , moves the number before it into the open array, and ] pushes the array.
This implementation is a subset. It stops with a reason on Ruby features it does not model, such as the reader and writer stacks, symbols and the assignment words like ++.
Patterns it teaches
- Stack machines: every word takes its arguments from one stack.
- Concatenative: a block is a piece of program you can pass around.
Try this
- Print the numbers 1 to 10, one per line, with
..andeach. - Read a line and print it twice.
- Hard: print the factorial of 10 with
timesand a variable.
Further
- The reference interpreter, cy.rb, in Ruby
See also
Also in Functional and concatenative
Write your own in Run Convert a program from Cy
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: cy.
Command line
shib run cy -e ':> :<' --input 'a cat'shib manual cySource
- Module
langs/tio/ports.js- Interpreter
langs/tio/cy.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language