Make a `rerun` command for Mini Micro
Tags: miniscript, minimicro, programming, tutorial
Published: 2023-01-21 14:33:55

[Mini Micro](https://miniscript.org/MiniMicro) is a virtual computer and development environment for [MiniScript](https://miniscript.org). 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](https://miniscript.org/wiki/How_to_convert_a_.minidisk_into_a_folder) rather than a minidisk file.)

In this case, you're faced with the need to [`reset`](https://miniscript.org/wiki/Reset) all variables, [`load`](https://miniscript.org/wiki/Load) your program, and [`run`](https://miniscript.org/wiki/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
```

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`](https://miniscript.org/wiki/Pprint) [`stackTrace`](https://miniscript.org/wiki/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**](https://miniscript.org/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
```

This uses a couple of tricks which you could glean from the startup.ms file on the system disk ([/sys/startup.ms](https://github.com/JoeStrout/minimicro-sysdisk/blob/master/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!