Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; These affect the name of string lists' length constants
- def slist_length_prefix equs "LENGTH_"
- def slist_length_suffix equs ""
- ; __slist_length LIST, length
- ; For internal use only!
- ;
- MACRO __slist_length
- if def({slist_length_prefix}\1{slist_length_suffix})
- purge {slist_length_prefix}\1{slist_length_suffix}
- endc
- def {slist_length_prefix}\1{slist_length_suffix} equ \2
- ENDM
- ; slist LIST, items...
- ; Create a new string list.
- ;
- ; \1: symbol - The string list to create
- ; \2+: string... - The initial items, if any
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; assert LENGTH_MONS == 3
- ; assert !STRCMP("{MONS#1}", "Bulbasaur")
- ;
- ; slist_item items...
- ; Append items to the previously created string list.
- ;
- ; \1+: string... - The items to append, if any
- ;
- ; slist MONS
- ; slist_item "Bulbasaur"
- ; slist_item "Charmander", "Squirtle"
- ; assert LENGTH_MONS == 3
- ; assert !STRCMP("{MONS#2}", "Charmander")
- ;
- MACRO slist
- assert !def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist: string list \1 is already defined"
- __slist_length \1, 0
- slist_append \#
- redef slist_item equs "slist_append \1,"
- ENDM
- ; slist_print LIST
- ; Print a string list.
- ;
- ; \1: symbol - The string list to print
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
- ; slist_print MONS
- ;
- MACRO slist_print
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_print: string list \1 is not defined"
- print "["
- for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
- if __i > 1
- print ", "
- endc
- print "\"{\1#{d:__i}}\""
- endr
- purge __i
- print "]"
- ENDM
- ; slist_println LIST
- ; Print a string list followed by a newline.
- ;
- ; \1: symbol - The string list to print
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
- ; slist_println MONS
- ;
- MACRO slist_println
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_println: string list \1 is not defined"
- slist_print \1
- println
- ENDM
- ; slist_append LIST, items...
- ; Append items to a string list.
- ;
- ; \1: symbol - The string list to update
- ; \2+: string... - The items to append, if any
- ;
- ; slist MONS
- ; slist_append MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; assert LENGTH_MONS == 3
- ; assert !STRCMP("{MONS#3}", "Squirtle")
- ;
- MACRO slist_append
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_append: string list \1 is not defined"
- def __n = {slist_length_prefix}\1{slist_length_suffix}
- for __i, 2, _NARG + 1
- def __j = __n + __i - 1
- def \1#{d:__j} equs \<__i>
- purge __j
- endr
- purge __i
- __slist_length \1, __n + {_NARG} - 1
- purge __n
- ENDM
- ; slist_extend LIST1, LIST2
- ; Append items to a string list from another.
- ;
- ; \1: symbol - The string list to update
- ; \2: symbol - The string list to append from
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist MOREMONS, "Pikachu", "Eevee"
- ; slist_extend MONS, MOREMONS
- ; assert LENGTH_MONS == 5
- ;
- MACRO slist_extend
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_extend: string list \1 is not defined"
- assert def({slist_length_prefix}\2{slist_length_suffix}), \
- "slist_extend: string list \2 is not defined"
- for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
- slist_append \1, "{\2#{d:__i}}"
- endr
- purge __i
- ENDM
- ; slist_copy LIST1, LIST2
- ; Create a new string list as a copy of another string list.
- ;
- ; \1: symbol - The string list to create
- ; \2: symbol - The string list to copy
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_copy STARTERS, MONS
- ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
- ; slist_println STARTERS
- ;
- MACRO slist_copy
- assert !def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_copy: string list \1 is already defined"
- assert def({slist_length_prefix}\2{slist_length_suffix}), \
- "slist_copy: string list \2 is not defined"
- __slist_length \1, {slist_length_prefix}\2{slist_length_suffix}
- for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
- def \1#{d:__i} equs "{\2#{d:__i}}"
- endr
- purge __i
- ENDM
- ; slist_clear LIST
- ; Clear a string list.
- ;
- ; \1: symbol - The string list to clear
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_clear MONS
- ; assert LENGTH_MONS == 0
- ;
- MACRO slist_clear
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_clear: string list \1 is not defined"
- for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
- purge \1#{d:__i}
- endr
- purge __i
- __slist_length \1, 0
- ENDM
- ; slist_purge LIST
- ; Purge a string list.
- ;
- ; \1: symbol - The string list to purge
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_purge MONS
- ; assert !DEF(LENGTH_MONS)
- ;
- MACRO slist_purge
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_purge: string list \1 is not defined"
- slist_clear \1
- purge {slist_length_prefix}\1{slist_length_suffix}
- ENDM
- ; slist_set LIST, index, item
- ; Set an index of a string list to an item.
- ;
- ; \1: symbol - The string list to update
- ; \2: number - The index to be set
- ; \3: string - The item to set
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_set MONS, 2, "Cyndaquil"
- ; assert !STRCMP("{MONS#2}", "Cyndaquil")
- ;
- MACRO slist_set
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_set: string list \1 is not defined"
- def __idx = \2
- assert 1 <= __idx && __idx <= {slist_length_prefix}\1{slist_length_suffix}, \
- "slist_set: invalid index for string list \1: {d:__idx}"
- redef \1#{d:__idx} equs \3
- purge __idx
- ENDM
- ; slist_insert LIST, index, item
- ; Insert an item into a string list before an index.
- ;
- ; \1: symbol - The string list to update
- ; \2: number - The index to insert before
- ; \3: string - The item to insert
- ;
- ; slist MONS, "Bulbasaur", "Squirtle"
- ; slist_insert MONS, 2, "Charmander"
- ; assert !STRCMP("{MONS#2}", "Charmander")
- ;
- MACRO slist_insert
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_insert: string list \1 is not defined"
- def __n = {slist_length_prefix}\1{slist_length_suffix}
- def __idx = \2
- assert 1 <= __idx && __idx <= __n, \
- "slist_insert: invalid index for string list \1: {d:__idx}"
- for __i, __n + 1, __idx, -1
- def __j = __i - 1
- def \1#{d:__i} equs "{\1#{d:__j}}"
- purge \1#{d:__j}, __j
- endr
- purge __i
- def \1#{d:__idx} equs \3
- purge __idx
- __slist_length \1, __n + 1
- purge __n
- ENDM
- ; slist_delete LIST, index
- ; Delete an index from a string list.
- ;
- ; \1: symbol - The string list to update
- ; \2: number - The index to delete
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_delete MONS, 1
- ; assert LENGTH_MONS == 2
- ; assert !STRCMP("{MONS#1}", "Charmander")
- ;
- MACRO slist_delete
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_delete: string list \1 is not defined"
- def __n = {slist_length_prefix}\1{slist_length_suffix}
- def __idx = \2
- assert 1 <= __idx && __idx <= __n, \
- "slist_delete: invalid index for string list \1: {d:__idx}"
- purge \1#{d:__idx}
- for __i, __idx, __n
- def __j = __i + 1
- def \1#{d:__i} equs "{\1#{d:__j}}"
- purge \1#{d:__j}, __j
- endr
- purge __i, __idx
- __slist_length \1, __n - 1
- purge __n
- ENDM
- ; slist_find INDEX, LIST, item
- ; Find the first index of an item in a string list.
- ;
- ; \1: symbol - The index to define
- ; \2: symbol - The string list to use
- ; \3: string - The item to search for
- ;
- ; slist MONS, "Bulbasaur", "Charmander", "Squirtle"
- ; slist_find N, MONS, "Charmander"
- ; assert N == 2
- ; slist_find N, MONS, "Mew"
- ; assert N == 0
- ;
- MACRO slist_find
- assert def({slist_length_prefix}\2{slist_length_suffix}), \
- "slist_find: string list \2 is not defined"
- def \1 = 0
- for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
- if !strcmp(\3, "{\2#{d:__i}}")
- def \1 = __i
- break
- endc
- endr
- purge __i
- ENDM
- ; slist_rfind INDEX, LIST, item
- ; Find the last index of an item in a string list.
- ;
- ; \1: symbol - The index to define
- ; \2: symbol - The string list to use
- ; \3: string - The item to search for
- ;
- ; slist MONS, "Pidgey", "Spearow", "Pidgey"
- ; slist_rfind N, MONS, "Pidgey"
- ; assert N == 3
- ;
- MACRO slist_rfind
- assert def({slist_length_prefix}\2{slist_length_suffix}), \
- "slist_rfind: string list \2 is not defined"
- def \1 = 0
- for __i, {slist_length_prefix}\2{slist_length_suffix}, 0, -1
- if !strcmp(\3, "{\2#{d:__i}}")
- def \1 = __i
- break
- endc
- endr
- purge __i
- ENDM
- ; slist_count NUM, LIST, item
- ; Count the occurrences of an item in a string list.
- ;
- ; \1: symbol - The quantity to define
- ; \2: symbol - The string list to use
- ; \3: string - The item to count
- ;
- ; slist MONS, "Magikarp", "Magikarp", "Gyarados", "Magikarp"
- ; slist_count N, MONS, "Magikarp"
- ; assert N == 3
- ;
- MACRO slist_count
- assert def({slist_length_prefix}\2{slist_length_suffix}), \
- "slist_count: string list \2 is not defined"
- def \1 = 0
- for __i, 1, {slist_length_prefix}\2{slist_length_suffix} + 1
- if !strcmp(\3, "{\2#{d:__i}}")
- def \1 = \1 + 1
- endc
- endr
- purge __i
- ENDM
- ; slist_replace LIST, old, new
- ; Replace each occurrence of an item in a string list.
- ;
- ; \1: symbol - The string list to update
- ; \2: string - The old string to be replaced
- ; \3: string - The new string to replace with
- ;
- ; slist MONS, "Ditto", "Mewtwo", "Ditto"
- ; slist_replace MONS, "Ditto", "Mew"
- ; ; Prints ["Mew", "Mewtwo", "Mew"]
- ; slist_println MONS
- ;
- MACRO slist_replace
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_replace: string list \1 is not defined"
- for __i, 1, {slist_length_prefix}\1{slist_length_suffix} + 1
- if !strcmp(\2, "{\1#{d:__i}}")
- redef \1#{d:__i} equs \3
- endc
- endr
- purge __i
- ENDM
- ; slist_remove LIST, item
- ; Remove the first occurrence of an item in a string list.
- ;
- ; \1: symbol - The string list to update
- ; \2: string - The string to be removed
- ;
- ; slist MONS, "Pidgey", "Rattata", "Pikachu", "Rattata"
- ; slist_remove MONS, "Rattata"
- ; ; Prints ["Pidgey", "Pikachu", "Rattata"]
- ; slist_println MONS
- ;
- MACRO slist_remove
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_remove: string list \1 is not defined"
- slist_find ___idx\@, \1, \2
- if ___idx\@ > 0
- slist_delete \1, ___idx\@
- endc
- purge ___idx\@
- ENDM
- ; slist_remove_all LIST, item
- ; Remove each occurrence of an item in a string list.
- ;
- ; \1: symbol - The string list to update
- ; \2: string - The string to be removed
- ;
- ; slist MONS, "Pidgey", "Rattata", "Pikachu", "Rattata"
- ; slist_remove_all MONS, "Rattata"
- ; ; Prints ["Pidgey", "Pikachu"]
- ; slist_println MONS
- ;
- MACRO slist_remove_all
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_remove_all: string list \1 is not defined"
- slist_count ___num\@, \1, \2
- rept ___num\@
- slist_remove \1, \2
- endr
- purge ___num\@
- ENDM
- ; slist_sort LIST
- ; Sort a string list.
- ;
- ; \1: symbol - The string list to sort
- ;
- ; slist MONS, "Squirtle", "Bulbasaur", "Charmander"
- ; slist_sort MONS
- ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
- ; slist_println MONS
- ;
- MACRO slist_sort
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_sort: string list \1 is not defined"
- def __n = {slist_length_prefix}\1{slist_length_suffix}
- for __p, ceil(log(__n << 16, 2.0)) >> 16
- def __w = 2 ** __p
- for __i, 1, __n + 1, 2 * __w
- if __i + __w > __n
- break
- endc
- def __l1 = __i
- def __r1 = __l1 + __w - 1
- def __l2 = __r1 + 1
- def __r2 = __l2 + __w - 1
- if __r2 > __n
- def __r2 = __n
- endc
- for __t, __n
- if __l1 > __r1 || __l2 > __r2
- break
- endc
- if strcmp("{\1#{d:__l1}}", "{\1#{d:__l2}}") <= 0
- def __tmp{d:__t} equs "{\1#{d:__l1}}"
- def __l1 = __l1 + 1
- else
- def __tmp{d:__t} equs "{\1#{d:__l2}}"
- def __l2 = __l2 + 1
- endc
- endr
- for __j, __l1, __r1 + 1
- def __tmp{d:__t} equs "{\1#{d:__j}}"
- def __t = __t + 1
- endr
- for __j, __l2, __r2 + 1
- def __tmp{d:__t} equs "{\1#{d:__j}}"
- def __t = __t + 1
- endr
- purge __l1, __r1, __l2, __r2
- for __j, __t
- def __k = __i + __j
- redef \1#{d:__k} equs "{__tmp{d:__j}}"
- purge __k, __tmp{d:__j}
- endr
- purge __j, __t
- endr
- purge __i, __w
- endr
- purge __p, __n
- ENDM
- ; slist_reverse LIST
- ; Reverse a string list.
- ;
- ; \1: symbol - The string list to reverse
- ;
- ; slist MONS, "Squirtle", "Charmander", "Bulbasaur"
- ; slist_reverse MONS
- ; ; Prints ["Bulbasaur", "Charmander", "Squirtle"]
- ; slist_println MONS
- ;
- MACRO slist_reverse
- assert def({slist_length_prefix}\1{slist_length_suffix}), \
- "slist_reverse: string list \1 is not defined"
- def __n = {slist_length_prefix}\1{slist_length_suffix}
- for __i, 1, __n / 2 + 1
- def __j = __n - __i + 1
- def __tmp equs "{\1#{d:__i}}"
- redef \1#{d:__i} equs "{\1#{d:__j}}"
- redef \1#{d:__j} equs "{__tmp}"
- purge __tmp, __j
- endr
- purge __i, __n
- ENDM
- slist MONS
- slist_item "Squirtle"
- slist_item "Bulbasaur"
- slist_item "Charmander"
- slist_sort MONS
- slist_println MONS ; ["Bulbasaur", "Charmander", "Squirtle"]
- println LENGTH_MONS ; $3
- slist_copy STARTERS, MONS
- slist_append STARTERS, "Pikachu", "Eevee"
- slist_println STARTERS ; ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Eevee"]
- slist_replace STARTERS, "Pikachu", "Raichu"
- println "{STARTERS#4}" ; Raichu
- slist GEN2MONS, "Chikorita", "Cyndaquil", "Totodile"
- slist_delete STARTERS, 4
- slist_remove STARTERS, "Eevee"
- slist_extend STARTERS, GEN2MONS
- slist_purge GEN2MONS
- slist_println STARTERS ; ["Bulbasaur", "Charmander", "Squirtle", "Chikorita", "Cyndaquil", "Totodile"]
- slist_purge MONS
- assert !DEF(LENGTH_MONS)
- slist MONS, "Rattata", "Pidgey", "Pidgey", "Spearow", "Pidgey", "Spearow"
- slist_find N, MONS, "Pidgey"
- println N ; $2
- slist_rfind N, MONS, "Pidgey"
- println N ; $5
- slist_count N, MONS, "Pidgey"
- println N ; $3
- slist_remove_all MONS, "Pidgey"
- slist_set MONS, 2, "Fearow"
- slist_insert MONS, 3, "Raticate"
- slist_reverse MONS
- slist_println MONS ; ["Spearow", "Raticate", "Fearow", "Rattata"]
Advertisement
Add Comment
Please, Sign In to add comment