Interpreter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createParser() 0 9 1
A exec() 0 9 1
A getOutput() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\UniLex\Example\Brainfuck;
6
7
use Remorhaz\UniLex\Example\Brainfuck\Grammar\TokenMatcher;
8
use Remorhaz\UniLex\Example\Brainfuck\Grammar\TranslationScheme;
9
use Remorhaz\UniLex\Grammar\ContextFree\GrammarLoader;
10
use Remorhaz\UniLex\Grammar\ContextFree\TokenFactory;
11
use Remorhaz\UniLex\Parser\LL1\Parser;
12
use Remorhaz\UniLex\Parser\LL1\TranslationSchemeApplier;
13
use Remorhaz\UniLex\Lexer\TokenReader;
14
use Remorhaz\UniLex\Unicode\CharBufferFactory;
15
16
class Interpreter
17
{
18
    private $output;
19
20
21
    /**
22
     * @param string $text
23
     * @throws Exception
24
     * @throws \Remorhaz\UniLex\Exception
25
     */
26
    public function exec(string $text): void
27
    {
28
        unset($this->output);
29
        $runtime = new Runtime();
30
        $this
31
            ->createParser($text, $runtime)
32
            ->run();
33
        $runtime->exec();
34
        $this->output = $runtime->getOutput();
35
    }
36
37
    /**
38
     * @return string
39
     * @throws Exception
40
     */
41
    public function getOutput(): string
42
    {
43
        if (!isset($this->output)) {
44
            throw new Exception("Output is not defined");
45
        }
46
        return $this->output;
47
    }
48
49
    /**
50
     * @param string $text
51
     * @param Runtime $runtime
52
     * @return Parser
53
     * @throws \Remorhaz\UniLex\Exception
54
     */
55
    private function createParser(string $text, Runtime $runtime): Parser
56
    {
57
        $buffer = CharBufferFactory::createFromString($text);
58
        $grammar = GrammarLoader::loadFile(__DIR__ . "/Grammar/Config.php");
59
        $tokenReader = new TokenReader($buffer, new TokenMatcher(), new TokenFactory($grammar));
60
        $translator = new TranslationSchemeApplier(new TranslationScheme($runtime));
61
        $parser = new Parser($grammar, $tokenReader, $translator);
62
        $parser->loadLookupTable(__DIR__ . "/Grammar/LookupTable.php");
63
        return $parser;
64
    }
65
}
66