1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Variables; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Graph\Node; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A variable with a certain property. |
17
|
|
|
*/ |
18
|
|
|
class WithProperty extends Variable { |
19
|
|
|
/** |
20
|
|
|
* @var Variable |
21
|
|
|
*/ |
22
|
|
|
private $other; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Property |
26
|
|
|
*/ |
27
|
|
|
private $property; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $arguments; |
33
|
|
|
|
34
|
29 |
|
public function __construct(Variable $other, Property $property, array $arguments) { |
35
|
29 |
|
parent::__construct(); |
36
|
29 |
|
assert('$property->arguments_are_valid($arguments)'); |
37
|
29 |
|
$this->other = $other; |
38
|
29 |
|
$this->property = $property; |
39
|
29 |
|
$this->arguments = $arguments; |
40
|
29 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Variable |
44
|
|
|
*/ |
45
|
14 |
|
public function variable() { |
46
|
14 |
|
return $this->other; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdocs |
51
|
|
|
*/ |
52
|
14 |
|
public function meaning() { |
53
|
14 |
|
return $this->variable()->meaning()." ".$this->property->parse_as().": ".$this->argument_list(); |
54
|
|
|
} |
55
|
|
|
|
56
|
14 |
|
protected function argument_list() { |
57
|
14 |
|
$args = array(); |
58
|
14 |
|
foreach ($this->arguments as $argument) { |
59
|
14 |
|
if (is_string($argument)) { |
60
|
12 |
|
$args[] = "\"$argument\""; |
61
|
12 |
|
} |
62
|
2 |
|
else if ($argument instanceof Variable) { |
63
|
2 |
|
$name = $argument->name(); |
64
|
2 |
|
if ($name !== null) { |
65
|
2 |
|
$args[] = $name; |
66
|
2 |
|
} |
67
|
|
|
else { |
68
|
|
|
$args[] = "(".$argument->meaning().")"; |
69
|
|
|
} |
70
|
2 |
|
} |
71
|
|
|
else { |
72
|
|
|
throw new \LogicException("Unknown arg type: ".gettype($argument)); |
73
|
|
|
} |
74
|
14 |
|
} |
75
|
14 |
|
return implode(", ", $args); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdocs |
80
|
|
|
*/ |
81
|
23 |
|
public function compile() { |
82
|
23 |
|
$left_condition = $this->other->compile(); |
83
|
23 |
|
$property_condition = $this->property->compile($this->arguments); |
84
|
|
|
|
85
|
23 |
|
return function(Node $n) use ($left_condition, $property_condition) { |
86
|
23 |
|
return $left_condition($n) |
87
|
23 |
|
&& $property_condition($n); |
88
|
23 |
|
}; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|