Guest User

slist.asm

a guest
Apr 22nd, 2021
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.15 KB | None | 0 0
  1. ; These affect the name of string lists' length constants
  2. def slist_length_prefix equs "LENGTH_"
  3. def slist_length_suffix equs ""
  4.  
  5. ; __slist_length LIST, length
  6. ; For internal use only!
  7. ;
  8. MACRO __slist_length
  9. if def({slist_length_prefix}\1{slist_length_suffix})
  10. purge {slist_length_prefix}\1{slist_length_suffix}
  11. endc
  12. def {slist_length_prefix}\1{slist_length_suffix} equ \2
  13. ENDM
  14.  
  15. ; slist LIST, items...
  16. ; Create a new string list.
  17. ;
  18. ; \1: symbol - The string list to create
  19. ; \2+: string... - The initial items, if any
  20. ;
  21. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  22. ; assert LENGTH_MONS == 3
  23. ; assert !STRCMP("{MONS#1}", "Bulbasaur")
  24. ;
  25. ; slist_item items...
  26. ; Append items to the previously created string list.
  27. ;
  28. ; \1+: string... - The items to append, if any
  29. ;
  30. ; slist MONS
  31. ; slist_item "Bulbasaur"
  32. ; slist_item "Charmander", "Squirtle"
  33. ; assert LENGTH_MONS == 3
  34. ; assert !STRCMP("{MONS#2}", "Charmander")
  35. ;
  36. MACRO slist
  37. assert !def({slist_length_prefix}\1{slist_length_suffix}), \
  38. "slist: string list \1 is already defined"
  39. __slist_length \1, 0
  40. slist_append \#
  41. redef slist_item equs "slist_append \1,"
  42. ENDM
  43.  
  44. ; slist_print LIST
  45. ; Print a string list.
  46. ;
  47. ; \1: symbol - The string list to print
  48. ;
  49. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  50. ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
  51. ; slist_print MONS
  52. ;
  53. MACRO slist_print
  54. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  55. "slist_print: string list \1 is not defined"
  56. print "["
  57. for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
  58. if __i > 1
  59. print ", "
  60. endc
  61. print "\"{\1#{d:__i}}\""
  62. endr
  63. purge __i
  64. print "]"
  65. ENDM
  66.  
  67. ; slist_println LIST
  68. ; Print a string list followed by a newline.
  69. ;
  70. ; \1: symbol - The string list to print
  71. ;
  72. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  73. ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
  74. ; slist_println MONS
  75. ;
  76. MACRO slist_println
  77. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  78. "slist_println: string list \1 is not defined"
  79. slist_print \1
  80. println
  81. ENDM
  82.  
  83. ; slist_append LIST, items...
  84. ; Append items to a string list.
  85. ;
  86. ; \1: symbol - The string list to update
  87. ; \2+: string... - The items to append, if any
  88. ;
  89. ; slist MONS
  90. ; slist_append MONS, "Bulbasaur", "Charmander", "Squirtle"
  91. ; assert LENGTH_MONS == 3
  92. ; assert !STRCMP("{MONS#3}", "Squirtle")
  93. ;
  94. MACRO slist_append
  95. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  96. "slist_append: string list \1 is not defined"
  97. def __n = {slist_length_prefix}\1{slist_length_suffix}
  98. for __i, 2, _NARG + 1
  99. def __j = __n + __i - 1
  100. def \1#{d:__j} equs \<__i>
  101. purge __j
  102. endr
  103. purge __i
  104. __slist_length \1, __n + {_NARG} - 1
  105. purge __n
  106. ENDM
  107.  
  108. ; slist_extend LIST1, LIST2
  109. ; Append items to a string list from another.
  110. ;
  111. ; \1: symbol - The string list to update
  112. ; \2: symbol - The string list to append from
  113. ;
  114. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  115. ; slist MOREMONS, "Pikachu", "Eevee"
  116. ; slist_extend MONS, MOREMONS
  117. ; assert LENGTH_MONS == 5
  118. ;
  119. MACRO slist_extend
  120. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  121. "slist_extend: string list \1 is not defined"
  122. assert def({slist_length_prefix}\2{slist_length_suffix}), \
  123. "slist_extend: string list \2 is not defined"
  124. for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
  125. slist_append \1, "{\2#{d:__i}}"
  126. endr
  127. purge __i
  128. ENDM
  129.  
  130. ; slist_copy LIST1, LIST2
  131. ; Create a new string list as a copy of another string list.
  132. ;
  133. ; \1: symbol - The string list to create
  134. ; \2: symbol - The string list to copy
  135. ;
  136. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  137. ; slist_copy STARTERS, MONS
  138. ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
  139. ; slist_println STARTERS
  140. ;
  141. MACRO slist_copy
  142. assert !def({slist_length_prefix}\1{slist_length_suffix}), \
  143. "slist_copy: string list \1 is already defined"
  144. assert def({slist_length_prefix}\2{slist_length_suffix}), \
  145. "slist_copy: string list \2 is not defined"
  146. __slist_length \1, {slist_length_prefix}\2{slist_length_suffix}
  147. for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
  148. def \1#{d:__i} equs "{\2#{d:__i}}"
  149. endr
  150. purge __i
  151. ENDM
  152.  
  153. ; slist_clear LIST
  154. ; Clear a string list.
  155. ;
  156. ; \1: symbol - The string list to clear
  157. ;
  158. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  159. ; slist_clear MONS
  160. ; assert LENGTH_MONS == 0
  161. ;
  162. MACRO slist_clear
  163. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  164. "slist_clear: string list \1 is not defined"
  165. for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
  166. purge \1#{d:__i}
  167. endr
  168. purge __i
  169. __slist_length \1, 0
  170. ENDM
  171.  
  172. ; slist_purge LIST
  173. ; Purge a string list.
  174. ;
  175. ; \1: symbol - The string list to purge
  176. ;
  177. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  178. ; slist_purge MONS
  179. ; assert !DEF(LENGTH_MONS)
  180. ;
  181. MACRO slist_purge
  182. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  183. "slist_purge: string list \1 is not defined"
  184. slist_clear \1
  185. purge {slist_length_prefix}\1{slist_length_suffix}
  186. ENDM
  187.  
  188. ; slist_set LIST, index, item
  189. ; Set an index of a string list to an item.
  190. ;
  191. ; \1: symbol - The string list to update
  192. ; \2: number - The index to be set
  193. ; \3: string - The item to set
  194. ;
  195. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  196. ; slist_set MONS, 2, "Cyndaquil"
  197. ; assert !STRCMP("{MONS#2}", "Cyndaquil")
  198. ;
  199. MACRO slist_set
  200. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  201. "slist_set: string list \1 is not defined"
  202. def __idx = \2
  203. assert 1 <= __idx && __idx <= {slist_length_prefix}\1{slist_length_suffix}, \
  204. "slist_set: invalid index for string list \1: {d:__idx}"
  205. redef \1#{d:__idx} equs \3
  206. purge __idx
  207. ENDM
  208.  
  209. ; slist_insert LIST, index, item
  210. ; Insert an item into a string list before an index.
  211. ;
  212. ; \1: symbol - The string list to update
  213. ; \2: number - The index to insert before
  214. ; \3: string - The item to insert
  215. ;
  216. ; slist MONS, "Bulbasaur", "Squirtle"
  217. ; slist_insert MONS, 2, "Charmander"
  218. ; assert !STRCMP("{MONS#2}", "Charmander")
  219. ;
  220. MACRO slist_insert
  221. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  222. "slist_insert: string list \1 is not defined"
  223. def __n = {slist_length_prefix}\1{slist_length_suffix}
  224. def __idx = \2
  225. assert 1 <= __idx && __idx <= __n, \
  226. "slist_insert: invalid index for string list \1: {d:__idx}"
  227. for __i, __n + 1, __idx, -1
  228. def __j = __i - 1
  229. def \1#{d:__i} equs "{\1#{d:__j}}"
  230. purge \1#{d:__j}, __j
  231. endr
  232. purge __i
  233. def \1#{d:__idx} equs \3
  234. purge __idx
  235. __slist_length \1, __n + 1
  236. purge __n
  237. ENDM
  238.  
  239. ; slist_delete LIST, index
  240. ; Delete an index from a string list.
  241. ;
  242. ; \1: symbol - The string list to update
  243. ; \2: number - The index to delete
  244. ;
  245. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  246. ; slist_delete MONS, 1
  247. ; assert LENGTH_MONS == 2
  248. ; assert !STRCMP("{MONS#1}", "Charmander")
  249. ;
  250. MACRO slist_delete
  251. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  252. "slist_delete: string list \1 is not defined"
  253. def __n = {slist_length_prefix}\1{slist_length_suffix}
  254. def __idx = \2
  255. assert 1 <= __idx && __idx <= __n, \
  256. "slist_delete: invalid index for string list \1: {d:__idx}"
  257. purge \1#{d:__idx}
  258. for __i, __idx, __n
  259. def __j = __i + 1
  260. def \1#{d:__i} equs "{\1#{d:__j}}"
  261. purge \1#{d:__j}, __j
  262. endr
  263. purge __i, __idx
  264. __slist_length \1, __n - 1
  265. purge __n
  266. ENDM
  267.  
  268. ; slist_find INDEX, LIST, item
  269. ; Find the first index of an item in a string list.
  270. ;
  271. ; \1: symbol - The index to define
  272. ; \2: symbol - The string list to use
  273. ; \3: string - The item to search for
  274. ;
  275. ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
  276. ; slist_find N, MONS, "Charmander"
  277. ; assert N == 2
  278. ; slist_find N, MONS, "Mew"
  279. ; assert N == 0
  280. ;
  281. MACRO slist_find
  282. assert def({slist_length_prefix}\2{slist_length_suffix}), \
  283. "slist_find: string list \2 is not defined"
  284. def \1 = 0
  285. for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
  286. if !strcmp(\3, "{\2#{d:__i}}")
  287. def \1 = __i
  288. break
  289. endc
  290. endr
  291. purge __i
  292. ENDM
  293.  
  294. ; slist_rfind INDEX, LIST, item
  295. ; Find the last index of an item in a string list.
  296. ;
  297. ; \1: symbol - The index to define
  298. ; \2: symbol - The string list to use
  299. ; \3: string - The item to search for
  300. ;
  301. ; slist MONS, "Pidgey", "Spearow", "Pidgey"
  302. ; slist_rfind N, MONS, "Pidgey"
  303. ; assert N == 3
  304. ;
  305. MACRO slist_rfind
  306. assert def({slist_length_prefix}\2{slist_length_suffix}), \
  307. "slist_rfind: string list \2 is not defined"
  308. def \1 = 0
  309. for __i, {slist_length_prefix}\2{slist_length_suffix}, 0, -1
  310. if !strcmp(\3, "{\2#{d:__i}}")
  311. def \1 = __i
  312. break
  313. endc
  314. endr
  315. purge __i
  316. ENDM
  317.  
  318. ; slist_count NUM, LIST, item
  319. ; Count the occurrences of an item in a string list.
  320. ;
  321. ; \1: symbol - The quantity to define
  322. ; \2: symbol - The string list to use
  323. ; \3: string - The item to count
  324. ;
  325. ; slist MONS, "Magikarp", "Magikarp", "Gyarados", "Magikarp"
  326. ; slist_count N, MONS, "Magikarp"
  327. ; assert N == 3
  328. ;
  329. MACRO slist_count
  330. assert def({slist_length_prefix}\2{slist_length_suffix}), \
  331. "slist_count: string list \2 is not defined"
  332. def \1 = 0
  333. for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
  334. if !strcmp(\3, "{\2#{d:__i}}")
  335. def \1 = \1 + 1
  336. endc
  337. endr
  338. purge __i
  339. ENDM
  340.  
  341. ; slist_replace LIST, old, new
  342. ; Replace each occurrence of an item in a string list.
  343. ;
  344. ; \1: symbol - The string list to update
  345. ; \2: string - The old string to be replaced
  346. ; \3: string - The new string to replace with
  347. ;
  348. ; slist MONS, "Ditto", "Mewtwo", "Ditto"
  349. ; slist_replace MONS, "Ditto", "Mew"
  350. ; ; Prints ["Mew", "Mewtwo", "Mew"]
  351. ; slist_println MONS
  352. ;
  353. MACRO slist_replace
  354. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  355. "slist_replace: string list \1 is not defined"
  356. for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
  357. if !strcmp(\2, "{\1#{d:__i}}")
  358. redef \1#{d:__i} equs \3
  359. endc
  360. endr
  361. purge __i
  362. ENDM
  363.  
  364. ; slist_remove LIST, item
  365. ; Remove the first occurrence of an item in a string list.
  366. ;
  367. ; \1: symbol - The string list to update
  368. ; \2: string - The string to be removed
  369. ;
  370. ; slist MONS, "Pidgey", "Rattata", "Pikachu", "Rattata"
  371. ; slist_remove MONS, "Rattata"
  372. ; ; Prints ["Pidgey", "Pikachu", "Rattata"]
  373. ; slist_println MONS
  374. ;
  375. MACRO slist_remove
  376. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  377. "slist_remove: string list \1 is not defined"
  378. slist_find ___idx\@, \1, \2
  379. if ___idx\@ > 0
  380. slist_delete \1, ___idx\@
  381. endc
  382. purge ___idx\@
  383. ENDM
  384.  
  385. ; slist_remove_all LIST, item
  386. ; Remove each occurrence of an item in a string list.
  387. ;
  388. ; \1: symbol - The string list to update
  389. ; \2: string - The string to be removed
  390. ;
  391. ; slist MONS, "Pidgey", "Rattata", "Pikachu", "Rattata"
  392. ; slist_remove_all MONS, "Rattata"
  393. ; ; Prints ["Pidgey", "Pikachu"]
  394. ; slist_println MONS
  395. ;
  396. MACRO slist_remove_all
  397. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  398. "slist_remove_all: string list \1 is not defined"
  399. slist_count ___num\@, \1, \2
  400. rept ___num\@
  401. slist_remove \1, \2
  402. endr
  403. purge ___num\@
  404. ENDM
  405.  
  406. ; slist_sort LIST
  407. ; Sort a string list.
  408. ;
  409. ; \1: symbol - The string list to sort
  410. ;
  411. ; slist MONS, "Squirtle", "Bulbasaur", "Charmander"
  412. ; slist_sort MONS
  413. ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
  414. ; slist_println MONS
  415. ;
  416. MACRO slist_sort
  417. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  418. "slist_sort: string list \1 is not defined"
  419. def __n = {slist_length_prefix}\1{slist_length_suffix}
  420. for __p, ceil(log(__n << 16, 2.0)) >> 16
  421. def __w = 2 ** __p
  422. for __i, 1, __n + 1, 2 * __w
  423. if __i + __w > __n
  424. break
  425. endc
  426. def __l1 = __i
  427. def __r1 = __l1 + __w - 1
  428. def __l2 = __r1 + 1
  429. def __r2 = __l2 + __w - 1
  430. if __r2 > __n
  431. def __r2 = __n
  432. endc
  433. for __t, __n
  434. if __l1 > __r1 || __l2 > __r2
  435. break
  436. endc
  437. if strcmp("{\1#{d:__l1}}", "{\1#{d:__l2}}") <= 0
  438. def __tmp{d:__t} equs "{\1#{d:__l1}}"
  439. def __l1 = __l1 + 1
  440. else
  441. def __tmp{d:__t} equs "{\1#{d:__l2}}"
  442. def __l2 = __l2 + 1
  443. endc
  444. endr
  445. for __j, __l1, __r1 + 1
  446. def __tmp{d:__t} equs "{\1#{d:__j}}"
  447. def __t = __t + 1
  448. endr
  449. for __j, __l2, __r2 + 1
  450. def __tmp{d:__t} equs "{\1#{d:__j}}"
  451. def __t = __t + 1
  452. endr
  453. purge __l1, __r1, __l2, __r2
  454. for __j, __t
  455. def __k = __i + __j
  456. redef \1#{d:__k} equs "{__tmp{d:__j}}"
  457. purge __k, __tmp{d:__j}
  458. endr
  459. purge __j, __t
  460. endr
  461. purge __i, __w
  462. endr
  463. purge __p, __n
  464. ENDM
  465.  
  466. ; slist_reverse LIST
  467. ; Reverse a string list.
  468. ;
  469. ; \1: symbol - The string list to reverse
  470. ;
  471. ; slist MONS, "Squirtle", "Charmander", "Bulbasaur"
  472. ; slist_reverse MONS
  473. ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
  474. ; slist_println MONS
  475. ;
  476. MACRO slist_reverse
  477. assert def({slist_length_prefix}\1{slist_length_suffix}), \
  478. "slist_reverse: string list \1 is not defined"
  479. def __n = {slist_length_prefix}\1{slist_length_suffix}
  480. for __i, 1, __n / 2 + 1
  481. def __j = __n - __i + 1
  482. def __tmp equs "{\1#{d:__i}}"
  483. redef \1#{d:__i} equs "{\1#{d:__j}}"
  484. redef \1#{d:__j} equs "{__tmp}"
  485. purge __tmp, __j
  486. endr
  487. purge __i, __n
  488. ENDM
  489.  
  490. slist MONS
  491. slist_item "Squirtle"
  492. slist_item "Bulbasaur"
  493. slist_item "Charmander"
  494. slist_sort MONS
  495.  
  496. slist_println MONS ; ["Bulbasaur", "Charmander", "Squirtle"]
  497. println LENGTH_MONS ; $3
  498.  
  499. slist_copy STARTERS, MONS
  500. slist_append STARTERS, "Pikachu", "Eevee"
  501.  
  502. slist_println STARTERS ; ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Eevee"]
  503. slist_replace STARTERS, "Pikachu", "Raichu"
  504. println "{STARTERS#4}" ; Raichu
  505.  
  506. slist GEN2MONS, "Chikorita", "Cyndaquil", "Totodile"
  507. slist_delete STARTERS, 4
  508. slist_remove STARTERS, "Eevee"
  509. slist_extend STARTERS, GEN2MONS
  510. slist_purge GEN2MONS
  511. slist_println STARTERS ; ["Bulbasaur", "Charmander", "Squirtle", "Chikorita", "Cyndaquil", "Totodile"]
  512.  
  513. slist_purge MONS
  514. assert !DEF(LENGTH_MONS)
  515. slist MONS, "Rattata", "Pidgey", "Pidgey", "Spearow", "Pidgey", "Spearow"
  516. slist_find N, MONS, "Pidgey"
  517. println N ; $2
  518. slist_rfind N, MONS, "Pidgey"
  519. println N ; $5
  520. slist_count N, MONS, "Pidgey"
  521. println N ; $3
  522. slist_remove_all MONS, "Pidgey"
  523. slist_set MONS, 2, "Fearow"
  524. slist_insert MONS, 3, "Raticate"
  525. slist_reverse MONS
  526. slist_println MONS ; ["Spearow", "Raticate", "Fearow", "Rattata"]
  527.  
Advertisement
Add Comment
Please, Sign In to add comment