Shell: a token threaded based script language

The Shell library allows a simple script language to be added to a sketch. The shell is a token threaded based virtual machine where the tokens (instructions) are characters (abbreviated commands).

The implementation is highly influenced by Forth (and PostScript) but there is no token compiler (included). The command string is the code. This allows a very minimalistic implementation that can be reduced to a few Kbyte.

The classical blink sketch in the script language is:

13O{13H1000D13L1000DT}w

Which expanded with "instruction names" to:

13 Output { 13 High 1000 Delay 13 Low 1000 Delay True } while

Please see the repository for further details and example sketches.

Cheers!

Interesting.

Can you generate and implement the text for a "script" from within your Arduino program, or must it be done when the program is being written.

...R

Robin2:
Can you generate and implement the text for a “script” from within your Arduino program, or must it be done when the program is being written.

You can generate a script at run-time and execute it. It could come partially from memory, serial, disk, network, etc, One possible application is dynamic configuration, special start-up code.

The ShellDemo reads from Serial and executes it as a script.

Think of this as a scaled down variant of Forth but with PostScript code blocks and control structure words possible to integrate with other functions in your sketch.

Cheers!

Well done. The trick is to add features but not over complicate the system. The compactness of the scripts is great – clever use of single characters.

David

I would possibly like to use this in the OpenLCB project to add scripting to nodes. :o :)

dpharris:
I would possibly like to use this in the OpenLCB project to add scripting to nodes. :o :slight_smile:

That sounds like a perfect use case for this script language. It is work in progress but stable enough to test the basic ideas.

Cheers!

With some excellent testing and suggestions by @dpharris the Shell library is approaching a first release. Below is screenshot when running the virtual stack machine in trace mode with a script version of the classical Blink sketch. |500x396 The script language is a complete stack machine with function declaration, parameter frames, control structures, and the core Arduino functions as operations. It also includes high level support for timed functions (i.e. "blink without delay" pattern) and may be integrated with a Scheduler.

For more details and example sketches please see the repository.

Cheers!

The latest update of the Arduino Shell includes:

  1. Scripts may be stored in SRAM, EEPROM and PROGMEM. SRAM is typically used for interactive mode (command line buffer). EEPROM is used for dynamically defined and updated functions. And PROGMEM for statically defined scritps.
  2. Heap usage has been removed and SRAM usage reduced. EEPROM is used for dictionary strings.
  3. Additional address space information in script tracing.
  4. Variables may be mirrored in EEPROM and restored on startup.
  5. Additional functions for dictionary handling; a(llocated) dictionary space, f(orget) entries.
  6. Updated loop control structure.
  7. Performance improvements.

Cheers!