Passed
Push — master ( 633b25...e5131a )
by Alexander
01:44 queued 10s
created

Interpreter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 51
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A evaluate() 0 8 1
A interpr() 0 4 1
1
<?php
2
3
4
namespace App;
5
6
7
class Interpreter
8
{
9
    /**
10
     * @var Parser
11
     */
12
    private $parser;
13
    /**
14
     * @var Visitor
15
     */
16
    private $visitor;
17
18
    /**
19
     * Interpreter constructor.
20
     * @param Parser $parser
21
     * @param Visitor $visitor
22
     */
23
    public function __construct(
24
        Parser $parser,
25
        Visitor $visitor
26
    )
27
    {
28
        $this->parser  = $parser;
29
        $this->visitor = $visitor;
30
    }
31
32
    /**
33
     * @param string $input
34
     * @return bool|float|int|string|null
35
     * @throws Exception\LexerException
36
     * @throws Exception\UnknownIdentifier
37
     */
38
    public static function evaluate(string $input)
39
    {
40
        $lexer       = new Lexer($input);
41
        $parser      = new Parser($lexer);
42
        $visitor     = new Visitor();
43
        $interpreter = new static($parser, $visitor);
44
45
        return $interpreter->interpr();
46
    }
47
48
    /**
49
     * @return bool|float|int|string|null
50
     * @throws Exception\LexerException
51
     * @throws Exception\ParseError
52
     * @throws Exception\UnknownIdentifier
53
     */
54
    public function interpr()
55
    {
56
        $ast = $this->parser->parse();
57
        return $this->visitor->visit($ast);
58
    }
59
}