ES
games/alis / src / Alis / Lang / Forms.php · commit 9c722cf
<?phpnamespace App\Alis\Lang;/** * The three forms of an ALIS source (docs/language.md): the machine's words (canonical, Printer / Assembler), the * "90" form (Human: French words) and ALIS BASIC (Basic). All three compile to the same bytes. */final class Forms{    public const MACHINE = 'machine';    public const NINETIES = '90';    public const BASIC = 'basic';    /** F2 in the editor: the next form */    public const NEXT = [self::NINETIES => self::BASIC, self::BASIC => self::MACHINE, self::MACHINE => self::NINETIES];    public const TITLES = [self::NINETIES => 'format 90', self::BASIC => 'ALIS BASIC', self::MACHINE => 'format machine'];    public static function of(string $source): string    {        return match (true) {            Basic::isBasic($source) => self::BASIC,            Human::isHuman($source) => self::NINETIES,            default => self::MACHINE,        };    }    /** The canonical text of a source in any form. */    public static function canonical(string $source): string    {        return match (self::of($source)) {            self::BASIC => Basic::toCanonical($source),            self::NINETIES => Human::toCanonical($source),            default => $source,        };    }    /** A source in any form, written in $form. */    public static function to(string $form, string $source): string    {        if (self::of($source) === $form) {            return $source;        }        $canonical = self::canonical($source);        return match ($form) {            self::BASIC => Basic::toBasic($canonical),            self::NINETIES => Human::toHuman($canonical),            default => $canonical,        };    }}