'basic definitions
'-----------------
0=[-];                  
zero(a)=$map(a) 0;      'a=0
inc(a)=                 'a=a+1
  $map(a) +;
dec(a)=                 'a=a-1
  $map(a) -;
push(a)=                'push a
  $map(a) ^;            
pop(a)=                 'pop a
  $map(a) v;            

'loops
'-----
for(a)=                 'for a=a to 1 step -1
  $map(a) [;
next(a)=                'next a
  $map(a) -];

'data relocation
'---------------
move(source,dest)=      'dest=source:source=0
  push(source)
  pop(dest)
  zero(source);
copy(source,dest)=      'dest=source
  push(source)
  pop(d);
swap(source1,source2)=  'swap s1,s2
  push(source2)
  move(source2,source1)
  pop(source1);

'math
'----
add(source,dest)=       'dest=dest+source:source=0
  for(source)
    inc(dest) 
  next(source);
add2(s,d1,d2)=          'd1=d1+s:d2=d2+s:s=0
  for(s)
    inc(d1) 
    inc(d2) 
  next(s);
double(s,d)=            'd=s*2
  add2(s,d,d);
'multiply(s1,s2,d,t)=    'd=s1*s2:s2=0
'  for(s1)
'    copy(s2,d,t)
'  next(s1)
'  zero(s2);
mul(s1,s2,d)=           'd=s1*s2
  push(s1)
  for(s1)
    push(s2)
    add(s2,d)
    pop(s2)
  next(s1)
  pop(s2)

'MUL     >[-]>[-]<< <[>[>+>+<<-] >[<+>-] <-] >>>[<<<+>>>-]<<<
'        > >[-]>[-]<< <[>[>+>+<<-] >[<+>-] <-] >>>[<<<+>>>-]<<<<
'        > >[-]>[-]<< <
'        [
'         >[>+>+<<-] >
'          [<+>-] <
'        -]
'        >>>[<<<+>>>-]<<<
'mul(a,b,c,d)=
'  zero(b)
'  zero(c)
'  for(a)
'    add2(b,c,d)
'    add(b,a)
'  next(a)
'  add(d,a)

'decisions
'---------
if(a)=                  'if a<>0 then
  to(a) [;
endif(a)=               'end if
  zero(a) ];
ifelse(a,t)=            'if a<>0 then 
  inc(t)
  if(a)
  dec(t);
else(a,t)=              'else
  endif(a)
  if(t);
endelse(t)=             'end if
  dec(t) ];

'logic
'-----
or(s1,s2,d)=
  move(s1,d)
  move(s2,d);
and(s1,s2,d)=
  if(s1)
    move(s2,d)
  endif(s1)
  zero(s2);
not(s,d)=
  inc(d)
  if(s)
    dec(d)
  endif(s);

'integer addition
'----------------
1=+;
2=1 +;
3=2 +;
4=3 +;
5=4 +;
6=5 +;
7=6 +;
8=7 +;
9=8 +;
10=9 +;
11=10 +;
12=11 +;
13=12 +;
14=13 +;
15=14 +;
16=>^ 0 4 [< 4 >-]v<;
17=16 +;
18=>^ 0 3 [< 6 >-]v<;
19=18 +;
20=>^ 0 4 [< 5 >-]v<;
21=20 +;
22=21 +;
24=>^ 0 4 [< 6 >-]v<;
23=24 -;
25=>^ 0 5 [< 5 >-]v<;
26=25 +;
28=>^ 0 4 [< 7 >-]v<;
27=28 -;
29=28 +;
30=>^ 0 5 [< 6 >-]v<;
31=30 +;
32=>^ 0 4 [< 8 >-]v<;
33=32 +;
35=>^ 0 5 [< 7 >-]v<;
34=35 -;
36=>^ 0 6 [< 6 >-]v<;
37=36 +;
38=37 +;
40=>^ 0 5 [< 8 >-]v<;
39=40 -;
41=40 +;
42=>^ 0 6 [< 7 >-]v<;
43=42 +;
45=>^ 0 5 [< 9 >-]v<;
44=45 -;
46=45 +;
48=>^ 0 6 [< 8 >-]v<;
47=48 -;
49=>^ 0 7 [< 7 >-]v<;
50=49 +;
51=50 +;
54=>^ 0 6 [< 9 >-]v<;
53=54 -;
52=53 -;
55=54 +;
56=>^ 0 7 [< 8 >-]v<;
57=56 +;
58=57 +;
60=>^ 0 6 [< 10 >-]v<;
59=60 -;
61=60 +;
63=>^ 0 7 [< 9 >-]v<;
62=63 -;
64=>^ 0 8 [< 8 >-]v<;
65=64 +;
66=>^ 0 6 [< 11 >-]v<;
67=66 +;
68=67 +;
70=>^ 0 7 [< 10 >-]v<;
69=70 -;
71=70 +;
72=>^ 0 8 [< 9 >-]v<;
73=72 +;
74=73 +;
77=>^ 0 7 [< 11 >-]v<;
76=77 -;
75=76 -;
78=77 +;
80=>^ 0 8 [< 10 >-]v<;
79=80 -;
81=>^ 0 9 [< 9 >-]v<;
82=81 +;
84=>^ 0 7 [< 12 >-]v<;
83=84 -;
85=84 +;
86=85 +;
88=>^ 0 8 [< 11 >-]v<;
87=88 -;
89=88 +;
90=>^ 0 9 [< 10 >-]v<;
91=90 +;
92=91 +;
93=92 +;
96=>^ 0 8 [< 12 >-]v<;
95=96 -;
94=95 -;
97=96 +;
99=>^ 0 9 [< 11 >-]v<;
98=99 -;
100=>^ 0 10 [< 10 >-]v<;
101=100 +;
102=101 +;
104=>^ 0 8 [< 13 >-]v<;
103=104 -;
105=104 +;
106=105 +;
108=>^ 0 9 [< 12 >-]v<;
107=108 -;
109=108 +;
110=>^ 0 10 [< 11 >-]v<;
111=110 +;
112=>^ 0 8 [< 14 >-]v<;
113=112 +;
114=113 +;
117=>^ 0 9 [< 13 >-]v<;
116=117 -;
115=116 -;
118=117 +;
119=120 -;
120=>^ 0 10 [< 12 >-]v<;
121=>^ 0 11 [< 11 >-]v<;
122=121 +;
123=122 +;
126=>^ 0 9 [< 14 >-]v<;
125=126 -;
124=125 -;
127=126 +;
128=127 +;
n126=>^ 0 9 [< n14 >-]v<;
n127=n126 -;
n125=n126 +;

