Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

3.9 KB
Raw Blame
"""Script op table shared by the compiler and the C VM.

Inputs and outputs are ordered top to bottom (highest terminal z first), which
matches the operand order shown in the Fancade editor (Num1 above Num2, ...).
"""
from blockdefs import DEFS

# stock ids that compile to VM ops (everything else is ignored or handled specially)
SUPPORTED = [
    36, 38, 42, 449, 451,                      # literals
    46, 48, 50, 52, 54, 56,                    # get variable
    428, 430, 432, 434, 436, 438,              # set variable
    58, 62, 66, 70, 74, 78,                    # set via pointer
    82, 86, 461, 465, 469, 473,                # list element
    556, 558,                                  # increase / decrease
    90, 92, 96, 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 146,
    150, 156, 162, 168, 172, 176, 180, 184, 186, 188, 190, 194, 200, 204, 208,
    413, 417, 421, 440, 442, 453, 455, 457, 481, 570, 574, 578, 580,
    216, 220, 224, 477, 564,                   # screen / sensors values
    228, 278, 288, 489,                        # raycast, get position, get velocity, get size
    234, 238, 242, 248, 566, 409, 588, 592, 401, 560,  # control / sensors
    252, 256, 260, 268, 274,                   # game
    282, 292, 298, 306, 310, 316, 320, 324, 328, 332, 336,  # objects / physics
    340, 346, 352, 358, 364, 370, 376,         # constraints
    264, 391, 397, 485,                        # sound, random seed
    16, 20, 24, 28, 32,                        # inspect (no-op)
]

EXTRA_OPS = ['CUSTOM']  # child environment (custom script / scripted object)


def terminal_lists(id):
    name, kind, size, terms = DEFS[id]
    ins = [t for t in terms if t and t[1] == 'In' and t[0] != 'Void']
    outs = [t for t in terms if t and t[1] == 'Out' and t[0] != 'Void']
    execs = [t for t in terms if t and t[1] == 'Out' and t[0] == 'Void' and t[2] != 'After']
    has_before = any(t and t[1] == 'In' and t[0] == 'Void' for t in terms)
    key = lambda t: -t[3][2]
    return name, kind, sorted(ins, key=key), sorted(outs, key=key), sorted(execs, key=key), has_before


OPS = {}
for i, id in enumerate(SUPPORTED):
    name, kind, ins, outs, execs, has_before = terminal_lists(id)
    OPS[id] = dict(index=i, name=name, kind=kind, ins=ins, outs=outs, execs=execs, active=kind == 'Active')

OP_CUSTOM = len(SUPPORTED)


def c_name(id):
    n = OPS[id]['name'].upper().replace(' ', '_').replace('?', '')
    if id in (46, 48, 50, 52, 54, 56):
        n = 'GET_VAR_' + ['NUM', 'VEC', 'ROT', 'TRU', 'OBJ', 'CON'][(id - 46) // 2]
    if id in (428, 430, 432, 434, 436, 438):
        n = 'SET_VAR_' + ['NUM', 'VEC', 'ROT', 'TRU', 'OBJ', 'CON'][(id - 428) // 2]
    if id in (16, 20, 24, 28, 32):
        n = 'INSPECT_%d' % id
    if id in (58, 62, 66, 70, 74, 78):
        n = 'SET_PTR_' + ['NUM', 'VEC', 'ROT', 'TRU', 'OBJ', 'CON'][(id - 58) // 4]
    if id in (82, 86, 461, 465, 469, 473):
        n = 'LIST_' + {82: 'NUM', 86: 'OBJ', 461: 'VEC', 465: 'ROT', 469: 'TRU', 473: 'CON'}[id]
    return 'OP_' + n


def c_header():
    lines = ['/* Generated by tools/ops.py. Do not edit. */', '#ifndef OPS_H', '#define OPS_H', '', 'enum {']
    for id in SUPPORTED:
        o = OPS[id]
        lines.append('  %s, /* %d: ins %s outs %s execs %s */' % (
            c_name(id), id, [t[2] for t in o['ins']], [t[2] for t in o['outs']], [t[2] for t in o['execs']]))
    lines.append('  OP_CUSTOM,')
    lines.append('  OP_COUNT')
    lines.append('};')
    lines.append('')
    lines.append('/* per op: number of value inputs, value outputs, extra exec outputs, flags (1 = active) */')
    lines.append('static const unsigned char op_shape[OP_COUNT][4] = {')
    for id in SUPPORTED:
        o = OPS[id]
        lines.append('  {%d, %d, %d, %d}, /* %s */' % (len(o['ins']), len(o['outs']), len(o['execs']), 1 if o['active'] else 0, o['name']))
    lines.append('  {0, 0, 0, 1}, /* custom */')
    lines.append('};')
    lines.append('')
    lines.append('#endif')
    return '\n'.join(lines) + '\n'