| 269 | |
| 270 | |
| 271 | def generate_class_cpp(dir: Path, bytecode_class: BytecodeClass) -> None: |
| 272 | file_stem = bytecode_class.file_stem |
| 273 | class_name = bytecode_class.class_name |
| 274 | func_names = bytecode_class.func_names |
| 275 | tk_names = bytecode_class.tk_names |
| 276 | bytecode_version = bytecode_class.bytecode_version |
| 277 | bytecode_rev = bytecode_class.bytecode_rev |
| 278 | bytecode_rev_num = bytecode_class.bytecode_rev_num |
| 279 | engine_ver_major = bytecode_class.engine_ver_major |
| 280 | variant_ver_major = bytecode_class.variant_ver_major |
| 281 | new_dir = dir |
| 282 | # ensure the directory exists |
| 283 | if not new_dir.exists(): |
| 284 | new_dir.mkdir() |
| 285 | new_file_cpp = new_dir / (file_stem + ".cpp") |
| 286 | with open(new_file_cpp, "w") as f: |
| 287 | f.write(PRELUDE) |
| 288 | f.write(CLANG_FORMAT_OFF) |
| 289 | # 1) the header declarations: |
| 290 | f.write('#include "' + file_stem + '.h"\n') |
| 291 | f.write("\n") |
| 292 | |
| 293 | # 2) the builtin function declarations: |
| 294 | if len(func_names) != 0: |
| 295 | f.write("static const Pair<String, Pair<int, int>> funcs[] = {\n") |
| 296 | for func_name in func_names: |
| 297 | if func_name == "var2bytes" or func_name == "bytes2var": |
| 298 | if bytecode_rev_num in greater_than_3_1_versions: |
| 299 | f.write('\t{ "' + func_name + '", Pair<int, int>(1, 2) },\n') |
| 300 | else: |
| 301 | f.write('\t{ "' + func_name + '", Pair<int, int>(1, 1) },\n') |
| 302 | else: |
| 303 | for builtin_func_arg_element in builtin_func_arg_elements: |
| 304 | if builtin_func_arg_element[0] == func_name: |
| 305 | last_el = builtin_func_arg_element[1][1] |
| 306 | if last_el == INT_MAX: |
| 307 | last_el = "INT_MAX" |
| 308 | f.write( |
| 309 | '\t{ "' |
| 310 | + func_name |
| 311 | + '", Pair<int, int>(' |
| 312 | + str(builtin_func_arg_element[1][0]) |
| 313 | + ", " |
| 314 | + str(last_el) |
| 315 | + ") },\n" |
| 316 | ) |
| 317 | break |
| 318 | f.write("};\n") |
| 319 | f.write("\n") |
| 320 | f.write("static constexpr int num_funcs = sizeof(funcs) / sizeof(Pair<String, Pair<int, int>>);\n") |
| 321 | # 3) the Token enum declarations: |
| 322 | f.write("enum Token {\n") |
| 323 | for tk_name in tk_names: |
| 324 | f.write("\t" + tk_name + ",\n") |
| 325 | f.write("};\n") |
| 326 | f.write("\n") |
| 327 | |
| 328 | # 3.5) the `get_token_max` function: |