Jan-Langevad

ESP32Forth-Local-variables.TXT

Feb 15th, 2026
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | Software | 0 0
  1. \ ESP32Forth-Local-variables
  2.  
  3. \ ESP32Forth supports local variables.
  4. \ They are declared between { and } right after the colon-definition, e.g.:
  5.  
  6. : 3x+1 { x -- sum } x 3 * 1 + ;
  7.  
  8. \ Here x is taken from the stack at the call and can be
  9. \ used as a local variable in the definition.
  10.  
  11. : 2OVER { a b c d -- a b } a b c d a b ;
  12.  
  13. \ Assignment/modification of local variables is done with to and +to, e.g.:
  14.  
  15. : a+bEXP2 { a b -- (a+b)^2 }
  16. 0 { result }
  17. a a * to result
  18. b b * +to result
  19. a b * 2 * +to result
  20. result
  21. ;
  22.  
  23. \ EOF
  24.  
  25. Important: If you use local variables in a definition,
  26. you should not use >r and r> in the same word,
  27. as it can interfere with the internal handling of the local variables.
  28.  
  29. There must be two hyphens (--) in the stack effect for local variables
  30. in ESP32Forth (and most Forth systems with locals).
  31.  
  32. Stack-effect rules
  33. 1. Input variables (before --)
  34. text
  35. : 3x+1 { x -- sum } \ x comes from the data stack
  36. x 3 * 1 + ; \ returns sum to the data stack
  37. Stack before call: ( x -- sum )
  38.  
  39. 2. Output variables (after --)
  40. text
  41. : a+bEXP2 { a b -- (a+b)^2 }
  42. 0 { result }
  43. ...
  44. result ; \ result goes out to the data stack
  45. Stack before call: ( a b -- (a+b)^2 )
  46.  
  47. 3. -- always separates input from output
  48.  
  49. : 2OVER { a b c d -- a b }
  50. a b c d a b ; \ takes 4, returns 2
  51. Stack before call: ( a b c d -- a b )
  52.  
  53. Why two hyphens?
  54. First -: Ends the input list (what's taken from the stack)
  55.  
  56. Second -: Starts the output list (what's placed on the stack)
  57.  
  58. --- would be incorrect syntax – Forth expects exactly --.
  59.  
  60. Typical examples
  61.  
  62. { x -- n } \ input: x, output: n
  63. { -- n } \ no input, output: n
  64. { x y -- z } \ input: x y, output: z
  65. { x y z -- } \ input: x y z, no output
  66. { x -- x } \ input: x, output: same x
  67.  
  68. Your code is correct: { a b -- (a+b)^2 } means "takes a b
  69. from the stack, returns (a+b)^2 to the stack".
  70.  
  71. Det der står efter -- er fri dokumentation/kommentar – ikke en del af Forth‑syntaksen.
  72.  
  73. Stack‑effekt syntaks i Forth
  74. text
  75. : navn { input1 input2 -- output1 output2 } \ <-- Kommentar her
  76. Før --: Lokale variabler (bindes til stack‑elementer)
  77. Efter --: Fri tekst som beskriver, hvad ordet returnerer.
  78.  
  79. Eksempler på "fri tekst" efter --
  80. text
  81. : 3x+1 { x -- 3*x+1 } \ Beskriver hvad der returneres
  82. : MAX { a b -- larger } \ Navngiver resultatet
  83. : AREA { l w -- area } \ Kort beskrivelse
  84. : SWAP { a b -- b a } \ Præcis stack‑effekt
  85. : 2OVER { a b c d -- a b } \ Klassisk Forth stack notation
  86. Hvad parseren ignorerer
  87. Forth‑kompileren læser kun:
  88.  
  89. text
  90. { x y -- } \ x og y er lokale variabler
  91. Alt efter -- (inkl. parenteser (a+b)^2) er kommentar der:
  92.  
  93. Ikke påvirker kompilation
  94.  
  95. Kun er til læsbarhed
  96.  
  97. Kan udelades uden fejl
  98.  
  99. text
  100. : a+bEXP2 { a b -- } \ <-- Fungerer også!
  101. ... result ;
  102. (a+b)^2 er altså din kommentar til at forklare logikken – Forth bryder sig ikke om den, men det gør koden meget læsbar!
  103.  
  104. Angående "0 { result }" blive result også en variabel
  105. man fx. kan arbejde med inde i ordet?
  106. Ja, præcis! result bliver også en fuldt ud funktionel lokal variabel, som du kan læse fra og skrive til inde i ordet.
  107.  
  108. Sådan virker 0 { result }
  109.  
  110. : a+bEXP2 { a b -- (a+b)^2 }
  111. 0 { result } \ result initialiseres til 0
  112. a a * to result \ result = a*a
  113. b b * +to result \ result += b*b
  114. a b * 2 * +to result \ result += 2*a*b
  115. result \ læg result på stakken
  116. ;
  117.  
  118. result er en ægte lokal variabel
  119. Efter 0 { result } kan du:
  120.  
  121. Læse værdien: result henter den aktuelle værdi på datastakken
  122.  
  123. Skrive værdien: to result sætter ny værdi
  124.  
  125. Tilføje til værdien: +to result øger den eksisterende værdi
  126.  
  127. Bruge i beregninger: result 2 *, result a + osv.
  128.  
  129. Flere lokale variabler samtidigt
  130.  
  131. : complex { x y -- result }
  132. 0 { sum }
  133. 1 { count }
  134.  
  135. x to sum \ sum = x
  136. y +to sum \ sum += y
  137. count 2 * to count \ count = 2
  138.  
  139. sum count * \ return sum*count
  140. ;
  141.  
  142. Vigtige regler
  143. Alle lokale variabler (både input som a b og interne som result) er tilgængelige i hele ordet
  144.  
  145. to variabel: Sætter værdien (overskriver)
  146.  
  147. +to variabel: Tilføjer til værdien (sum)
  148.  
  149. Ingen kollision: result er forskellig fra a og b – hver er sin egen "boks"
  150.  
  151. Scope: Alle lokaler findes kun inde i dette ord, forsvinder når ordet afsluttes
  152.  
  153. 0 { result } skaber altså en lokal variabel result startende med værdien 0,
  154. som du frit kan arbejde med ligesom a og b.
  155. Det er med til at gøre koden læsbar uden at skulle jonglere med stack‑elementer hele tiden.
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment