IT
games/alis / tools / lang / reference.php · commit 9c722cf
<?php// The reference tables of docs/language.md, from the grammar (src/Alis/Lang/Grammar.php)://   bin/jumphp php games/alis tools/lang/reference.php > /tmp/ref.mdrequire __DIR__ . '/../../vendor/autoload.php';use App\Alis\Lang\Grammar;use App\Alis\Lang\Names;$tok = ['E' => 'expr', 'S' => 'cible', 'A' => 'cible+', 'b' => 'octet', 'w' => 'mot', 'W' => 'mot', 'l' => 'long',    'z' => '"texte"', 'v' => '&adresse', 'j8' => 'étiquette', 'j16' => 'étiquette', 'j24' => 'étiquette'];$special = [    'cesc1' => '(voir les échappements)', 'cdim' => 'offset, compteur, taille, [valeurs]', 'cswitch1' => 'expr (bloc `case`)',    'cswitch2' => 'expr, base (bloc `goto`)', 'cload' => 'id, "fichier" | expr', 'cfopen' => 'z"fichier", mode | expr, expr',    'csend' => 'entité, valeur, …', 'czap' => 'expr ×4 (×5 si la durée n\'est pas la constante 0)', 'cdefvect' => 'hex"…"',    'cchartmap' => '&adresse, mode, (&adresse si mode = 0 ou 1 | expr)', 'cdefsc' => 'mot, octet, hex"(32 octets)"', 'cdefworld' => 'mot, hex"(6 octets)"',];echo "| Code | Instruction | Opérandes |\n|---|---|---|\n";foreach (Grammar::OPCODES as $code => [$name, $spec]) {    $word = Names::statement($name);    $ops = $special[$name] ?? implode(', ', array_map(fn ($t) => $tok[$t] ?? ($t[0] === 'r' ? 'hex"(' . substr($t, 1) . ' octets)"' : $t), array_filter(explode(' ', $spec))));    $form = match (true) {        str_starts_with($name, 'cjmp') && $spec[0] === 'j' => '`goto` étiquette',        str_starts_with($name, 'cjsr') && $spec[0] === 'j' => '`call` étiquette',        str_starts_with($name, 'cstart') => '`start` étiquette',        str_starts_with($name, 'cbz') => '`ifz goto` étiquette, ou `ifnot expr goto` étiquette',        str_starts_with($name, 'cbnz') => '`ifnz goto` étiquette, ou `if expr goto` étiquette',        str_starts_with($name, 'cbeq') => '`ifeq goto` étiquette',        str_starts_with($name, 'cbne') => '`ifne goto` étiquette',        str_starts_with($name, 'cloop') => '`loop` cible `goto` étiquette',        $name === 'cstore' => 'cible `=` expr',        $name === 'cadd' => 'cible `+=` expr',        $name === 'csub' => 'cible `-=` expr',        $name === 'cswitch1' => '`switch` expr … `end`',        $name === 'cswitch2' => '`table` expr, base … `end`',        default => "`$word`" . ($ops !== '' ? ' ' . $ops : ''),    };    printf("| 0x%02x | %s | %s |\n", $code, $name, $form);}echo "\n| Échappement | Instruction | Opérandes |\n|---|---|---|\n";foreach (Grammar::ESC1 as $code => [$name, $spec]) {    printf("| 0x01 0x%02x | %s | `%s`%s |\n", $code, $name, $code ? substr($name, 1) : 'esc1 nul', $spec ? ' ' . implode(', ', array_map(fn ($t) => $tok[$t], explode(' ', $spec))) : '');}echo "\n| Code | Élément | Écriture |\n|---|---|---|\n";foreach (Grammar::OPERS as $code => [$name, $spec, $kind]) {    $v = Names::variable($name);    $w = match (true) {        isset(Names::BINOPS[$name]) => 'a `' . Names::BINOPS[$name] . '` b',        isset(Names::FUNCS[$name]) => '`' . Names::FUNCS[$name] . '(…)`',        $name === 'oimmb' => 'nombre de -128 à 127', $name === 'oimmw' => 'nombre de -32768 à 32767', $name === 'oimml' => 'autre nombre',        $name === 'oimmp' => '"texte"', $name === 'oeval' => '( … )', $name === 'ofin' => '(fin d\'une parenthèse)',        $v !== null => '`' . $v[0] . ($spec === 'h' ? '[var, offset]' : '[offset]') . ($kind === 't' ? '(index…)' : '') . '`' . ($v[1] === 'dir' ? ' (offset 0 à 255)' : ($v[1] === 'loc' ? ' (autres offsets)' : '')),        default => $name,    };    printf("| 0x%02x | %s | %s |\n", $code, $name, $w);}