| 1 |
// name: eval.ck |
| 2 |
// desc: use Machine.eval() to compile and run code from a string; |
| 3 |
// this as enables chuck to generate code to run at run-time; |
| 4 |
// powerful! perilous! |
| 5 |
// |
| 6 |
// version: requires chuck-1.5.0.5 or higher |
| 7 |
// Machine.eval() was first introduced in 1.5.0.0; |
| 8 |
// the operation semantics was changed in 1.5.0.5 for |
| 9 |
// Machine eval to run immediately, and automatically |
| 10 |
// yielding the current shred to give the eval'ed code |
| 11 |
// a chance to run -- all this without advancing time |
| 12 |
// |
| 13 |
// uncomment this to print out info about Machine: |
| 14 |
// Machine.help(); |
| 15 |
// |
| 16 |
// date: Spring 2023 |
| 17 |
|
| 18 |
// our code to run |
| 19 |
"cherr <= \"hello!\" <= IO.newline();" => string codeStr; |
| 20 |
|
| 21 |
// compile the string as code and spork it as a new shred |
| 22 |
if( !Machine.eval( codeStr ) ) <<< "error evaluating code!", "" >>>; |
| 23 |
|
| 24 |
// each of these will be evaluated and run as code, |
| 25 |
// each on its independent shred; Machine.eval() automatically |
| 26 |
// yields the originating "evaluator" shred, giving the evaluated |
| 27 |
// code a chance to run without advancing time |
| 28 |
Machine.eval( "<<< 1 >>>;" ); |
| 29 |
<<< "a" >>>; |
| 30 |
Machine.eval( "<<< 2 >>>;" ); |
| 31 |
<<< "b" >>>; |
| 32 |
Machine.eval( "<<< 3 >>>;" ); |
| 33 |
<<< "c" >>>; |