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.

105 lines
2.0KB

  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 DE
  52. jr nz, .exprErr
  53. pop af ; <-- lvl 4
  54. call varAssign
  55. xor a ; ensure Z
  56. .end:
  57. pop de ; <-- lvl 3
  58. pop hl ; <-- lvl 2
  59. pop ix ; <-- lvl 1
  60. ret
  61. .exprErr:
  62. pop af ; <-- lvl 4
  63. jr .end
  64. ; Given a variable **index** in A (call varChk to transform) and a value in
  65. ; DE, assign that value in the proper cell in VAR_TBL.
  66. ; No checks are made.
  67. varAssign:
  68. push hl
  69. add a, a ; * 2 because each element is a word
  70. ld hl, VAR_TBL
  71. call addHL
  72. ; HL placed, write number
  73. ld (hl), e
  74. inc hl
  75. ld (hl), d
  76. pop hl
  77. ret
  78. ; Check if value at (HL) is a variable. If yes, returns its associated value.
  79. ; Otherwise, jump to parseLiteral.
  80. parseLiteralOrVar:
  81. call isLiteralPrefix
  82. jp z, parseLiteral
  83. ; not a literal, try var
  84. ld a, (hl)
  85. call varChk
  86. ret nz
  87. ; It's a variable, resolve!
  88. add a, a ; * 2 because each element is a word
  89. push hl ; --> lvl 1
  90. ld hl, VAR_TBL
  91. call addHL
  92. ld e, (hl)
  93. inc hl
  94. ld d, (hl)
  95. pop hl ; <-- lvl 1
  96. inc hl ; point to char after variable
  97. cp a ; ensure Z
  98. ret