-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15 Generating ChaiVM/interpreter/autogen/operations.hpp from yaml us…
…ing python
- Loading branch information
1 parent
a3d8a67
commit 9922b98
Showing
9 changed files
with
73 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ build/ | |
# ide stuff | ||
.idea | ||
cmake-build-debug | ||
include/ChaiVM/interpreter/autogen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,60 @@ | ||
# @todo #8:90m Finish this script and genereate operations.hpp via yml file | ||
|
||
import sys | ||
from datetime import datetime | ||
import jinja2 | ||
import yaml | ||
with open("resources/instructions.yml") as file: | ||
full = yaml.safe_load(file) | ||
import os | ||
|
||
def main() -> int: | ||
if len(sys.argv) != 3: | ||
raise RuntimeError('Should be 2 args, received ' + str(len(sys.argv))) | ||
with open(sys.argv[2]) as file: | ||
full = yaml.safe_load(file) | ||
instructions = full["instructions"] | ||
opcodes = set() | ||
# print(instructions) | ||
opcodes_arr = ["Inv"] * 255 | ||
|
||
# Validating yaml file | ||
for instruction in instructions: | ||
# print(instruction) | ||
old_len = len(opcodes) | ||
opcodes.add(instruction["fixedvalue"]) | ||
new_len = len(opcodes) | ||
if (new_len == old_len): | ||
raise RuntimeError("opcode " + str(instruction["fixedvalue"]) + " repeats") | ||
opcodes_arr[instruction["fixedvalue"]] = instruction["mnemonic"] | ||
# print(max(opcodes)) | ||
tpl = """{{ disclaimer }} | ||
#pragma once | ||
namespace chai::interpreter { | ||
enum Operation { | ||
Inv = 0, | ||
{% for n, item in enumerate(items, 1) %} | ||
{{ item.mnemonic }} = {{ item.fixedvalue }}, | ||
{% endfor %} | ||
}; | ||
} // namespace chai::interpreter | ||
instructions = full["instructions"] | ||
opcodes = set() | ||
print(instructions) | ||
opcodes_arr = ["Inv"] * 255 | ||
""" | ||
content = { | ||
'disclaimer': '/* This file was automatically generated by the script ' + | ||
sys.argv[0] + ' at ' + datetime.now().strftime("%d.%m.%Y %H:%M:%S") + ' */', | ||
'items': instructions, | ||
'enumerate': enumerate, | ||
} | ||
|
||
for instruction in instructions: | ||
print(instruction) | ||
old_len = len(opcodes) | ||
opcodes.add(instruction["fixedvalue"]) | ||
new_len = len(opcodes) | ||
if(new_len == old_len): | ||
raise RuntimeError("opcode " + string(instruction["fixedvalue"]) + " repeats") | ||
opcodes_arr[instruction["fixedvalue"]] = instruction["mnemonic"] | ||
print(max(opcodes)) | ||
directory = os.path.dirname(sys.argv[1]) | ||
if not os.path.exists(directory): | ||
os.makedirs(directory) | ||
fp = open(sys.argv[1], 'w') | ||
fp.write( | ||
jinja2 | ||
.Template(tpl, trim_blocks=True) | ||
.render(content) | ||
) | ||
fp.close() | ||
return 0 | ||
|
||
for instruction in instructions: | ||
print(instruction["mnemonic"] + ",") | ||
|
||
print("{", end=" ") | ||
for operation in opcodes_arr: | ||
print(operation + ",", end=" ") | ||
print("}") | ||
if __name__ == '__main__': | ||
sys.exit(main()) |
9922b98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
8-7b1130e7
disappeared fromtools/opcode2operation-generator.py
), that's why I closed #15. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.9922b98
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
15-cdb3f64a
discovered inCMakeLists.txt
) and submitted as #49. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.