Comun

From Esolang
Jump to navigation Jump to search

Comun is a stack-based imperative toy language created by drummyfish. The author was inspired by Forth and describes the language as "something in between Brainfuck and C" that will hopefully be actually usable.

Comun works on compile-time allocated arrays made integers of various sizes, as well as compile-time allocated single pointers into these arrays. The pointers are mutable, but you can't have arrays of them, so if you want an array of pointers you have to use indexes and pointer arithmetic instead.

Four types of integers are supported: native (the unsigned int C type of the host), 8-bit, 16-bit, and 32-bit. These types don't mix, each array has a definite type and you aren't allowed to access it using any other types. With the exception of a type conversion operator, every built-in works on just one type that all its arguments and results share.

The built-in operations are stack-based, popping arguments and pushing results onto a stack; however, the stack is just a normal array like any other, you can make the stack pointer point into any array or access the stack with pointers like you can access any array. There are shortcuts to read or write the top ten elements of the stack.

Control constructs are similar to traditional C: named functions, if conditionals, if/else conditionals, while loops, all of these with any length of code in their body, as well as a command to break out of the innermost loop, and one to return out of the innermost function.

Examples

Hello, World!

This example pushes values 0 (string terminator), 10 (newline) and an ASCII string and subsequently uses the built-in string-printing function.

0 10 "Hello, World!" -->

Factorial

The following example prints the factorial (computed recursively) of numbers from 1 to 10. The most complex part is actually the number printing function which does not exist in the language itself.

factorial:
  ?'
    $0 -- factorial *
  ;
    ^ 1
  .
.

printNum:
  0 ><
  @'
    $0 10 % "0" + ><
    10 /
  .
  ^
  -->
.

10 @'
  11 $1 - factorial printNum  # C equivalent: printNum(factorial(11 - i))
  10 ->   # newline 
  --
.

External links