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.
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:
#
##
###
## #
#####
## #
### ##
## # ###
####### #
## ###
### ## #
## # #####
##### ## #
## # ### ##
### #### # ###
## # ## ##### #
######## ## ###
## #### ## #
### ## # #####
## # ### #### #
##### ## ### # ##
## # ##### # ## ###
### ## ## ######## #
## # ###### ## ###
####### # ### ## #
## # #### # #####
### ## ## ### ## #
## # ### ### ## # ### ##
##### ## ### ###### ## # ###
## # ##### ### ######## #
### #### ### # ## ###
## # ## # ## ### ### ## #
######## ## ##### # ## # #####
## ###### ######## ## #
### ## # ## # ### ##
## # ### ## ### ## ## # ###
##### ## # ##### # ########## #
## # ##### ## ### ## ###
### ## ## #### ## # ### ## #
## # ###### ## # ##### ## # #####
####### # ### #### ###### ## #
## # #### ### # ## # ### ##
### ## ## ### # ## ### ## ## # ###
## # ### ### ## ######## # ### ####### #
##### ## ### ###### ### ## # ## ###
## # ##### ### # ## ######### ## #
### #### ### # ## ##### # #####
## # ## # ## ### ### ## # ## ## #
######## ## ##### # ## # ### ## ### ### ##
## ###### ######## ## # ### ## ### # ###
### ## # ## ######## # ##### ##### #
## # ### ## ### ## ### ## ### ###
##### ## # ##### # ### ## # ### ## # ## #
## # ##### ## ### ## # ####### # ##### #####
### ## ## #### ## # ##### ## ##### ### #
## # ###### ## # ##### ## # ### ## # ## # ##
####### # ### #### #### ## ## # ### ## ##### ###
## # #### ### # ## # ######## ## # ##### ### #
### ## ## ### # ## ### #### # ####### # ## ###
## # ### ### ## ######## ### # #### # ## ##### #
##### ## ### ###### ### # ## ## # ## ##### ###
## # ##### ### # ## ###### ### ## ##### # ## #
### #### ### # ## ##### # ## #### ## # ## #####
## # ## # ## ### ### ## # ## ##### # ### ## ##### #
######## ## ##### # ## # ### ## ##### # #### # ##### # ##
## ###### ######## ## # ### ## # #### ##### # ## ###
### ## # ## ######## #### ## ## # ## # ## ##### #
## # ### ## ### ## ### # ###### ##### ## ##### ###
##### ## # ##### # ### ## # #### ### # ##### # ## #
## # ##### ## ### ## # ####### # ## # #### # ## #####
### ## ## #### ## # ##### ## # ## ##### ## # ## ##### #
## # ###### ## # ##### ## # ### ##### ## #### ## ##### # ##
####### # ### #### #### ## ## # ## #### ## ###### # ## ###
## # #### ### # ## # ######## ### ## # ### ## # ## ##### #
### ## ## ### # ## ### #### # ## # ### #### #### ## ##### ###
## # ### ### ## ######## ### # ######### ### ### # ##### # ## #
##### ## ### ###### ### # ## ## ### # ## # ## ## # ## #####
## # ##### ### # ## ###### ### ## ############## ## ##### #
### #### ### # ## ##### # ## # ##### # ##### # ##
## # ## # ## ### ### ## # ## ##### ## # #### # ## ###
######## ## ##### # ## # ### ## ##### # ### ## ## # ## ##### #
## ###### ######## ## # ### ## # ## ## # ### ### ## ##### ###
### ## # ## ######## #### ## ### ####### # ## ###### # ## #
## # ### ## ### ## ### # ##### ### ### ##### # ## #####
##### ## # ##### # ### ## # #### ### # ## # ## # ## ##### #
## # ##### ## ### ## # ####### # ## ### ##### ### ## ##### # ##
### ## ## #### ## # ##### ## # ## ##### # ## # ## # ### ## # ## ###
## # ###### ## # ##### ## # ### ####### ### ### ## ####### #### ## ##### #
####### # ### #### #### ## ## # ## # ## ### # ### ## ### # ##### ###
## # #### ### # ## # ######## ### ## ##### ##### # ### ## # #### # ## #
### ## ## ### # ## ### #### # ## # ##### ### ##### # ####### # ## #####
## # ### ### ## ######## ### # ####### ## # ## # ## ### ## # ## ##### #
##### ## ### ###### ### # ## ## # ### ## ##### ### ## # ### ####### # ##
## # ##### ### # ## ###### ### #### # ##### ### # ####### # ## # ## ###
### #### ### # ## ##### # ## # ## ##### # ## ##### ### ### ## ##### #
## # ## # ## ### ### ## # ## ##### ### ## # ## ##### # ## # ## # ##### ###
######## ## ##### # ## # ### ## ##### # ## #### ## ##### # ## ########## ## # ## #
## ###### ######## ## # ### ## # ####### # ##### # ## ### ## # ### ## #####
### ## # ## ######## #### ## ## # #### # ## ##### # ### #### # ##### #
# # ### ## ### ## ### # ###### #### # ## ##### ##### # ## ##### # ##
### ## # ##### # ### ## # #### # ## # ## ##### # ## ### ### ## # ## ###