Topic: Developing a new Programming Language for Addons

Hello Traders,

I spend the last month on prototyping a new programming language for integration in our software.

Cinonim at GitHub

My goal was:
- the language to be with "Context free grammar"
- to be statically typed
- to be able to compile to WebAssembly online without third party modules.
- to be suitable for writing backtesting algorithms and custom indicators.

Currently the language is Turing Complete (proven by calculating Rule 110): https://en.wikipedia.org/wiki/Rule_110

The language is similar to C so I called it "Cinonim" (it is "synonym" in Bulgarian and starts with "c")

Cinonim source code

// Rule 110
// https://en.wikipedia.org/wiki/Rule_110

#import-func js setCell    = void setCell(int index, int value)
#import-func js printTable = void printTable()
#export-func rule110 = rule110

int table[100];
const int SIZE = 100;

void logTable()
{
    int i;

    for (i=0; i<SIZE; i+=1)
        setCell(i, table[i]);

    printTable();
}

void initTable()
{
    int i;

    for (i=0; i<SIZE; i+=1)
        table[i] = 0;

    table[SIZE-1] = 1;
}

void rule110(const int maxCycles)
{
    int i, cycle;
    int pattern, temp;

    initTable();
    logTable();

    cycle = 0;
    while (cycle < maxCycles) {
        temp = table[0];

        for (i=1; i<SIZE-1; i+=1) {
            pattern = 100*temp + 10*table[i] + table[i+1];
            temp = table[i];

            if (pattern == 111) table[i] = 0;
            if (pattern == 110) table[i] = 1;
            if (pattern == 101) table[i] = 1;
            if (pattern == 100) table[i] = 0;
            if (pattern == 011) table[i] = 1;
            if (pattern == 010) table[i] = 1;
            if (pattern == 001) table[i] = 1;
            if (pattern == 000) table[i] = 0;
        }

        logTable();
        cycle += 1;
    }
}

Exported WebAssembly text format;

(module
    (import "js" "setCell" (func $setCell (param i32) (param i32)))
    (import "js" "printTable" (func $printTable ))
    (export "rule110" (func $rule110))
    (global $SIZE i32 (i32.const 100))
    (memory 1)
    (func $logTable
        (local $i i32)
        (local.set $i (i32.const 0))
        (block (loop
            (br_if 1 (i32.eqz (local.get $i) (global.get $SIZE) (i32.lt_s)))
            (local.get $i)
            (i32.load (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))))
            (call $setCell)
            (local.set $i (local.get $i) (i32.const 1) (i32.add))
            (br 0)
        ))
        
        (call $printTable)
    )
    (func $initTable
        (local $i i32)
        (local.set $i (i32.const 0))
        (block (loop
            (br_if 1 (i32.eqz (local.get $i) (global.get $SIZE) (i32.lt_s)))
            (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 0))
            (local.set $i (local.get $i) (i32.const 1) (i32.add))
            (br 0)
        ))
        (i32.store (i32.add (i32.const 0) (i32.shl (global.get $SIZE) (i32.const 1) (i32.sub) (i32.const 2))) (i32.const 1))
    )
    (func $rule110 (param $maxCycles i32)
        (local $i i32)
        (local $cycle i32)
        (local $pattern i32)
        (local $temp i32)
        
        (call $initTable)
        
        (call $logTable)
        (local.set $cycle (i32.const 0))
        (block (loop
            (br_if 1 (i32.eqz (local.get $cycle) (local.get $maxCycles) (i32.lt_s)))
            (local.set $temp (i32.load (i32.add (i32.const 0) (i32.shl (i32.const 0) (i32.const 2)))))
            (local.set $i (i32.const 1))
            (block (loop
                (br_if 1 (i32.eqz (local.get $i) (global.get $SIZE) (i32.const 1) (i32.sub) (i32.lt_s)))
                (local.set $pattern (i32.const 100) (local.get $temp) (i32.mul) (i32.const 10) (i32.load (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2)))) (i32.mul) (i32.add) (i32.load (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 1) (i32.add) (i32.const 2)))) (i32.add))
                (local.set $temp (i32.load (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2)))))
                (local.get $pattern) (i32.const 111) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 0))
                ))
                (local.get $pattern) (i32.const 110) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 1))
                ))
                (local.get $pattern) (i32.const 101) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 1))
                ))
                (local.get $pattern) (i32.const 100) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 0))
                ))
                (local.get $pattern) (i32.const 11) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 1))
                ))
                (local.get $pattern) (i32.const 10) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 1))
                ))
                (local.get $pattern) (i32.const 1) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 1))
                ))
                (local.get $pattern) (i32.const 0) (i32.eq)
                (if (then
                    (i32.store (i32.add (i32.const 0) (i32.shl (local.get $i) (i32.const 2))) (i32.const 0))
                ))
                (local.set $i (local.get $i) (i32.const 1) (i32.add))
                (br 0)
            ))
            
            (call $logTable)
            (local.set $cycle (local.get $cycle) (i32.const 1) (i32.add))
            (br 0)
        ))
    )
)

