syntax_highlight_example.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <html><head><style>
  2. .str { color: #0d3; }
  3. .comm { color: #888; }
  4. .prep { color: #00f; }
  5. .ptr { color: #b80; }
  6. .dir { color: #08e; }
  7. .name { color: #da0; }
  8. .con { color: #f00; }
  9. </style></head><body>
  10. <p>Example of syntax-highlighted comun program (highlighted with a comun program in example directory):</p>
  11. <pre>
  12. <span class='comm'># small comun library of generally useful functions</span>
  13. <span class='comm'># by drummyfish, released under CC0 1.0, public domain</span>
  14. <span class='comm'># TODO: exp? strfind</span>
  15. <span class='comm'># internal library pointers</span>
  16. <span class='dir'>~_mLibPtrA:0</span> <span class='comm'># tmp pointer</span>
  17. <span class='dir'>~_mLibPtrB:0</span> <span class='comm'># tmp pointer</span>
  18. <span class='dir'>~_mLibTmpMem:3</span> <span class='comm'># tiny tmp memory</span>
  19. <span class='dir'>~_mLibVarA:0</span> <span class='comm'># tmp variable, 2nd cell of tmp memory</span>
  20. <span class='dir'>~_mLibVarB:0</span> <span class='comm'># tmp variable, 3rd cell of tmp memory</span>
  21. <span class='ptr'>$_mLibTmpMem&gt;_mLibVarA</span>
  22. <span class='ptr'>$&gt;_mLibVarA</span>
  23. <span class='ptr'>$_mLibVarA&gt;_mLibVarB</span>
  24. <span class='ptr'>$&gt;_mLibVarB</span>
  25. <span class='comm'># ========== GENERAL ===========================================================</span>
  26. <span class='name'>abs:</span> <span class='ptr'>$0</span> -1</span> &lt;&lt;</span> <span class='con'>?</span> -1</span> *</span> <span class='con'>.</span> <span class='con'>.</span> <span class='comm'># Absolute value.</span>
  27. <span class='name'>min:</span> &gt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Minimum of two values.</span>
  28. <span class='name'>max:</span> &lt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Maximum of two vlaues.</span>
  29. <span class='name'>minS:</span> &gt;&gt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Minimum of two signed values.</span>
  30. <span class='name'>maxS:</span> &lt;&lt;'</span> <span class='con'>?</span> &gt;&lt;</span> <span class='con'>.</span> ^</span> <span class='con'>.</span> <span class='comm'># Maximum of two signed values.</span>
  31. <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>
  32. <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>
  33. <span class='comm'># Interpolates from $3 to $2 by a factor of $1/$0.</span>
  34. <span class='name'>lerp:</span>
  35. <span class='ptr'>$3</span> <span class='ptr'>$3</span> &gt;</span> <span class='con'>?</span> <span class='comm'># if going from higher to lower, swap</span>
  36. <span class='ptr'>$3</span> <span class='ptr'>$3</span> <span class='ptr'>$:5</span> <span class='ptr'>$:3</span>
  37. <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> <span class='ptr'>$:2</span>
  38. <span class='con'>.</span>
  39. &gt;&lt;</span> <span class='ptr'>$2</span> <span class='ptr'>$4</span> -</span> *</span> &gt;&lt;</span> /</span> &gt;&lt;</span> ^</span> +</span>
  40. <span class='con'>.</span>
  41. <span class='comm'># Hashes input number to a 16 bit value, can also be used as a pseudorandom</span>
  42. <span class='comm'># generator.</span>
  43. <span class='name'>hash:</span>
  44. <span class='ptr'>$0</span> +b010000000</span> *</span> +</span> +xffff</span> %</span>
  45. <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
  46. <span class='ptr'>$0</span> +b000001000</span> *</span> +</span> +xffff</span> %</span>
  47. <span class='ptr'>$0</span> +b000000100</span> /</span> |!</span> +xffff</span> %</span>
  48. <span class='ptr'>$0</span> +b000010000</span> *</span> +</span> +xffff</span> %</span>
  49. <span class='ptr'>$0</span> +b100000000</span> /</span> |!</span> +xffff</span> %</span>
  50. <span class='con'>.</span>
  51. <span class='comm'># ========== STRINGS ===========================================================</span>
  52. <span class='comm'># String is a sequence of ASCII character codes, considered starting stack top</span>
  53. <span class='comm'># and continuing downwards, ending with value 0.</span>
  54. <span class='comm'># Reads newline-terminated string from input, pushes it as a zero terminated</span>
  55. <span class='comm'># string in reversed order, without the newline.</span>
  56. <span class='name'>strRead:</span>
  57. 0</span>
  58. <span class='con'>@@</span>
  59. &lt;-</span>
  60. <span class='ptr'>$0</span> 10</span> =</span> <span class='con'>?</span>
  61. ^</span> !@</span>
  62. <span class='con'>.</span>
  63. <span class='con'>.</span>
  64. <span class='name'>strReverse</span>
  65. <span class='con'>.</span>
  66. <span class='comm'># Prints values from memory stack up to stack top.</span>
  67. <span class='name'>stackPrint:</span>
  68. <span class='ptr'>$$</span> <span class='name'>arrayPrint</span>
  69. <span class='con'>.</span>
  70. <span class='comm'># Computes string length.</span>
  71. <span class='name'>strLen:</span>
  72. <span class='ptr'>$0&gt;_mLibPtrA</span>
  73. 0</span>
  74. <span class='con'>@@</span>
  75. <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
  76. !@</span>
  77. <span class='con'>.</span>
  78. ++</span>
  79. <span class='ptr'>$&lt;_mLibPtrA</span>
  80. <span class='con'>.</span>
  81. <span class='con'>.</span>
  82. <span class='comm'># Expects two strings which won't be popped, pushes 1 if they're equal, else 0.</span>
  83. <span class='name'>strEq:</span>
  84. <span class='ptr'>$0&gt;_mLibPtrA</span>
  85. <span class='ptr'>$0&gt;_mLibPtrB</span>
  86. <span class='con'>@@</span>
  87. <span class='ptr'>$_mLibPtrB</span> 0</span> =</span> <span class='con'>?</span>
  88. !@</span>
  89. <span class='con'>.</span>
  90. <span class='ptr'>$&lt;_mLibPtrB</span>
  91. <span class='con'>.</span>
  92. <span class='ptr'>$&lt;_mLibPtrB</span>
  93. <span class='con'>@@</span>
  94. <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span> !=</span> <span class='con'>?</span>
  95. 0</span> !.</span>
  96. <span class='con'>.</span>
  97. <span class='ptr'>$_mLibPtrA</span> 0</span> =</span> <span class='con'>?</span>
  98. 1</span> !.</span>
  99. <span class='con'>.</span>
  100. <span class='ptr'>$&lt;_mLibPtrA</span>
  101. <span class='ptr'>$&lt;_mLibPtrB</span>
  102. <span class='con'>.</span>
  103. <span class='con'>.</span>
  104. <span class='comm'># Left-pads a string to length $1 with character $0.</span>
  105. <span class='name'>strPad:</span>
  106. <span class='ptr'>$:_mLibVarA</span>
  107. <span class='ptr'>$:_mLibVarB</span>
  108. <span class='name'>strLen</span> <span class='ptr'>$_mLibVarB</span> &gt;&lt;</span> -</span>
  109. <span class='con'>@@</span>
  110. <span class='ptr'>$0</span> 0</span> &lt;&lt;=</span> <span class='con'>?</span>
  111. ^</span> !@</span>
  112. <span class='con'>.</span>
  113. <span class='ptr'>$_mLibVarA</span> &gt;&lt;</span>
  114. --</span>
  115. <span class='con'>.</span>
  116. <span class='con'>.</span>
  117. <span class='comm'># Reverses a zero-terminated string.</span>
  118. <span class='name'>strReverse:</span>
  119. <span class='name'>strLen</span> <span class='name'>arrayReverse</span> ^</span>
  120. <span class='con'>.</span>
  121. <span class='comm'># ========== MATH ==============================================================</span>
  122. <span class='comm'># Power: pops 2, pushes $1 raised to $0.</span>
  123. <span class='name'>pow:</span>
  124. 1</span> &gt;&lt;</span>
  125. <span class='con'>@'</span>
  126. &gt;&lt;</span> <span class='ptr'>$2</span> *</span> &gt;&lt;</span>
  127. --</span>
  128. <span class='con'>.</span>
  129. ^</span> &gt;&lt;</span> ^</span>
  130. <span class='con'>.</span>
  131. <span class='comm'># Square roots a number using binary search.</span>
  132. <span class='name'>sqrt:</span>
  133. 0</span> <span class='ptr'>$1</span> 2</span> /</span> <span class='comm'># stack: x lower upper</span>
  134. <span class='con'>@@</span>
  135. <span class='ptr'>$0</span> <span class='ptr'>$2</span> -</span> 1</span> &lt;=</span> <span class='con'>?</span>
  136. !@</span>
  137. <span class='con'>.</span>
  138. <span class='ptr'>$1</span> <span class='ptr'>$1</span> +</span> 2</span> /</span>
  139. <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span> <span class='comm'># stack: x lower upper middle middle^2</span>
  140. <span class='ptr'>$4</span> &gt;</span> <span class='con'>?</span>
  141. <span class='ptr'>$:1</span>
  142. <span class='con'>;</span>
  143. <span class='ptr'>$:2</span>
  144. <span class='con'>.</span>
  145. <span class='con'>.</span>
  146. <span class='ptr'>$0</span> <span class='ptr'>$0</span> *</span> <span class='ptr'>$3</span> &lt;=</span> <span class='con'>?</span>
  147. &gt;&lt;</span>
  148. <span class='con'>.</span>
  149. ^</span> &gt;&lt;</span> ^</span>
  150. <span class='con'>.</span>
  151. <span class='comm'># Computes integer logarithm of $0 with base $1.</span>
  152. <span class='name'>log:</span>
  153. 0</span> <span class='comm'># result</span>
  154. 1</span> <span class='comm'># exponentiated value</span>
  155. 1</span> <span class='comm'># previous exponentiated value</span>
  156. <span class='con'>@@</span>
  157. <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>
  158. !@</span>
  159. <span class='con'>.</span>
  160. <span class='ptr'>$2</span> ++</span> <span class='ptr'>$:3</span> <span class='comm'># compute next power</span>
  161. <span class='ptr'>$1</span> <span class='ptr'>$5</span> *</span> <span class='ptr'>$:2</span>
  162. <span class='ptr'>$1</span> <span class='ptr'>$4</span> &gt;</span> <span class='ptr'>$2</span> <span class='ptr'>$2</span> &lt;=</span> ||</span> <span class='con'>?</span> <span class='comm'># new power &gt; x or new &lt;= previous (overflow)?</span>
  163. <span class='ptr'>$2</span> --</span> <span class='ptr'>$:3</span>
  164. !@</span>
  165. <span class='con'>.</span>
  166. <span class='ptr'>$1</span> <span class='ptr'>$:1</span> <span class='comm'># current -&gt; previous</span>
  167. <span class='con'>.</span>
  168. ^</span> ^</span> &gt;&lt;</span> ^</span> &gt;&lt;</span> ^</span>
  169. <span class='con'>.</span>
  170. <span class='comm'># Computes 8bit sine function approximation with quadratic curve, value 256 on</span>
  171. <span class='comm'># input represents the whole period (2 * pi), output value is between 0 and 255.</span>
  172. <span class='name'>sin8:</span>
  173. <span class='ptr'>$0</span> 256</span> %</span> 64</span> /</span> <span class='comm'># quadrant</span>
  174. &gt;&lt;</span> 64</span> %</span>
  175. <span class='ptr'>$1</span> 2</span> %</span> 1</span> =</span> <span class='con'>?</span>
  176. 63</span> &gt;&lt;</span> -</span>
  177. <span class='con'>.</span>
  178. 63</span> -</span> <span class='ptr'>$0</span> *</span> 32</span> /</span>
  179. &gt;&lt;</span> 1</span> &lt;=</span> <span class='con'>?</span>
  180. 255</span> &gt;&lt;</span> -</span>
  181. <span class='con'>.</span>
  182. <span class='con'>.</span>
  183. <span class='comm'># Computes 8bit cosine function approximation.</span>
  184. <span class='name'>cos8:</span>
  185. 64</span> +</span> <span class='name'>sin8</span>
  186. <span class='con'>.</span>
  187. <span class='comm'># Computes factorial recursively.</span>
  188. <span class='name'>fact:</span>
  189. <span class='con'>?'</span>
  190. <span class='ptr'>$0</span> --</span> <span class='name'>fact</span> *</span>
  191. <span class='con'>;</span>
  192. ^</span> 1</span>
  193. <span class='con'>.</span>
  194. <span class='con'>.</span>
  195. <span class='comm'># Checkes if number is a prime.</span>
  196. <span class='name'>isPrime:</span>
  197. <span class='ptr'>$0</span> 1</span> &lt;=</span>
  198. <span class='con'>?</span>
  199. ^</span> 0</span> <span class='comm'># 0 and 1 are not primes</span>
  200. <span class='con'>;</span>
  201. <span class='ptr'>$0</span> 2</span> /</span>
  202. <span class='con'>@@</span>
  203. <span class='ptr'>$0</span> 1</span> &lt;=</span>
  204. <span class='con'>?</span>
  205. ^</span> ^</span> 1</span> !.</span>
  206. <span class='con'>.</span>
  207. <span class='ptr'>$1</span> <span class='ptr'>$1</span> %</span> 0</span> =</span>
  208. <span class='con'>?</span>
  209. ^</span> ^</span> 0</span> !.</span>
  210. <span class='con'>.</span>
  211. --</span>
  212. <span class='con'>.</span>
  213. <span class='con'>.</span>
  214. <span class='con'>.</span>
  215. <span class='comm'># ========== NUMBER PRINTING AND CONVERSION TO/FROM STRINGS ====================</span>
  216. <span class='comm'># Internal function, converts number to base passed in variable A.</span>
  217. <span class='name'>_numToStrBase:</span>
  218. 0</span> &gt;&lt;</span>
  219. <span class='con'>@@</span>
  220. <span class='ptr'>$0</span> <span class='ptr'>$_mLibVarA</span> %</span>
  221. <span class='ptr'>$0</span> 10</span> &lt;</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>
  222. +</span> &gt;&lt;</span>
  223. <span class='ptr'>$_mLibVarA</span> /</span>
  224. <span class='ptr'>$0</span> 0</span> =</span> <span class='con'>?</span>
  225. !@</span>
  226. <span class='con'>.</span>
  227. <span class='con'>.</span>
  228. ^</span>
  229. <span class='con'>.</span>
  230. <span class='name'>numToStrBin:</span> 2</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
  231. <span class='name'>numToStrHex:</span> 16</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
  232. <span class='name'>numToStr:</span> 10</span> <span class='ptr'>$:_mLibVarA</span> <span class='name'>_numToStrBase</span> <span class='con'>.</span>
  233. <span class='comm'># Converts a signed number to zero-terminated string.</span>
  234. <span class='name'>numToStrS:</span>
  235. <span class='ptr'>$0</span> 0</span> &lt;&lt;</span>
  236. <span class='con'>?</span>
  237. -1</span> &gt;&lt;</span> -</span> 1</span> +</span>
  238. <span class='name'>numToStr</span>
  239. <span class='str'>"-"</span>
  240. <span class='con'>;</span>
  241. <span class='name'>numToStr</span>
  242. <span class='con'>.</span>
  243. <span class='con'>.</span>
  244. <span class='name'>numPrint:</span> <span class='name'>numToStr</span> --&gt;</span> <span class='con'>.</span>
  245. <span class='name'>numPrintS:</span> <span class='name'>numToStrS</span> --&gt;</span> <span class='con'>.</span>
  246. <span class='name'>numPrintHex:</span> <span class='name'>numToStrHex</span> --&gt;</span> <span class='con'>.</span>
  247. <span class='name'>numPrintBin:</span> <span class='name'>numToStrBin</span> --&gt;</span> <span class='con'>.</span>
  248. <span class='comm'># Converts number from decadic string (zero terminated reverse pushed on stack),</span>
  249. <span class='comm'># negative numbers are also supported, validity is not checked.</span>
  250. <span class='name'>numFromStr:</span>
  251. <span class='ptr'>$0</span> <span class='str'>"-"</span> =</span> <span class='con'>?</span>
  252. ^</span> <span class='name'>numFromStr</span> -1</span> *</span>
  253. <span class='con'>;</span>
  254. 0</span>
  255. <span class='con'>@@</span>
  256. <span class='ptr'>$1</span> 0</span> =</span> <span class='con'>?</span>
  257. !@</span>
  258. <span class='con'>.</span>
  259. 10</span> *</span>
  260. <span class='str'>"0"</span> -</span> +</span>
  261. <span class='con'>.</span>
  262. &gt;&lt;</span> ^</span>
  263. <span class='con'>.</span>
  264. <span class='con'>.</span>
  265. <span class='comm'># ========== ARRAYS ============================================================</span>
  266. <span class='comm'># Array here means N valus on the stack and the value N pushed afterwards.</span>
  267. <span class='comm'># Functions working with arrays normally leave this unchanged without popping,</span>
  268. <span class='comm'># e.g. sorting an array will just change orders of the items in the array.</span>
  269. <span class='comm'># Prints out given array (from lowest memory address towards stack top).</span>
  270. <span class='name'>arrayPrint:</span>
  271. <span class='ptr'>$0&gt;_mLibPtrA</span>
  272. <span class='ptr'>$:_mLibVarA'</span>
  273. <span class='con'>@@</span> <span class='comm'># move pointer A to bottom of array</span>
  274. <span class='ptr'>$_mLibVarA</span>
  275. <span class='con'>?'</span>
  276. --</span> <span class='ptr'>$:_mLibVarA</span>
  277. <span class='ptr'>$&lt;_mLibPtrA</span>
  278. <span class='con'>;</span>
  279. ^</span> !@</span>
  280. <span class='con'>.</span>
  281. <span class='con'>.</span>
  282. <span class='con'>@@</span> <span class='comm'># now go up and print</span>
  283. <span class='ptr'>$_mLibPtrA=0</span> 0</span> =</span> <span class='con'>?</span>
  284. !@</span>
  285. <span class='con'>.</span>
  286. <span class='ptr'>$_mLibPtrA</span> <span class='name'>printNum</span> <span class='str'>" "</span> -&gt;</span>
  287. <span class='ptr'>$&gt;_mLibPtrA</span>
  288. <span class='con'>.</span>
  289. <span class='con'>.</span>
  290. <span class='comm'># Reverses items in given array.</span>
  291. <span class='name'>arrayReverse:</span>
  292. <span class='ptr'>$0&gt;_mLibPtrA</span>
  293. <span class='ptr'>$:_mLibVarA'</span>
  294. <span class='con'>@@</span>
  295. <span class='ptr'>$_mLibVarA</span>
  296. <span class='con'>?'</span>
  297. --</span> <span class='ptr'>$:_mLibVarA</span>
  298. <span class='ptr'>$&lt;_mLibPtrA</span>
  299. <span class='con'>;</span>
  300. ^</span> !@</span>
  301. <span class='con'>.</span>
  302. <span class='con'>.</span>
  303. <span class='ptr'>$0&gt;_mLibPtrB</span>
  304. <span class='ptr'>$&lt;_mLibPtrB</span>
  305. <span class='con'>@@</span>
  306. <span class='ptr'>$_mLibPtrA=_mLibPtrB</span> 2</span> !=</span> <span class='con'>?</span>
  307. !.</span>
  308. <span class='con'>.</span>
  309. <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$_mLibPtrB</span>
  310. <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$:_mLibPtrB</span>
  311. <span class='ptr'>$&gt;_mLibPtrA</span>
  312. <span class='ptr'>$&lt;_mLibPtrB</span>
  313. <span class='con'>.</span>
  314. <span class='con'>.</span>
  315. <span class='comm'># Sorts an array in ascending order (towards stack top) with bubble sort.</span>
  316. <span class='name'>arraySort:</span>
  317. <span class='ptr'>$0</span> --</span>
  318. <span class='con'>@'</span>
  319. <span class='ptr'>$0&gt;_mLibPtrA</span>
  320. <span class='ptr'>$&lt;_mLibPtrA</span>
  321. <span class='ptr'>$&lt;_mLibPtrA</span>
  322. <span class='ptr'>$0</span>
  323. <span class='con'>@'</span>
  324. <span class='ptr'>$_mLibPtrA</span> <span class='ptr'>$&lt;_mLibPtrA</span> <span class='ptr'>$_mLibPtrA</span>
  325. &lt;'</span> <span class='con'>?</span>
  326. &gt;&lt;</span>
  327. <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$&gt;_mLibPtrA</span> <span class='ptr'>$:_mLibPtrA</span> <span class='ptr'>$&lt;_mLibPtrA</span>
  328. <span class='con'>;</span>
  329. ^</span> ^</span>
  330. <span class='con'>.</span>
  331. --</span>
  332. <span class='con'>.</span>
  333. ^</span>
  334. --</span>
  335. <span class='con'>.</span>
  336. ^</span>
  337. <span class='con'>.</span>
  338. <span class='comm'># Sorts an array in descending order.</span>
  339. <span class='name'>arraySortDesc:</span>
  340. <span class='name'>arraySort</span> <span class='name'>arrayReverse</span>
  341. <span class='con'>.</span>
  342. </pre></body></html>