Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.3KB

  1. ; run RLA the number of times specified in B
  2. rlaX:
  3. ; first, see if B == 0 to see if we need to bail out
  4. inc b
  5. dec b
  6. ret z ; Z flag means we had B = 0
  7. .loop: rla
  8. djnz .loop
  9. ret
  10. callHL:
  11. jp (hl)
  12. ret
  13. ; HL - DE -> HL
  14. subDEFromHL:
  15. push af
  16. ld a, l
  17. sub e
  18. ld l, a
  19. ld a, h
  20. sbc a, d
  21. ld h, a
  22. pop af
  23. ret
  24. ; make Z the opposite of what it is now
  25. toggleZ:
  26. jp z, unsetZ
  27. cp a
  28. ret
  29. ; Sets Z if string at (HL) is one character long
  30. strIs1L:
  31. xor a
  32. cp (hl)
  33. jp z, unsetZ ; empty string
  34. inc hl
  35. cp (hl) ; Z has proper value
  36. dec hl ; doesn't touch Z
  37. ret
  38. ; Compares strings pointed to by HL and DE up to A count of characters in a
  39. ; case-insensitive manner.
  40. ; If equal, Z is set. If not equal, Z is reset.
  41. strncmpI:
  42. push bc
  43. push hl
  44. push de
  45. ld b, a
  46. .loop:
  47. ld a, (de)
  48. call upcase
  49. ld c, a
  50. ld a, (hl)
  51. call upcase
  52. cp c
  53. jr nz, .end ; not equal? break early. NZ is carried out
  54. ; to the called
  55. or a ; cp 0. If our chars are null, stop the cmp
  56. jr z, .end ; The positive result will be carried to the
  57. ; caller
  58. inc hl
  59. inc de
  60. djnz .loop
  61. ; Success
  62. ; We went through all chars with success. Ensure Z
  63. cp a
  64. .end:
  65. pop de
  66. pop hl
  67. pop bc
  68. ; Because we don't call anything else than CP that modify the Z flag,
  69. ; our Z value will be that of the last cp (reset if we broke the loop
  70. ; early, set otherwise)
  71. ret
  72. ; If string at (HL) starts with ( and ends with ), "enter" into the parens
  73. ; (advance HL and put a null char at the end of the string) and set Z.
  74. ; Otherwise, do nothing and reset Z.
  75. enterParens:
  76. ld a, (hl)
  77. cp '('
  78. ret nz ; nothing to do
  79. push hl
  80. ld a, 0 ; look for null char
  81. ; advance until we get null
  82. .loop:
  83. cpi
  84. jp z, .found
  85. jr .loop
  86. .found:
  87. dec hl ; cpi over-advances. go back to null-char
  88. dec hl ; looking at the last char before null
  89. ld a, (hl)
  90. cp ')'
  91. jr nz, .doNotEnter
  92. ; We have parens. While we're here, let's put a null
  93. xor a
  94. ld (hl), a
  95. pop hl ; back at the beginning. Let's advance.
  96. inc hl
  97. cp a ; ensure Z
  98. ret ; we're good!
  99. .doNotEnter:
  100. pop hl
  101. call unsetZ
  102. ret
  103. ; Scans (HL) and sets Z according to whether the string is double quoted, that
  104. ; is, starts with a " and ends with a ". If it is double quoted, "enter" them,
  105. ; that is, advance HL by one and transform the ending quote into a null char.
  106. ; If the string isn't double-enquoted, HL isn't changed.
  107. enterDoubleQuotes:
  108. ld a, (hl)
  109. cp '"'
  110. ret nz
  111. push hl
  112. inc hl
  113. ld a, (hl)
  114. or a ; already end of string?
  115. jr z, .nomatch
  116. xor a
  117. call findchar ; go to end of string
  118. dec hl
  119. ld a, (hl)
  120. cp '"'
  121. jr nz, .nomatch
  122. ; We have a match, replace ending quote with null char
  123. xor a
  124. ld (hl), a
  125. ; Good, let's go back
  126. pop hl
  127. ; ... but one char further
  128. inc hl
  129. cp a ; ensure Z
  130. ret
  131. .nomatch:
  132. call unsetZ
  133. pop hl
  134. ret
  135. ; Find string (HL) in string list (DE) of size B, in a case-insensitive manner.
  136. ; Each string is C bytes wide.
  137. ; Returns the index of the found string. Sets Z if found, unsets Z if not found.
  138. findStringInList:
  139. push de
  140. push bc
  141. .loop:
  142. ld a, c
  143. call strncmpI
  144. ld a, c
  145. call addDE
  146. jr z, .match
  147. djnz .loop
  148. ; no match, Z is unset
  149. pop bc
  150. pop de
  151. ret
  152. .match:
  153. ; Now, we want the index of our string, which is equal to our initial B
  154. ; minus our current B. To get this, we have to play with our registers
  155. ; and stack a bit.
  156. ld d, b
  157. pop bc
  158. ld a, b
  159. sub d
  160. pop de
  161. cp a ; ensure Z
  162. ret