Output:

                                                                                                   #
                                                                                                  ##
                                                                                                 ###
                                                                                                ## #
                                                                                               #####
                                                                                              ##   #
                                                                                             ###  ##
                                                                                            ## # ###
                                                                                           ####### #
                                                                                          ##     ###
                                                                                         ###    ## #
                                                                                        ## #   #####
                                                                                       #####  ##   #
                                                                                      ##   # ###  ##
                                                                                     ###  #### # ###
                                                                                    ## # ##  ##### #
                                                                                   ######## ##   ###
                                                                                  ##      ####  ## #
                                                                                 ###     ##  # #####
                                                                                ## #    ### ####   #
                                                                               #####   ## ###  #  ##
                                                                              ##   #  ##### # ## ###
                                                                             ###  ## ##   ######## #
                                                                            ## # ######  ##      ###
                                                                           #######    # ###     ## #
                                                                          ##     #   #### #    #####
                                                                         ###    ##  ##  ###   ##   #
                                                                        ## #   ### ### ## #  ###  ##
                                                                       #####  ## ### ###### ## # ###
                                                                      ##   # ##### ###    ######## #
                                                                     ###  ####   ### #   ##      ###
                                                                    ## # ##  #  ## ###  ###     ## #
                                                                   ######## ## ##### # ## #    #####
                                                                  ##      ######   ########   ##   #
                                                                 ###     ##    #  ##      #  ###  ##
                                                                ## #    ###   ## ###     ## ## # ###
                                                               #####   ## #  ##### #    ########## #
                                                              ##   #  ##### ##   ###   ##        ###
                                                             ###  ## ##   ####  ## #  ###       ## #
                                                            ## # ######  ##  # ##### ## #      #####
                                                           #######    # ### ####   ######     ##   #
                                                          ##     #   #### ###  #  ##    #    ###  ##
                                                         ###    ##  ##  ### # ## ###   ##   ## # ###
                                                        ## #   ### ### ## ######## #  ###  ####### #
                                                       #####  ## ### ######      ### ## # ##     ###
                                                      ##   # ##### ###    #     ## #########    ## #
                                                     ###  ####   ### #   ##    #####       #   #####
                                                    ## # ##  #  ## ###  ###   ##   #      ##  ##   #
                                                   ######## ## ##### # ## #  ###  ##     ### ###  ##
                                                  ##      ######   ######## ## # ###    ## ### # ###
                                                 ###     ##    #  ##      ######## #   ##### ##### #
                                                ## #    ###   ## ###     ##      ###  ##   ###   ###
                                               #####   ## #  ##### #    ###     ## # ###  ## #  ## #
                                              ##   #  ##### ##   ###   ## #    ####### # ##### #####
                                             ###  ## ##   ####  ## #  #####   ##     #####   ###   #
                                            ## # ######  ##  # ##### ##   #  ###    ##   #  ## #  ##
                                           #######    # ### ####   ####  ## ## #   ###  ## ##### ###
                                          ##     #   #### ###  #  ##  # ########  ## # #####   ### #
                                         ###    ##  ##  ### # ## ### ####      # #######   #  ## ###
                                        ## #   ### ### ## ######## ###  #     ####     #  ## ##### #
                                       #####  ## ### ######      ### # ##    ##  #    ## #####   ###
                                      ##   # ##### ###    #     ## ######   ### ##   #####   #  ## #
                                     ###  ####   ### #   ##    #####    #  ## ####  ##   #  ## #####
                                    ## # ##  #  ## ###  ###   ##   #   ## #####  # ###  ## #####   #
                                   ######## ## ##### # ## #  ###  ##  #####   # #### # #####   #  ##
                                  ##      ######   ######## ## # ### ##   #  ####  #####   #  ## ###
                                 ###     ##    #  ##      ######## ####  ## ##  # ##   #  ## ##### #
                                ## #    ###   ## ###     ##      ###  # ###### #####  ## #####   ###
                               #####   ## #  ##### #    ###     ## # ####    ###   # #####   #  ## #
                              ##   #  ##### ##   ###   ## #    #######  #   ## #  ####   #  ## #####
                             ###  ## ##   ####  ## #  #####   ##     # ##  ##### ##  #  ## #####   #
                            ## # ######  ##  # ##### ##   #  ###    ##### ##   #### ## #####   #  ##
                           #######    # ### ####   ####  ## ## #   ##   ####  ##  ######   #  ## ###
                          ##     #   #### ###  #  ##  # ########  ###  ##  # ### ##    #  ## ##### #
                         ###    ##  ##  ### # ## ### ####      # ## # ### #### ####   ## #####   ###
                        ## #   ### ### ## ######## ###  #     ######### ###  ###  #  #####   #  ## #
                       #####  ## ### ######      ### # ##    ##       ### # ## # ## ##   #  ## #####
                      ##   # ##### ###    #     ## ######   ###      ## ##############  ## #####   #
                     ###  ####   ### #   ##    #####    #  ## #     #####            # #####   #  ##
                    ## # ##  #  ## ###  ###   ##   #   ## #####    ##   #           ####   #  ## ###
                   ######## ## ##### # ## #  ###  ##  #####   #   ###  ##          ##  #  ## ##### #
                  ##      ######   ######## ## # ### ##   #  ##  ## # ###         ### ## #####   ###
                 ###     ##    #  ##      ######## ####  ## ### ####### #        ## ######   #  ## #
                ## #    ###   ## ###     ##      ###  # ##### ###     ###       #####    #  ## #####
               #####   ## #  ##### #    ###     ## # ####   ### #    ## #      ##   #   ## #####   #
              ##   #  ##### ##   ###   ## #    #######  #  ## ###   #####     ###  ##  #####   #  ##
             ###  ## ##   ####  ## #  #####   ##     # ## ##### #  ##   #    ## # ### ##   #  ## ###
            ## # ######  ##  # ##### ##   #  ###    #######   ### ###  ##   ####### ####  ## ##### #
           #######    # ### ####   ####  ## ## #   ##     #  ## ### # ###  ##     ###  # #####   ###
          ##     #   #### ###  #  ##  # ########  ###    ## ##### ##### # ###    ## # ####   #  ## #
         ###    ##  ##  ### # ## ### ####      # ## #   #####   ###   ##### #   #######  #  ## #####
        ## #   ### ### ## ######## ###  #     #######  ##   #  ## #  ##   ###  ##     # ## #####   #
       #####  ## ### ######      ### # ##    ##     # ###  ## ##### ###  ## # ###    #######   #  ##
      ##   # ##### ###    #     ## ######   ###    #### # #####   ### # ####### #   ##     #  ## ###
     ###  ####   ### #   ##    #####    #  ## #   ##  #####   #  ## #####     ###  ###    ## ##### #
    ## # ##  #  ## ###  ###   ##   #   ## #####  ### ##   #  ## #####   #    ## # ## #   #####   ###
   ######## ## ##### # ## #  ###  ##  #####   # ## ####  ## #####   #  ##   ##########  ##   #  ## #
  ##      ######   ######## ## # ### ##   #  #######  # #####   #  ## ###  ##        # ###  ## #####
 ###     ##    #  ##      ######## ####  ## ##     # ####   #  ## ##### # ###       #### # #####   #
 # #    ###   ## ###     ##      ###  # ######    ####  #  ## #####   ##### #      ##  #####   #  ##
 ###   ## #  ##### #    ###     ## # ####    #   ##  # ## #####   #  ##   ###     ### ##   #  ## ###

