A Python 3 implementation of the famous META II metacompiler virtual machine and a compiler writing domain-specific language.
Based on the original virtual machine from the “META II: A Syntax-Oriented Compiler Writing Language” 1965 paper[1], which is also explained in a brilliant tutorial[2] by James M. Neighbors. Wikipedia also has a page on META II[3]. There are numerous implementations in different languages out there [4][5][6].
Sources:
- http://www.chilton-computing.org.uk/acl/literature/reports/p025.htm
- http://www.bayfronttechnologies.com/mc_tutorial.html
- https://en.wikipedia.org/wiki/META_II
- https://github.com/stevenbagley/metaii
- https://github.com/EyeBool/Meta-II-Compiler
- https://github.com/siraben/meta-II
The parsing virtual machine is implemented in metaiivm.py
. VM code for the DSL is in
metaii.masm
. META II DSL implemented in META II DSL is in metaii.meta
. And yes, it’s a
recursive definition, thus “metacompiler”.
Having these it’s possible to regenerate the masm
file:
python metaiivm.py metaii.masm < metaii.meta > metaii-copy.masm
diff metaii-copy.masm metaii.masm
# nothing here - we can reproduce all the VM code necessary from the DSL
It’s also possible to use the VM from Python code:
import metaiivm
import io
code = parse_code(open("metaii.masm"))
meta_dsl = open("metaii.meta").read()
output_file = io.StringIO()
vm = metaiivm.VM(meta_dsl, output_file)
vm.run(code)
# print a fresh META II VM code copy
print(output_file.getvalue())
Running VM tests requires the pytest package:
py.test metaiivm_test.py
# ....