DEV Community

JoeStrout
JoeStrout

Posted on

Make a `rerun` command for Mini Micro

Mini Micro is a virtual computer and development environment for MiniScript. It has a built-in code editor that features syntax coloring, navigation to line or function, code snippets, a nifty color picker, etc. Still, for various reasons, many developers prefer to use an external editor like Visual Studio Code on larger projects. (You can easily do this by mounting a folder rather than a minidisk file.)

In this case, you're faced with the need to reset all variables, load your program, and run it again after each change to the code. I'll confess, I usually just use the command-line history to simplify this. If I'm working on a program called "myProgram.ms", then the first time, I'll manually type

reset; load "myProgram"; run
Enter fullscreen mode Exit fullscreen mode

Then after testing, exiting out of the program (or breaking out with Control-C), and making some change with my external editor, I'll just up-arrow to recall the above command, and hit Return to run it again.

But, sometimes there are some debugging steps in between, which result in a bunch of additional commands on the command line. Maybe I've done a pprint stackTrace to see where the program broke, and then typed several variable names to see what values they contain, and called some functions directly to confirm how they behave. Now I'm ready to run the program again, but to get back to my reset/load/run command in the command history, I have to hammer my up-arrow key like a woodpecker.

So, a better solution is to add a rerun command. Its job is to do that reset/load/run sequence, but do it for any loaded program. Just put the following into your own startup.ms file:

rerun = function
    if not _sourceFile then
        _printMark "No source file loaded; `load` or `save` something first."
        return
    end if
    sourceFile = _sourceFile
    reset
    load sourceFile
    run
end function
_savedGlobals.rerun = @rerun
Enter fullscreen mode Exit fullscreen mode

This uses a couple of tricks which you could glean from the startup.ms file on the system disk (/sys/startup.ms):

  1. The path of the currently loaded program is always held in a global variable called _sourceFile.
  2. There is a _printMark function which handles simple markup (this is used to print help text for example).
  3. Any globals in the _savedGlobals map will survive a reset.

So, the above code first makes sure we have a program loaded from disk; then it saves that _sourceFile path in a local variable, and uses it to do the reset/load/run sequence. Finally, we save this new rerun function in _savedGlobals so it will always be there for you.

Now after making some change with your external editor, you need only get to the command line and type rerun to try the new code.

So why doesn't Mini Micro come with such a handy function out of the box? Well, it's a little dangerous. If you use the built-in editor to change your code, exit that editor without saving, and then type rerun, all your changes will be lost. We assume that new users, in particular, are likely to be using the built-in editor (and also more likely to be confused about things like run vs. rerun). They also sometimes do things like type code for half an hour before trying to run it. So it would result in new users losing work and having a frustrating experience.

Conversely, if you're using Visual Studio Code or some other external editor on your Mini Micro projects, you're probably a more advanced user. Adding a few lines to your own startup.ms should be no big deal. And now you know how!

Top comments (1)

Collapse
 
retrojoe64 profile image
RetroJoe64

Great tip, and very helpful. Mini Micro's "extensible by design" is one of its greatest features.