Fun stack machine code (Python 3) posted to github in file sm.py
.
Examples:
1. Simple instruction: run("1 2 add") ==> 3
2. More complicated code: run("1 2 add 2 3 add mul") ==> 15
3. New instruction: run("defprog cube dup dup mul mul /defprog 3 cube") ==> 27
4. Use variables: run("3 a sto 4 b sto a rcl square b rcl square add") ==> 25
You can also run this at the command line, e.g., python3 sm.py 1 2 add
.
Verbosity is on by default, in which case you get a stack trace, e.g.,
[nano] python3 sm.py 1 2 add 3 add
=================================
code: [‘1’, ‘2’, ‘add’, ‘3’, ‘add’]
———
token: 1
stack: [1]
———
token: 2
stack: [1, 2]
———
token: add
in exop, stack: [1, 2, ‘add’]
stack: [3]
———
token: 3
stack: [3, 3]
———
token: add
in exop, stack: [3, 3, ‘add’]
stack: [6]
==============
6
Say python3 sm.py
for more information.
zipTimer: an iPod/iPhone app for pacing piano practice, cooking, workouts, you name it.