JFIF  x x C         C     "        } !1AQa "q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz        w !1AQ aq"2B #3Rbr{ gilour

File "Compiler.php"

Full Path: /home/palsarh/web/palsarh.in/public_html/vendor/cuyz/valinor/src/Compiler/Compiler.php
File size: 1.09 KB
MIME-type: text/x-php
Charset: utf-8

<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Compiler;

use function str_repeat;
use function str_replace;

/** @internal */
final class Compiler
{
    private string $code = '';

    /** @var non-negative-int */
    private int $indentation = 0;

    public function compile(Node ...$nodes): self
    {
        $compiler = $this;

        while ($current = array_shift($nodes)) {
            $compiler = $current->compile($compiler);

            if ($nodes !== []) {
                $compiler = $compiler->write("\n");
            }
        }

        return $compiler;
    }

    public function sub(): self
    {
        return new self();
    }

    public function write(string $code): self
    {
        $self = clone $this;
        $self->code .= $code;

        return $self;
    }

    public function indent(): self
    {
        $self = clone $this;
        $self->indentation++;

        return $self;
    }

    public function code(): string
    {
        $indent = str_repeat('    ', $this->indentation);

        return  $indent . str_replace("\n", "\n" . $indent, $this->code);
    }
}