esolang

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

Expected: Hello, World!

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.

WordDoes
+ - * / % ^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 $nameSet and read a variable
cond {body} ifRun the block when cond is true
{cond} {body} whileRepeat while the first block leaves a true value
list {body} eachPush each element and run the block
n {body} timesRun 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.

Expected: 5 4 3 2 1

The condition block copies the counter for while to test. The body prints a copy and subtracts one.

Expected: 2 4 6

Each , moves the number before it into the open array, and ] pushes the array.

Expected: 3.5

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

Try this

  1. Print the numbers 1 to 10, one per line, with .. and each.
  2. Read a line and print it twice.
  3. Hard: print the factorial of 10 with times and a variable.

Further

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 cy

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/cy.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language