*/ private array $labels = []; /** @var list [where, bytes, base, label, line] */ private array $fixups = []; private int $line = 0; /** @var list open blocks: [if|else|while, label, label, line] */ private array $blocks = []; private int $blockSeq = 0; /** @var list tokens of the current line */ private array $t = []; private int $k = 0; /** @var array statement word => opcode */ private static array $words = []; /** @var array> table => name => code */ private static array $codes = []; /** * @param callable(string): string $readFile reads a file named in the source (the resources) */ public function compile(string $source, callable $readFile): Script { self::init(); $s = new Script(); $this->out = str_repeat("\0", 0x18); $this->labels = []; $this->fixups = []; $this->blocks = []; $entries = []; $lines = preg_split('/\r?\n/', $source); $n = count($lines); for ($this->line = 1; $this->line <= $n; $this->line++) { $text = $lines[$this->line - 1]; $this->tokenize($text); if (!$this->t) { continue; } // labels while (count($this->t) >= 2 && $this->t[0][0] === 'id' && $this->t[1][1] === ':' && $this->t[1][0] === 'op') { $name = $this->t[0][1]; if (isset($this->labels[$name])) { $this->fail("label $name defined twice"); } $this->labels[$name] = strlen($this->out); array_splice($this->t, 0, 2); } if (!$this->t) { continue; } $this->k = 0; $word = $this->t[0][1]; if ($this->t[0][0] === 'id' && in_array($word, ['script', 'id', 'class', 'memory', 'pack', 'specs', 'resources', 'main', 'before', 'after'], true)) { $this->k = 1; switch ($word) { case 'script': $s->name = $this->string(); break; case 'id': $s->id = $this->number(); break; case 'class': $s->class1 = $this->number(); $s->class2 = $this->number(); break; case 'memory': $s->words1 = $this->number(); $s->vram = $this->number(); $s->words3 = $this->number(); break; case 'pack': $s->pack = $this->number(); if ($this->peekIs('check')) { $this->k++; $s->check = $this->number(); } break; case 'specs': $s->specs = $this->hexBlob(); break; case 'resources': $s->resources = $readFile($this->string()); break; default: $entries[$word] = [$this->ident(), $this->line]; break; } $this->end(); continue; } // switch / table blocks take the following lines up to "end" if ($word === 'switch' || $word === 'table') { $block = []; while (++$this->line <= $n) { $l = trim(preg_replace('#//.*$#', '', $lines[$this->line - 1])); if ($l === 'end') { break; } if ($l !== '') { $block[] = [$l, $this->line]; } } $this->tokenize($text); $this->k = 0; $this->switchBlock($block); continue; } if ($this->t[0] === ['op', '}']) { $this->closeBlock(); continue; } $this->statement(); $this->end(); } if ($this->blocks) { $this->line = end($this->blocks)[3]; $this->fail('this block has no }'); } foreach ($this->fixups as [$at, $bytes, $base, $label, $line]) { if (!isset($this->labels[$label])) { $this->line = $line; $this->fail("unknown label $label"); } $off = $this->labels[$label] - $base; if ($bytes === 1 ? ($off < -128 || $off > 127) : ($off < -32768 || $off > 32767)) { $this->line = $line; $this->fail("jump to $label is too far for its size"); } $this->out = substr_replace($this->out, $bytes === 1 ? chr($off & 0xff) : pack('v', $off & 0xffff), $at, $bytes); } foreach (['main', 'before', 'after'] as $e) { if (isset($entries[$e])) { [$label, $line] = $entries[$e]; if (!isset($this->labels[$label])) { $this->line = $line; $this->fail("unknown label $label"); } $s->$e = $this->labels[$label]; } } $s->code = substr($this->out, 0x18); return $s; } private static function init(): void { if (self::$words) { return; } foreach (Grammar::OPCODES as $code => [$name]) { self::$words[Names::statement($name)] = $code; } foreach (['O' => Grammar::OPERS, 'S' => Grammar::STORES, 'A' => Grammar::ADDS] as $t => $table) { foreach ($table as $code => [$name]) { self::$codes[$t][$name] ??= $code; } } } // --- statements ------------------------------------------------------------------------------------------- private function statement(): void { $word = $this->t[0][1]; $isId = $this->t[0][0] === 'id'; // assignments: target = / += / -= expression $eq = $this->findTop(['=', '+=', '-=']); if ($eq !== null) { $op = $this->t[$eq][1]; $table = $op === '=' ? 'S' : 'A'; $target = $this->target($table); if ($this->k !== $eq) { $this->fail('unexpected ' . $this->t[$this->k][1] . ' in the target'); } $this->k++; $e = $this->expression(); $this->emit(chr($op === '=' ? 0x1e : ($op === '+=' ? 0x20 : 0x21))); $this->emitItem($e, 'O'); $this->emitItem($target, $table); return; } if (!$isId) { $this->fail("unexpected $word"); } $this->k = 1; [$base, $size, $pad] = $this->sizedWord($word); switch ($base) { case 'goto': $this->jump([8 => 0x08, 16 => 0x09, 24 => 0x0a], $size, $pad); return; case 'call': $this->jump([8 => 0x05, 16 => 0x06, 24 => 0x07], $size, $pad); return; case 'start': $this->jump([8 => 0x30, 16 => 0x31, 24 => 0x32], $size, $pad); return; case 'ifz': case 'ifnz': case 'ifeq': case 'ifne': $ops = ['ifz' => [0x12, 0x13, 0x14], 'ifnz' => [0x15, 0x16, 0x17], 'ifeq' => [0x18, 0x19, 0x1a], 'ifne' => [0x1b, 0x1c, 0x1d]][$base]; [$gword, $gsize, $gpad] = $this->sizedWord($this->ident()); if ($gword !== 'goto') { $this->fail("$base goto