Files
corelang/code_generating_script.py
2022-06-21 13:05:11 +02:00

75 lines
1.7 KiB
Python

result = """
//
// Generated using code_generating_script.py
//
"""
types = ["S64", "U64", "F64"]
operations = [
["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"],
[">>", "SHR"], ["<<", "SHL"], ["&", "BITAND"], ["|", "BITOR"], ["^", "BITXOR"],
["~", "BITNOT"], ["==", "EQ"], ["!=", "NEQ"], [">", "GT"], ["<", "LT"], ["||", "OR"],
[">=", "GTE"], ["<=", "LTE"]
]
def should_skip(T, op):
if T == "F64":
if op != "DIV" and op != "SUB" and op != "ADD" and op != "MUL" and op != "EQ" and op != "NEQ"\
and op != "GT" and op != "LT" and op != "GTE" and op != "LTE":
return True
#
# Generate enum
#
if False:
for T in types:
for _, op in operations:
if should_skip(T, op):
continue
result += f" INS_{op}_{T},\n"
result += "\n"
#
# Generate switch cases
#
if True:
for T in types:
t = T.lower()
for symbol, op_name in operations:
if should_skip(T, op_name):
continue
# Unary operator special case
if symbol == "~":
result += f"""
case INS_{op_name}_{T}:{{
{T} left = ({T})b->registers[instr->left].{t};
{T} *dst = b->registers[instr->dst].pointer_{t};
*dst = {symbol}left;
}}break;
"""
continue
# Binary operation
result += f"""
case INS_{op_name}_{T}:{{
{T} left = b->registers[instr->left].{t};
{T} right = b->registers[instr->right].{t};
b->registers[instr->dst].{t} = left {symbol} right; break;
}}break;
"""
result += """
//
// **End** of generated using code_generating_script.py
//
"""
#
# Copy to **WINDOWS** clipboard
#
import subprocess
subprocess.run("clip", universal_newlines=True, input=result)