KO
games/alis / src / Alis / Compiled / Templates / OpersA.php · commit 9c722cf
<?phpnamespace App\Alis\Compiled\Templates;use App\Alis\Alis;use App\Alis\Compiled\Context;use App\Alis\Compiled\NoTemplate;use App\Alis\Compiled\Templates;/** * Expression elements 0x04, 0x0a .. 0x3a (fn/opernames.php): pointers / strings, tables, direct, main, other * entity ("him") variables, pile, eval, fin. Versions below 60 only (the compiler never compiles v6); the handlers * that read the stream differently in version 10 are left to the VM when the game is version 10. */final class OpersA{    private static function v10(string $what): void    {        if (Alis::$platform->version == 10) {            throw new NoTemplate($what . ' (version 10)');        }    }    private static function hasEval(array $item): bool    {        if ($item['c'] === 0x38) {            return true;        }        foreach ($item['sub'] as $x) {            if (self::hasEval($x)) {                return true;            }        }        return false;    }    public static function register(): void    {        $o = &Templates::$oper;        // oimmp: the string goes to sd7        $o[0x04] = static function (array $e): string {            self::v10('oimmp');            return sprintf('\App\Alis\mem_putstr(Alis::$sd7, %s);', Context::str($e['a']));        };        // olocp / odirp / omainp: a string of the variables area to sd7        $o[0x0a] = static function (array $e): string {            self::v10('olocp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, $s->vram_org + %d);', $e['a']);        };        $o[0x16] = static function (array $e): string {            self::v10('odirp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, $s->vram_org + %d);', $e['a']);        };        $o[0x22] = static function (array $e): string {            self::v10('omainp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, Alis::$basemain + %d);', $e['a']);        };        // oloctp / odirtp / omaintp: tabstring        $o[0x0c] = static function (array $e): string {            self::v10('oloctp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, \App\Alis\tabstring($s->vram_org + %d));', $e['a'] & 0xffff);        };        $o[0x18] = static function (array $e): string {            self::v10('odirtp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, \App\Alis\tabstring($s->vram_org + %d));', $e['a']);        };        $o[0x24] = static function (array $e): string {            self::v10('omaintp');            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, \App\Alis\tabstring(Alis::$basemain + %d));', $e['a'] & 0xffff);        };        // oloctc / odirtc / omaintc: tabchar        $o[0x0e] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\s8(\App\Alis\xread8(\App\Alis\tabchar($s->vram_org + %d)));', $e['a'] & 0xffff);        $o[0x1a] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\s8(\App\Alis\xread8(\App\Alis\tabchar($s->vram_org + %d)));', $e['a']);        $o[0x26] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\s8(\App\Alis\xread8(\App\Alis\tabchar(Alis::$basemain + %d)));', $e['a'] & 0xffff);        // olocti / odirti / omainti: tabint        $o[0x10] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\xread16(\App\Alis\tabint($s->vram_org + %d));', $e['a'] & 0xffff);        $o[0x1c] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\xread16(\App\Alis\tabint($s->vram_org + %d));', $e['a']);        $o[0x28] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\xread16(\App\Alis\tabint(Alis::$basemain + %d));', $e['a'] & 0xffff);        // odirb / odirw        $o[0x12] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\s8(\App\Alis\xread8($s->vram_org + %d));', $e['a']);        $o[0x14] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\xread16($s->vram_org + %d);', $e['a']);        // omainb / omainw        $o[0x1e] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\s8(\App\Alis\xread8(Alis::$basemain + %d));', $e['a']);        $o[0x20] = static fn (array $e): string => sprintf('Alis::$varD7 = \App\Alis\xread16(Alis::$basemain + %d);', $e['a']);        // ohimb / ohimw: a variable of another entity (D7 unchanged when it is dead, v3 DOS)        $him = static function (string $read): \Closure {            return static function (array $e) use ($read): string {                [$entOff, $off] = $e['a'];                return sprintf('$t = \App\Alis\xread32(Alis::$atent + (\App\Alis\xread16($s->vram_org + %d) & 0xffff));', $entOff) . "\n"                    . 'if (!(Alis::$platform->kind == \App\Alis\C::EPlatformPC && Alis::$platform->version >= 30 && $t <= 0)) {' . "\n"                    . '    Alis::$varD7 = ' . sprintf($read, $off) . ';' . "\n}";            };        };        $o[0x2a] = $him('\App\Alis\s8(\App\Alis\xread8($t + %d))');        $o[0x2c] = $him('\App\Alis\xread16($t + %d)');        $o[0x2e] = static function (array $e): string {            self::v10('ohimp');            [$entOff, $off] = $e['a'];            return sprintf('\App\Alis\mem_strcpy(Alis::$sd7, \App\Alis\xread32(Alis::$atent + (\App\Alis\xread16($s->vram_org + %d) & 0xffff)) + %d);', $entOff, $off);        };        // ohimtp / ohimtc / ohimti: nothing in the C (version 10 reads more: left to the VM)        foreach ([0x30, 0x32, 0x34] as $code) {            $o[$code] = static function (array $e): string {                self::v10('ohimt');                return '';            };        }        // opile        $o[0x36] = static fn (array $e): string => 'Alis::$varD7 = Alis::$varD6;' . "\n"            . 'Alis::$varD6 = \App\Alis\hread32(Alis::$acc);' . "\n" . 'Alis::$acc += 4;';        // oeval: the elements up to ofin, each as readexec_opername() does        $o[0x38] = static function (array $e, Context $c): string {            foreach ($e['sub'] as $x) {                if (self::hasEval($x)) {                    throw new NoTemplate('nested oeval');                }            }            $out = ['Alis::$oeval_loop = 1;'];            foreach ($e['sub'] as $x) {                $out[] = $c->opername($x);            }            return implode("\n", $out);        };        // ofin (inside an oeval: the loop stops; version < 60)        $o[0x3a] = static fn (array $e): string => 'Alis::$oeval_loop = 0;';    }}