|
96 | 96 | </div><!--header--> |
97 | 97 | <div class="contents"> |
98 | 98 | <div class="textblock"><div class="fragment"><div class="line">EVAL string-expression</div> |
99 | | -</div><!-- fragment --><p>Executes a <b>single complete line of BASIC</b> supplied in <span class="tt">string-expression</span> in the <b>current program's context</b>.</p> |
100 | | -<ul> |
101 | | -<li>The string must form <b>one valid statement line</b> (for example <span class="tt">PRINT "Hello"</span>, <span class="tt">X = 10</span>, <span class="tt">PROCdemo</span>).</li> |
102 | | -<li>On <b>error</b>, <span class="tt">ERR$</span> is set to the error message and <span class="tt">ERR</span> is set to <span class="tt">1</span>. Execution then continues at the statement after <span class="tt">EVAL</span>.</li> |
103 | | -<li><b>Recursive EVAL is not permitted</b> (code running under <span class="tt">EVAL</span> may not call <span class="tt">EVAL</span> again).</li> |
104 | | -</ul> |
105 | | -<dl class="section remark"><dt>Remarks</dt><dd>If you need a literal double quote inside the evaluated line, insert it using <span class="tt">CHR$(34)</span>. </dd> |
106 | | -<dd> |
107 | | -For example: <span class="tt">EVAL "PRINT " + CHR$(34) + "Hello" + CHR$(34)</span>.</dd></dl> |
108 | | -<hr /> |
109 | | -<h3 class="doxsection"><a class="anchor" id="examples-128"></a> |
110 | | -Examples</h3> |
111 | | -<p><b>Print from a generated line</b> </p><div class="fragment"><div class="line">EVAL "PRINT " + CHR$(34) + "Hello from EVAL!" + CHR$(34)</div> |
112 | | -</div><!-- fragment --><p><b>Set a variable dynamically</b> </p><div class="fragment"><div class="line">NAME$ = "COUNT"</div> |
113 | | -<div class="line">EVAL NAME$ + " = 42"</div> |
114 | | -<div class="line">PRINT COUNT</div> |
115 | | -</div><!-- fragment --><p><b>Call a procedure</b> </p><div class="fragment"><div class="line">DEF PROCgreet</div> |
116 | | -<div class="line"> PRINT "Greetings"</div> |
117 | | -<div class="line">ENDPROC</div> |
| 99 | +<div class="line">````</div> |
| 100 | +<div class="line"> </div> |
| 101 | +<div class="line">Executes BASIC code supplied in `string-expression`.</div> |
| 102 | +<div class="line"> </div> |
| 103 | +<div class="line">* If the string contains a **single line**, it is executed in the **current program's context**.</div> |
| 104 | +<div class="line">* If the string contains **multiple lines**, it is executed as an **anonymous child program** in a new process context.</div> |
118 | 105 | <div class="line"> </div> |
119 | | -<div class="line">EVAL "PROCgreet"</div> |
120 | | -</div><!-- fragment --><p><b>Error handling (ERR/ERR$ set, program continues)</b> </p><div class="fragment"><div class="line">ERR = 0</div> |
121 | | -<div class="line">ERR$ = ""</div> |
| 106 | +<div class="line">On **error**, `ERR$` is set to the error message and `ERR` is set to `1`. Execution then continues at the statement after `EVAL`.</div> |
122 | 107 | <div class="line"> </div> |
123 | | -<div class="line">EVAL "A = 1/0"</div> |
| 108 | +<div class="line">* Recursive **single-line EVAL** is not permitted.</div> |
124 | 109 | <div class="line"> </div> |
125 | | -<div class="line">IF ERR <> 0 THEN</div> |
126 | | -<div class="line"> PRINT "EVAL failed: "; ERR$</div> |
127 | | -<div class="line">ENDIF</div> |
128 | | -</div><!-- fragment --><hr /> |
| 110 | +<div class="line">\remark If you need a literal double quote inside the evaluated line, insert it using `CHR$(34)`.</div> |
| 111 | +<div class="line">\remark For example: `EVAL "PRINT " + CHR$(34) + "Hello" + CHR$(34)`.</div> |
| 112 | +<div class="line"> </div> |
| 113 | +<div class="line">---</div> |
| 114 | +<div class="line"> </div> |
| 115 | +<div class="line">### Examples</div> |
| 116 | +<div class="line"> </div> |
| 117 | +<div class="line">**Print from a generated line (single-line)**</div> |
| 118 | +</div><!-- fragment --><p> basic EVAL "PRINT " + CHR$(34) + "Hello from EVAL!" + CHR$(34) </p><div class="fragment"><div class="line">**Set a variable dynamically**</div> |
| 119 | +</div><!-- fragment --><p> basic NAME$ = "COUNT" EVAL NAME$ + " = 42" PRINT COUNT </p><div class="fragment"><div class="line">**Call a procedure**</div> |
| 120 | +</div><!-- fragment --><p> basic DEF PROCgreet PRINT "Greetings" ENDPROC</p> |
| 121 | +<p>EVAL "PROCgreet" </p><div class="fragment"><div class="line">**Multi-line anonymous program**</div> |
| 122 | +</div><!-- fragment --><p> basic EVAL "A=1" + CHR$(10) + "PRINT A" </p><div class="fragment"><div class="line">**Using GLOBAL variables in multi-line EVAL**</div> |
| 123 | +</div><!-- fragment --><p> basic GLOBAL A A = 5</p> |
| 124 | +<p>EVAL "PRINT A" </p><div class="fragment"><div class="line">**Error handling (ERR/ERR$ set, program continues)**</div> |
| 125 | +</div><!-- fragment --><p> basic ERR = 0 ERR$ = ""</p> |
| 126 | +<p>EVAL "A = 1/0"</p> |
| 127 | +<p>IF ERR <> 0 THEN PRINT "EVAL failed: "; ERR$ ENDIF ```</p> |
| 128 | +<hr /> |
129 | 129 | <h3 class="doxsection"><a class="anchor" id="notes-139"></a> |
130 | 130 | Notes</h3> |
131 | 131 | <ul> |
132 | | -<li>Provide <b>exactly one</b> statement; statement separators are not supported.</li> |
133 | | -<li>The evaluated line runs with access to the program's current variables, procedures, and functions.</li> |
134 | | -<li>Use <span class="tt">EVAL</span> for small, dynamic actions. For larger logic, write normal procedures/functions and call them directly.</li> |
| 132 | +<li>A <b>single-line</b> EVAL executes in the current context and has access to all variables, procedures, and functions.</li> |
| 133 | +<li>A <b>multi-line</b> EVAL runs as an anonymous child program.</li> |
| 134 | +<li>Anonymous child programs inherit <span class="tt">GLOBAL</span> variables from the parent.</li> |
| 135 | +<li>Single-line EVAL executes via a temporary injected line; therefore, recursive single-line EVAL is not permitted.</li> |
| 136 | +<li>Provide <b>exactly one statement</b> when using single-line EVAL; statement separators are not supported.</li> |
| 137 | +<li>Use EVAL for dynamic or generated code. For larger logic, prefer normal procedures or separate programs.</li> |
135 | 138 | </ul> |
136 | | -<p><b>See also:</b> <a class="el" href="DEF.html">DEF</a>, <a class="el" href="PROC.html">PROC</a>, <a class="el" href="FN.html">FN</a>, <a href="https://github.com/brainboxdotcc/retro-rocket/wiki/Builtin-Variables"><span class="tt">ERR</span>/<span class="tt">ERR$</span></a> </p> |
| 139 | +<p><b>See also:</b> <a class="el" href="DEF.html">DEF</a>, <a class="el" href="PROC.html">PROC</a>, <a class="el" href="FN.html">FN</a>, <span class="tt">ERR</span>, <span class="tt">ERR$</span> </p> |
137 | 140 | </div></div><!-- contents --> |
138 | 141 | </div><!-- PageDoc --> |
139 | 142 | </div><!-- doc-content --> |
|
0 commit comments