Files
corelang/code_generating_script.py
2022-06-20 20:14:34 +02:00

101 lines
1.9 KiB
Python

result = """
//
// Generated using code_generating_script.py
//
"""
types = ["S64", "S32", "S16", "S8", "U64", "U32", "U16", "U8", "F32", "F64"]
operations = [["+", "ADD"], ["-", "SUB"], ["/", "DIV"], ["*", "MUL"], ["%", "MOD"]]
enum = ["ADD", "SUB", "DIV", "MUL", "MOD", "PUSH"]
excludes = [["F64", "MOD"], ["F32", "MOD"]]
#
# Generate enum
#
if False:
for T in types:
for op in enum:
result += f" INS_{op}_{T},\n"
#
# Generate utility functions
#
if True:
for T in types:
t = T.lower()
result += f"""
force_inline void
ins_push_{t}(Bc *b, {T} value){{
auto data = b->stack_pointer;
b->stack_pointer += 2*sizeof(U64);
C({T}, data) = value;
data += sizeof(U64);
C(U64, data) = TYPE_{T};
}}
force_inline void
emit_push_{t}(Bc *bc, {T} emit_value){{
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8)+sizeof({T}));
*instruction = INS_PUSH_{T};
{T} *value = ({T} *)(instruction + 1);
*value = emit_value;
}}
"""
for symbol, OP in operations:
op = OP.lower()
result += f"""
force_inline void
emit_{op}_{t}(Bc *bc){{
U8 *instruction = (U8 *)arena_push_size(&bc->instructions, sizeof(U8));
*instruction = INS_{OP}_{T};
}}
"""
#
# Generate switch cases
#
if False:
for T in types:
t = T.lower()
for symbol, op_name in operations:
result += f"""
case INS_{op_name}_{T}:{{
{T} l = ins_pop_t(b, {T});
{T} r = ins_pop_t(b, {T});
{T} result = l {symbol} r;
ins_push_{t}(b, result);
}}break;
"""
result += f"""
case INS_PUSH_{T}:{{
// Fetch value from instruction
// instructions are tightly packed so we
// move pointer by the type size
auto value = ({T} *)b->ins_pointer;
b->ins_pointer += sizeof({T});
ins_push_{t}(b, *value);
}} break;
"""
result += """
//
// **End** of generated using code_generating_script.py
//
"""
#
# Copy to **WINDOWS** clipboard
#
import subprocess
subprocess.run("clip", universal_newlines=True, input=result)