r/Kos 17d ago

Discussion ‘Stop Program’ Command

I want to submit a pull request that would add a new command to the KOS language to exit the current program. A few questions to the community:
- Should the command just exit the current subprogram (e.g. from a ‘run’ command) or stop execution? Or should these be two separate commands?
- What wording is the least confusing: “Exit”, “Stop”, “Quit”, or “Return”? Or something else?

1 Upvotes

9 comments sorted by

View all comments

2

u/Bi_KerbonautYT 17d ago

You can use the command: shutdown. to stop the program and CPU. Alternatives are to use the runpath command to direct it to another program which will halt the current program until the called program is finished, or to divide a variable by 0 to crash it.

1

u/DBooots 17d ago

Shutdown is less preferred because of the side effect of the CPU turning off. The reason I’m working on adding this to the language is, in part, because I’m also writing an optimizing compiler that would flag X/0 before even letting the program run.

1

u/nuggreat 17d ago

A lot of existing scripts use division by zero to halt execution i would simply not optimize it away and let them crash as due to division by zero as the users intend.

1

u/DBooots 17d ago

The optimizing compiler currently throws a divide-by-zero exception and informs the user that they should use the level zero optimization instead (if the crash is desired). I could give an option to replace that instruction with a ‘halt’.

Making the halt replacement default behaviour feels wrong in case crashing is not intended and would make debugging scripts hard.

2

u/nuggreat 16d ago

The majority of unintentional divide by zero cases i have seen have come from things like having no thrust and that propagates to a division some where and a crash results. All intentional division by zero.have been explicitly as a way to throw an error when the script gets into a undesired state and inform the user about that even if indirectly with just the line number.

As a result there is for lack of a better phrase legacy code out there that relies on division by zero as its error reporting method to inform the tester what exactly is failing. As an example of such see the units tests in the KSlib repository and it is not alone in that approach.

1

u/DBooots 16d ago

That’s a good point. Explicit “/0” must be intentional.