esolang

Comun

Something between brainfuck and C, as its author puts it, written as a stack of short symbols.

drummyfish, 2022 · Turing complete: with unbounded memory · ●●○ moderate · esolangs.org

Tested with 2 known programs · Test method

Run it

Expected: Hello, World!

The idea

drummyfish made comun with Forth in mind and hopes it will be usable for real work. Every built-in pops its arguments off a stack and pushes its result. There are named functions, if and else, while loops, a break and a return. Memory is arrays fixed when the program is compiled, with pointers into them, and the stack is one of those arrays. In March 2024 comun became self-hosted.

Read the story

This implementation is the part the specification shows, on one stack of unsigned 32-bit numbers. Pointers, arrays, the 8, 16 and 32 bit types, input, comparisons and the rest of comun's specification are left out.

The machine

WordDoes
12 "text"Push a number; push the characters of a text, the first on top
+ - * / %Pop two, push the result
++ --Add or take 1
^ ><Pop; swap
$0 to $9Push a copy of the item that many places down
-> -->Print a character; print characters until a 0
?' ... ; ... .If the top is not 0, else, end
@' ... .While the top is not 0
name: ... .Define a function; name calls it

?' and @' leave the value they test on the stack. ? and @ pop it. # starts a comment.

Example

The text goes on last, so its first character is on top.

The text goes on last, so its first character is on top. --> prints down to the 0 that was pushed first, with the newline just before it.

The factorial program has no number printing to call, so it writes its own. printNum pushes a 0, then the digits from the last one up, and lets --> print them.

Expected: 1 2 6 24 120 720 5040 40320 362880 3628800

A countdown keeps its counter on the stack and prints each value as a digit.

Expected: 54321

Patterns it teaches

  • Stack machines: every built-in works on the stack, and $0 to $9 reach into it.

Further

Stack-based

Write your own in Run Convert a program from Comun

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run comun -e '0 10 "Hello, World!" -->'
shib manual comun

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/comun.js

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

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