Is there a keyword in the script to exit the script?


#1

I am wondering if the scripter (not the C++ side) can indicate in a script to simply exit?


#2

There is not, but it would be possible to implement that with the new Tracer that was added in ChaiScript 6. However, this is untested. https://github.com/ChaiScript/ChaiScript/blob/develop/include/chaiscript/language/chaiscript_tracer.hpp

You could also throw an exception that you know is not going to get caught until the top level of the script. This is, unfortunately, how things like return values are currently implemented, since they require unrolling the call stack.

-Jason


#3

It seems like a reasonable way to deal with the desire to exit the script is to structure the flow of execution to allow for dropping out of the script. In other words, just let the script end when you want to end.

For example (pseudo code)

// start of script
// initialization code...

done = false;    

// live in this loop...
while (!done)
{
    //code...

// place where you want to force exit
    if(some_exit_condition_exists)
        break;
    
    // more code...
    

    // normal end...
    if (normal_end_condition_exists)
        done = true;
}

// break; or done = true; falls to here...
// end of script

#4

While this suggestion works fine in a simple use case, it becomes cumbersome in multiple levels of functions.


#5

Another idea would be to execute an invalid statement, and let it throw. I presume you could even create your own exit statement that is designed to throw.


#6

Thanks for the replies. I was considering adding a function that throws a type of object that indicates a good-exit. I just wanted to make sure there wasn’t already a feature for this purpose. As I delve further into ChaiScript, I have found there is much to the language that is only documented in the defect resolutions. The examples page is far behind, not including things like “switch” and “add_global_const”. It would be nice if someone who really knew the language well could take some time to update the examples and/or the documentation to include the depth of the language that you guys have worked so hard to add over the years.

Thanks,
Don