ES
games/alis / src / Alis / Compiled / Templates.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled;/** * The translations of the instructions to PHP, by opcode name (Lang\Grammar): a template receives the decoded * instruction (Lang\Decoder: at, end, op, ops) and the compiler's context, and returns PHP statements, or null to let * the VM execute that instruction at its address (always correct: the compiled code and the VM share everything). * Each file of Templates/ registers its group (Templates/<Group>.php: a class with a static register() method), so * groups are written and checked apart. */final class Templates{    /** @var array<string, callable(array, Context): ?string> opcode name => template */    public static array $op = [];    /** @var array<int, callable(array, Context): string> expression element code (opernames) => template */    public static array $oper = [];    /** @var array<int, callable(array, Context): string> store target code (storenames) => template */    public static array $store = [];    /** @var array<int, callable(array, Context): string> add target code (addnames) => template */    public static array $add = [];    private static bool $loaded = false;    /** @var list<string> only these groups (file names without .php; tools/run.php --compiled-only=), empty = all */    public static array $only = [];    public static function load(): void    {        if (self::$loaded) {            return;        }        self::$loaded = true;        foreach (glob(__DIR__ . '/Templates/*.php') ?: [] as $file) {            $group = basename($file, '.php');            if (self::$only !== [] && !in_array($group, self::$only, true)) {                continue;            }            $class = __NAMESPACE__ . '\\Templates\\' . $group;            try {                if (class_exists($class) && method_exists($class, 'register')) {                    $class::register();                }            } catch (\Throwable $e) {                error_log("ALIS compiler: templates $group skipped: " . $e->getMessage());   // a file being written            }        }    }}