esolang

CJam

GolfScript rebuilt in Java, with characters as a type, more one-letter commands and fewer surprises.

aditsu, 2014 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 8 known programs · Hello World on Try It Online: identical · Test method

Run it

Expected: Hello, World!

The idea

CJam was made by aditsu in 2014 as a successor to GolfScript. The programs look much the same: a row of one-character commands that work on a stack, with the meaning of each command depending on the types it finds. For years it was one of the strongest languages on the code golf site.

Read the story

The changes are practical. Characters are a type of their own, so a string is an array of characters and 'a is a character literal. The preset variables give cheap constants: A is 10, S a space, N a newline, W is −1. Many letters became commands: q reads all input, l a line, i makes an integer, p prints a value as code.

The machine

One stack and 26 variables. At the end the whole stack is printed, element after element.

OpNumbersArrays and stringsBlocks
+ -Add, subtractJoin, remove elements
*MultiplyRepeat; join with a separatorRun n times; fold over an array
/ %Divide, remainderSplit, chunks; every nth (W% reverses)For each; map
,Range 0 … n−1LengthFilter an array
( )Step down, upTake off the first, the last
= < >CompareIndex (it wraps around), take, drop
_ ; \ @ $Duplicate, drop, swap, rotate three, copy the nthSort with $Sort by a key
~Bitwise notEvaluate a string, dump an arrayRun it
? g h wIf, do while, while

:+ folds + over an array, :i maps i over it, and 2f* multiplies every element by 2.

Example

The string is left on the stack and printed at the end.

The string is left on the stack and printed at the end.

Expected: 55

q~ reads the input and evaluates it to 10, ) makes it 11, , the range 0 to 10, and :+ adds them.

Expected: {"_~"}_~

The standard quine. _ copies the block and ~ runs one copy, which pushes _~. A block prints as its source in braces.

Expected: 1 1 2 3 5 8 13 21 34 55

Fibonacci: 2$ copies the number below the top, + adds, eight times. ] gathers the stack into an array and S* joins it with spaces.

This implementation is a subset. It has the core commands above and stops with a reason on anything else, such as floating point numbers, the e, m and . families of commands or the rarer letters, rather than guess what the Java interpreter would do.

Patterns it teaches

  • Code golf: one character per idea, and types that pick the meaning.
  • Concatenative: a program is functions applied to a stack in a row.
  • Stack machines: input is read on request, output is the stack.
  • Quine: a block prints as its own source, so a quine is seven characters.

Try this

  1. Print the squares of 0 to 9, separated by spaces.
  2. Reverse each word of a line of input.
  3. Hard: print the factorial of the number in the input.

Further

Golfing languages

Stack-based

Write your own in Run Convert a program from CJam

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: cjam.

Command line

shib run cjam -e 'q' --input 'a cat'
shib manual cjam

Source

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

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

Source esolangs.org (CC0) · Edit on esolangs.org