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.

111 lines
2.1KB

  1. ; *** Variables ***
  2. ; A list of words for each member of the A-Z range.
  3. .equ VAR_TBL VAR_RAMSTART
  4. .equ VAR_RAMEND @+52
  5. ; *** Code ***
  6. varInit:
  7. ld b, VAR_RAMEND-VAR_RAMSTART
  8. ld hl, VAR_RAMSTART
  9. xor a
  10. .loop:
  11. ld (hl), a
  12. inc hl
  13. djnz .loop
  14. ret
  15. ; Check if A is a valid variable letter (a-z or A-Z). If it is, set A to a
  16. ; valid VAR_TBL index and set Z. Otherwise, unset Z (and A is destroyed)
  17. varChk:
  18. call upcase
  19. sub 'A'
  20. ret c ; Z unset
  21. cp 27 ; 'Z' + 1
  22. jr c, .isVar
  23. ; A > 'Z'
  24. dec a ; unset Z
  25. ret
  26. .isVar:
  27. cp a ; set Z
  28. ret
  29. ; Try to interpret line at (HL) and see if it's a variable assignment. If it
  30. ; is, proceed with the assignment and set Z. Otherwise, NZ.
  31. varTryAssign:
  32. inc hl
  33. ld a, (hl)
  34. dec hl
  35. cp '='
  36. ret nz
  37. ld a, (hl)
  38. call varChk
  39. ret nz
  40. ; We have a variable! Its table index is currently in A.
  41. push ix ; --> lvl 1
  42. push hl ; --> lvl 2
  43. push de ; --> lvl 3
  44. push af ; --> lvl 4. save for later
  45. ; Let's put that expression to read in scratchpad
  46. inc hl \ inc hl
  47. ld de, SCRATCHPAD
  48. call rdWord
  49. ex de, hl
  50. ; Now, evaluate that expression now in (HL)
  51. call parseExpr ; --> number in IX
  52. jr nz, .exprErr
  53. pop af ; <-- lvl 4
  54. push ix \ pop de ; send number to DE
  55. call varAssign
  56. xor a ; ensure Z
  57. .end:
  58. pop de ; <-- lvl 3
  59. pop hl ; <-- lvl 2
  60. pop ix ; <-- lvl 1
  61. ret
  62. .exprErr:
  63. pop af ; <-- lvl 4
  64. jr .end
  65. ; Given a variable **index** in A (call varChk to transform) and a value in
  66. ; DE, assign that value in the proper cell in VAR_TBL.
  67. ; No checks are made.
  68. varAssign:
  69. push hl
  70. add a, a ; * 2 because each element is a word
  71. ld hl, VAR_TBL
  72. call addHL
  73. ; HL placed, write number
  74. ld (hl), e
  75. inc hl
  76. ld (hl), d
  77. pop hl
  78. ret
  79. ; Check if value at (HL) is a variable. If yes, returns its associated value.
  80. ; Otherwise, jump to parseLiteral.
  81. parseLiteralOrVar:
  82. inc hl
  83. ld a, (hl)
  84. dec hl
  85. or a
  86. ; if more than one in length, it can't be a variable
  87. jp nz, parseLiteral
  88. ld a, (hl)
  89. call varChk
  90. jp nz, parseLiteral
  91. ; It's a variable, resolve!
  92. add a, a ; * 2 because each element is a word
  93. push hl ; --> lvl 1
  94. ld hl, VAR_TBL
  95. call addHL
  96. push de ; --> lvl 2
  97. ld e, (hl)
  98. inc hl
  99. ld d, (hl)
  100. push de \ pop ix
  101. pop de ; <-- lvl 2
  102. pop hl ; <-- lvl 1
  103. cp a ; ensure Z
  104. ret