'ascii
'-----
" "=0 32;
'"!"=0 33; '
"""=0 34;
"#"=0 35;
'"$"=0 36; '
"%"=0 37;
"&"=0 38;
'"'"=0 39; '
'"("=0 40; '
'")"=0 41; '
"*"=0 42;
'"+"=0 43; '
'","=0 44; '
'"-"=0 45; '
'"."=0 46; '
"/"=0 47;
"0"=0 48;
"1"=0 49;
"2"=0 50;
"3"=0 51;
"4"=0 52;
"5"=0 53;
"6"=0 54;
"7"=0 55;
"8"=0 56;
"9"=0 57;
":"=0 58;
'";"=0 59; '
'"<"=0 60; '
'"="=0 61; '
'">"=0 62; '
"?"=0 63;
"@"=0 64;
"A"=0 65;
"B"=0 66;
"C"=0 67;
"D"=0 68;
"E"=0 69;
"F"=0 70;
"G"=0 71;
"H"=0 72;
"I"=0 73;
"J"=0 74;
"K"=0 75;
"L"=0 76;
"M"=0 77;
"N"=0 78;
"O"=0 79;
"P"=0 80;
"Q"=0 81;
"R"=0 82;
"S"=0 83;
"T"=0 84;
"U"=0 85;
"V"=0 86;
"W"=0 87;
"X"=0 88;
"Y"=0 89;
"Z"=0 90;
'"["=0 91; '
"\"=0 92;
'"]"=0 93; '
'"^"=0 94; '
"_"=0 95;
"`"=0 96;
"a"=0 97;
"b"=0 98;
"c"=0 99;
"d"=0 100;
"e"=0 101;
"f"=0 102;
"g"=0 103;
"h"=0 104;
"i"=0 105;
"j"=0 106;
"k"=0 107;
"l"=0 108;
"m"=0 109;
"n"=0 110;
"o"=0 111;
"p"=0 112;
"q"=0 113;
"r"=0 114;
"s"=0 115;
"t"=0 116;
"u"=0 117;
'"v"=0 118; '
"w"=0 119;
"x"=0 120;
"y"=0 121;
"z"=0 122;
'"{"=0 123; '
"|"=0 124;
'"}"=0 125; '
"~"=0 126;

'integer multiplication
'----------------------
*0=0;
*1=;
*2=>[-][<-> 2 ][<+>-];
*3=>[-][<-> 3 ][<+>-];
*4=>[-][<-> 4 ][<+>-];
*5=>[-][<-> 5 ][<+>-];
*6=>[-][<-> 6 ][<+>-];
*7=>[-][<-> 7 ][<+>-];
*8=>[-][<-> 8 ][<+>-];
*9=>[-][<-> 9 ][<+>-];
*10=>[-][<-> 10 ][<+>-];
*11=>[-][<-> 11 ][<+>-];
*12=>[-][<-> 12 ][<+>-];
*13=>[-][<-> 13 ][<+>-];
*14=>[-][<-> 14 ][<+>-];
*15=>[-][<-> 15 ][<+>-];
*16=>[-][<-> 16 ][<+>-];
*17=>[-][<-> 17 ][<+>-];
*18=>[-][<-> 18 ][<+>-];
*19=>[-][<-> 19 ][<+>-];
*20=>[-][<-> 20 ][<+>-];
*21=>[-][<-> 21 ][<+>-];
*22=>[-][<-> 22 ][<+>-];
*23=>[-][<-> 23 ][<+>-];
*24=>[-][<-> 24 ][<+>-];
*25=>[-][<-> 25 ][<+>-];
*26=>[-][<-> 26 ][<+>-];
*27=>[-][<-> 27 ][<+>-];
*28=>[-][<-> 28 ][<+>-];
*29=>[-][<-> 29 ][<+>-];