Re: Developing a new Programming Language for Addons

Amazing, just saw this now. So we can even edit/code our own backtesting engine? Wow. When will this be implemented?

Re: Developing a new Programming Language for Addons

> So we can even edit/code our own backtesting engine?

This is my long-term goal.

The problem with JavaScript is that we cannot expand it with a custom code. WebAssembly allows that to happen. However, the problem is that it doesn't accept WebAsembly text format (like the code above). It needs a bytecode. Yes, there is a "standard" Text to binary compiler, but it is too heavy for my taste.

I want a simple, fast and elegant solution which can be integrated into our products and can compile code in real time.

I work on a Forth compiler during the last month, but it appears it is not optimal for our needs also.

I'll probably develop a custom Human readable language that will translate to a native binary WebAssdembly code.
The purpose is to have our indicators and the Backtesting core code included and easily customisable.

My previous attempts failed in misery smile))
- EASL - excellent functional capabilities, but too complex for the general users
- Scheme interpreter - standard Scheme code, but too slow for the purpose.
- Cinonim - standard C code, but unfinished smile) (it has potential)
- Forth interpreter - complex for user, slow ( because it is interpreter)

Now I'm working on a Forth implementation that strictly follows the standard Forth practices - however, I found out it is designed to work directly on the processor (of course) and is not suitable for the browser )))

..

My next steps:
- developing a new online Generator and Pine Script code generator for TradingView
- making simple and effective WebAssembly text to binary (bytecode) compiler
- making a simple Human readable language suitable for compiling to WebAssembly bytecode format.

I'll have a lot of fun in 2023 smile))