This script generates C++ header file with opcode constants based on YAML file definition. It may be useful for virtual machine developers. Defining opcodes in external file instead of direct C++ enum has some advantages:
- It is easier to read. Definition file contains only relevan information, no C++ code.
- You can automate the building of documentation.
- Opcode definition is isolated from your virtual machine implementation, so you can use it in non-C++ implementation (of course you'll have to modify this script a little bit).
This script uses pyyaml package. Make sure this package is installed on your machine. To install it execute the following command:
pip install pyyaml Copy opcodes.example.yaml file to your application's folder, define all opcodes you need in this file. Then run the script with specified input and output files like this:
python /path/to/script/main.py path/to/opcodes.yaml /path/to/template opcodes.hppThe first argument is a path to file with opcodes. The second argument is a path to template file (look at the templates directory). The third argument is a C++ header file name which will be generated by the script. The result constant list may look like this:
enum _GeneratedOpCodes { OPCODE_NOP = 0x00, OPCODE_B = 0xA0, OPCODE_BZ = 0xA1, OPCODE_B_FALSE = 0xA1 /* alias for BZ */, OPCODE_B_ZERO = 0xA1 /* alias for BZ */, OPCODE_B_NULL = 0xA1 /* alias for BZ */, OPCODE_BNZ = 0xA2, OPCODE_B_TRUE = 0xA2 /* alias for BNZ */, OPCODE_B_NOT_ZERO = 0xA2 /* alias for BNZ */, OPCODE_B_NOT_NULL = 0xA2 /* alias for BNZ */ };