123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <html><head><style>
- .str { color: #0d3; }
- .comm { color: #888; }
- .prep { color: #00f; }
- .ptr { color: #b80; }
- .dir { color: #08e; }
- .name { color: #da0; }
- .con { color: #f00; }
- </style></head><body>
- <p>Example of syntax-highlighted comun program (highlighted with a comun program in example directory):</p>
- <pre>
- <span class='comm'># small comun library of generally useful functions</span>
- <span class='comm'># by drummyfish, released under CC0 1.0, public domain</span>
- <span class='comm'># TODO: exp? strfind</span>
- <span class='comm'># internal library pointers</span>
- <span class='dir'>~_mLibPtrA:0</span> <span class='comm'># tmp pointer</span>
- <span class='dir'>~_mLibPtrB:0</span> <span class='comm'># tmp pointer</span>
- <span class='dir'>~_mLibTmpMem:3</span> <span class='comm'># tiny tmp memory</span>
- <span class='dir'>~_mLibVarA:0</span> <span class='comm'># tmp variable, 2nd cell of tmp memory</span>
- <span class='dir'>~_mLibVarB:0</span> <span class='comm'># tmp variable, 3rd cell of tmp memory</span>
- <span class='ptr'>$_mLibTmpMem>_mLibVarA</span>
- <span class='ptr'>$>_mLibVarA</span>
- <span class='ptr'>$_mLibVarA>_mLibVarB</span>
- <span class='ptr'>$>_mLibVarB</span>
- <span class='comm'># ========== GENERAL ===========================================================</span>
- <span class='name'>abs:</span> <span class='ptr'>$0</span> -1</span> <<</span> <span class='con'>?</span> -1</span> *</span> <span class='con'>.</span> <span class='con'>.</span> <span class='comm'># Absolute value.</span>
- <span class='name'>min:</span> >'</span> <span class='con'>?</span> ><</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Minimum of two values.</span>
- <span class='name'>max:</span> <'</span> <span class='con'>?</span> ><</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Maximum of two vlaues.</span>
- <span class='name'>minS:</span> >>'</span> <span class='con'>?</span> ><</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Minimum of two signed values.</span>
- <span class='name'>maxS:</span> <<'</span> <span class='con'>?</span> ><</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Maximum of two signed values.</span>
- <span class='name'>min3:</span> <span class='name'>min</span> <span class='name'>min</span> <span class='con'>.</span> <span class='comm'># Minimum of three values.</span>
- <span class='name'>max3:</span> <span class='name'>max</span> <span class='name'>max</span> <span class='con'>.</span> <span class='comm'># Maximum of three values.</span>
- <span class='comm'># Interpolates from $3 to $2 by a factor of $1/$0.</span>
- <span class='name'>lerp:</span>
- <span class='ptr'>$3</span> <span class='ptr'>$3</span> ></span> <span class='con'>?</span> <span class='comm'># if going from higher to lower, swap</span>
- <span class='ptr'>$3</span> <span class='ptr'>$3</span> <span class='ptr'>$:5</span> <span class='ptr'>$:3</span>
- <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> <span class='ptr'>$:2</span>
- <span class='con'>.</span>
- ><</span> <span class='ptr'>$2</span> <span class='ptr'>$4</span> -</span> *</span> ><</span> /</span> ><</span> ^</span> +</span>
- <span class='con'>.</span>
- <span class='comm'># Hashes input number to a 16 bit value, can also be used as a pseudorandom</span>
- <span class='comm'># generator.</span>
- <span class='name'>hash:</span>
- <span class='ptr'>$0</span> +b010000000</span> *</span> +</span> +xffff</span> %</span>
- <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
- <span class='ptr'>$0</span> +b000001000</span> *</span> +</span> +xffff</span> %</span>
- <span class='ptr'>$0</span> +b000000100</span> /</span> |!</span> +xffff</span> %</span>
- <span class='ptr'>$0</span> +b000010000</span> *</span> +</span> +xffff</span> %</span>
- <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
- <span class='con'>.</span>
- <span class='comm'># ========== STRINGS ===========================================================</span>
- <span class='comm'># String is a sequence of ASCII character codes, considered starting stack top</span>
- <span class='comm'># and continuing downwards, ending with value 0.</span>
- <span class='comm'># Reads newline-terminated string from input, pushes it as a zero terminated</span>
- <span class='comm'># string in reversed order, without the newline.</span>
- <span class='name'>strRead:</span>
- 0</span>
- <span class='con'>@@</span>
- <-</span>
- <span class='ptr'>$0</span> 10</span> =</span> <span class='con'>?</span>
- ^</span> !@</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='name'>strReverse</span>
- <span class='con'>.</span>
- <span class='comm'># Prints values from memory stack up to stack top.</span>
- <span class='name'>stackPrint:</span>
- <span class='ptr'>$$</span> <span class='name'>arrayPrint</span>
- <span class='con'>.</span>
- <span class='comm'># Computes string length.</span>
- <span class='name'>strLen:</span>
- <span class='ptr'>$0>_mLibPtrA</span>
- 0</span>
- <span class='con'>@@</span>
- <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- ++</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Expects two strings which won't be popped, pushes 1 if they're equal, else 0.</span>
- <span class='name'>strEq:</span>
- <span class='ptr'>$0>_mLibPtrA</span>
- <span class='ptr'>$0>_mLibPtrB</span>
- <span class='con'>@@</span>
- <span class='ptr'>$_mLibPtrB</span> 0</span> =</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$<_mLibPtrB</span>
- <span class='con'>.</span>
- <span class='ptr'>$<_mLibPtrB</span>
- <span class='con'>@@</span>
- <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span> !=</span> <span class='con'>?</span>
- 0</span> !.</span>
- <span class='con'>.</span>
- <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
- 1</span> !.</span>
- <span class='con'>.</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='ptr'>$<_mLibPtrB</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Left-pads a string to length $1 with character $0.</span>
- <span class='name'>strPad:</span>
- <span class='ptr'>$:_mLibVarA</span>
- <span class='ptr'>$:_mLibVarB</span>
- <span class='name'>strLen</span> <span class='ptr'>$_mLibVarB</span> ><</span> -</span>
- <span class='con'>@@</span>
- <span class='ptr'>$0</span> 0</span> <<=</span> <span class='con'>?</span>
- ^</span> !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$_mLibVarA</span> ><</span>
- --</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Reverses a zero-terminated string.</span>
- <span class='name'>strReverse:</span>
- <span class='name'>strLen</span> <span class='name'>arrayReverse</span> ^</span>
- <span class='con'>.</span>
- <span class='comm'># ========== MATH ==============================================================</span>
- <span class='comm'># Power: pops 2, pushes $1 raised to $0.</span>
- <span class='name'>pow:</span>
- 1</span> ><</span>
- <span class='con'>@'</span>
- ><</span> <span class='ptr'>$2</span> *</span> ><</span>
- --</span>
- <span class='con'>.</span>
-
- ^</span> ><</span> ^</span>
- <span class='con'>.</span>
- <span class='comm'># Square roots a number using binary search.</span>
- <span class='name'>sqrt:</span>
- 0</span> <span class='ptr'>$1</span> 2</span> /</span> <span class='comm'># stack: x lower upper</span>
- <span class='con'>@@</span>
- <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> 1</span> <=</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$1</span> <span class='ptr'>$1</span> +</span> 2</span> /</span>
- <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span> <span class='comm'># stack: x lower upper middle middle^2</span>
- <span class='ptr'>$4</span> ></span> <span class='con'>?</span>
- <span class='ptr'>$:1</span>
- <span class='con'>;</span>
- <span class='ptr'>$:2</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span> <span class='ptr'>$3</span> <=</span> <span class='con'>?</span>
- ><</span>
- <span class='con'>.</span>
- ^</span> ><</span> ^</span>
- <span class='con'>.</span>
- <span class='comm'># Computes integer logarithm of $0 with base $1.</span>
- <span class='name'>log:</span>
- 0</span> <span class='comm'># result</span>
- 1</span> <span class='comm'># exponentiated value</span>
- 1</span> <span class='comm'># previous exponentiated value</span>
- <span class='con'>@@</span>
- <span class='ptr'>$1</span> <span class='ptr'>$4</span> =</span> <span class='con'>?</span> <span class='comm'># equality has to be checked, considering overflow may happen next</span>
- !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$2</span> ++</span> <span class='ptr'>$:3</span> <span class='comm'># compute next power</span>
- <span class='ptr'>$1</span> <span class='ptr'>$5</span> *</span> <span class='ptr'>$:2</span>
- <span class='ptr'>$1</span> <span class='ptr'>$4</span> ></span> <span class='ptr'>$2</span> <span class='ptr'>$2</span> <=</span> ||</span> <span class='con'>?</span> <span class='comm'># new power > x or new <= previous (overflow)?</span>
- <span class='ptr'>$2</span> --</span> <span class='ptr'>$:3</span>
- !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$1</span> <span class='ptr'>$:1</span> <span class='comm'># current -> previous</span>
- <span class='con'>.</span>
- ^</span> ^</span> ><</span> ^</span> ><</span> ^</span>
- <span class='con'>.</span>
- <span class='comm'># Computes 8bit sine function approximation with quadratic curve, value 256 on</span>
- <span class='comm'># input represents the whole period (2 * pi), output value is between 0 and 255.</span>
- <span class='name'>sin8:</span>
- <span class='ptr'>$0</span> 256</span> %</span> 64</span> /</span> <span class='comm'># quadrant</span>
- ><</span> 64</span> %</span>
- <span class='ptr'>$1</span> 2</span> %</span> 1</span> =</span> <span class='con'>?</span>
- 63</span> ><</span> -</span>
- <span class='con'>.</span>
- 63</span> -</span> <span class='ptr'>$0</span> *</span> 32</span> /</span>
- ><</span> 1</span> <=</span> <span class='con'>?</span>
- 255</span> ><</span> -</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Computes 8bit cosine function approximation.</span>
- <span class='name'>cos8:</span>
- 64</span> +</span> <span class='name'>sin8</span>
- <span class='con'>.</span>
- <span class='comm'># Computes factorial recursively.</span>
- <span class='name'>fact:</span>
- <span class='con'>?'</span>
- <span class='ptr'>$0</span> --</span> <span class='name'>fact</span> *</span>
- <span class='con'>;</span>
- ^</span> 1</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Checkes if number is a prime.</span>
- <span class='name'>isPrime:</span>
- <span class='ptr'>$0</span> 1</span> <=</span>
- <span class='con'>?</span>
- ^</span> 0</span> <span class='comm'># 0 and 1 are not primes</span>
- <span class='con'>;</span>
- <span class='ptr'>$0</span> 2</span> /</span>
- <span class='con'>@@</span>
- <span class='ptr'>$0</span> 1</span> <=</span>
- <span class='con'>?</span>
- ^</span> ^</span> 1</span> !.</span>
- <span class='con'>.</span>
- <span class='ptr'>$1</span> <span class='ptr'>$1</span> %</span> 0</span> =</span>
- <span class='con'>?</span>
- ^</span> ^</span> 0</span> !.</span>
- <span class='con'>.</span>
- --</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># ========== NUMBER PRINTING AND CONVERSION TO/FROM STRINGS ====================</span>
- <span class='comm'># Internal function, converts number to base passed in variable A.</span>
- <span class='name'>_numToStrBase:</span>
- 0</span> ><</span>
- <span class='con'>@@</span>
- <span class='ptr'>$0</span> <span class='ptr'>$_mLibVarA</span> %</span>
- <span class='ptr'>$0</span> 10</span> <</span> <span class='con'>?</span> <span class='str'>"0"</span> <span class='con'>;</span> <span class='str'>"A"</span> 10</span> -</span> <span class='con'>.</span>
- +</span> ><</span>
- <span class='ptr'>$_mLibVarA</span> /</span>
- <span class='ptr'>$0</span> 0</span> =</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- ^</span>
- <span class='con'>.</span>
- <span class='name'>numToStrBin:</span> 2</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
- <span class='name'>numToStrHex:</span> 16</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
- <span class='name'>numToStr:</span> 10</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
- <span class='comm'># Converts a signed number to zero-terminated string.</span>
- <span class='name'>numToStrS:</span>
- <span class='ptr'>$0</span> 0</span> <<</span>
- <span class='con'>?</span>
- -1</span> ><</span> -</span> 1</span> +</span>
- <span class='name'>numToStr</span>
- <span class='str'>"-"</span>
- <span class='con'>;</span>
- <span class='name'>numToStr</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='name'>numPrint:</span> <span class='name'>numToStr</span> --></span> <span class='con'>.</span>
- <span class='name'>numPrintS:</span> <span class='name'>numToStrS</span> --></span> <span class='con'>.</span>
- <span class='name'>numPrintHex:</span> <span class='name'>numToStrHex</span> --></span> <span class='con'>.</span>
- <span class='name'>numPrintBin:</span> <span class='name'>numToStrBin</span> --></span> <span class='con'>.</span>
- <span class='comm'># Converts number from decadic string (zero terminated reverse pushed on stack),</span>
- <span class='comm'># negative numbers are also supported, validity is not checked.</span>
- <span class='name'>numFromStr:</span>
- <span class='ptr'>$0</span> <span class='str'>"-"</span> =</span> <span class='con'>?</span>
- ^</span> <span class='name'>numFromStr</span> -1</span> *</span>
- <span class='con'>;</span>
- 0</span>
- <span class='con'>@@</span>
- <span class='ptr'>$1</span> 0</span> =</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- 10</span> *</span>
- <span class='str'>"0"</span> -</span> +</span>
- <span class='con'>.</span>
- ><</span> ^</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># ========== ARRAYS ============================================================</span>
- <span class='comm'># Array here means N valus on the stack and the value N pushed afterwards.</span>
- <span class='comm'># Functions working with arrays normally leave this unchanged without popping,</span>
- <span class='comm'># e.g. sorting an array will just change orders of the items in the array.</span>
- <span class='comm'># Prints out given array (from lowest memory address towards stack top).</span>
- <span class='name'>arrayPrint:</span>
- <span class='ptr'>$0>_mLibPtrA</span>
- <span class='ptr'>$:_mLibVarA'</span>
- <span class='con'>@@</span> <span class='comm'># move pointer A to bottom of array</span>
- <span class='ptr'>$_mLibVarA</span>
- <span class='con'>?'</span>
- --</span> <span class='ptr'>$:_mLibVarA</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='con'>;</span>
- ^</span> !@</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='con'>@@</span> <span class='comm'># now go up and print</span>
- <span class='ptr'>$_mLibPtrA=0</span> 0</span> =</span> <span class='con'>?</span>
- !@</span>
- <span class='con'>.</span>
- <span class='ptr'>$_mLibPtrA</span> <span class='name'>printNum</span> <span class='str'>" "</span> -></span>
- <span class='ptr'>$>_mLibPtrA</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Reverses items in given array.</span>
- <span class='name'>arrayReverse:</span>
- <span class='ptr'>$0>_mLibPtrA</span>
- <span class='ptr'>$:_mLibVarA'</span>
- <span class='con'>@@</span>
- <span class='ptr'>$_mLibVarA</span>
- <span class='con'>?'</span>
- --</span> <span class='ptr'>$:_mLibVarA</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='con'>;</span>
- ^</span> !@</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='ptr'>$0>_mLibPtrB</span>
- <span class='ptr'>$<_mLibPtrB</span>
- <span class='con'>@@</span>
- <span class='ptr'>$_mLibPtrA=_mLibPtrB</span> 2</span> !=</span> <span class='con'>?</span>
- !.</span>
- <span class='con'>.</span>
- <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span>
- <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$:_mLibPtrB</span>
- <span class='ptr'>$>_mLibPtrA</span>
- <span class='ptr'>$<_mLibPtrB</span>
- <span class='con'>.</span>
- <span class='con'>.</span>
- <span class='comm'># Sorts an array in ascending order (towards stack top) with bubble sort.</span>
- <span class='name'>arraySort:</span>
- <span class='ptr'>$0</span> --</span>
- <span class='con'>@'</span>
- <span class='ptr'>$0>_mLibPtrA</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='ptr'>$<_mLibPtrA</span>
- <span class='ptr'>$0</span>
- <span class='con'>@'</span>
- <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$<_mLibPtrA</span> <span class='ptr'>$_mLibPtrA</span>
- <'</span> <span class='con'>?</span>
- ><</span>
- <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$>_mLibPtrA</span> <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$<_mLibPtrA</span>
- <span class='con'>;</span>
- ^</span> ^</span>
- <span class='con'>.</span>
- --</span>
- <span class='con'>.</span>
- ^</span>
- --</span>
- <span class='con'>.</span>
- ^</span>
- <span class='con'>.</span>
- <span class='comm'># Sorts an array in descending order.</span>
- <span class='name'>arraySortDesc:</span>
- <span class='name'>arraySort</span> <span class='name'>arrayReverse</span>
- <span class='con'>.</span>
- </pre></body></html>
|