1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 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\Definition as Def; |
14
|
|
|
|
15
|
|
|
abstract class Variable extends Def\Definition { |
16
|
|
|
// TODO: Use these in Graph/IndexDB. |
17
|
|
|
const CLASS_TYPE = "class"; |
18
|
|
|
const FILE_TYPE = "file"; |
19
|
|
|
const GLOBAL_TYPE = "global"; |
20
|
|
|
const FUNCTION_TYPE = "function"; |
21
|
|
|
const METHOD_TYPE = "method"; |
22
|
|
|
const LANGUAGE_CONSTRUCT_TYPE = "language construct"; |
23
|
|
|
|
24
|
|
|
static public function is_type($t) { |
|
|
|
|
25
|
|
|
static $types = array |
26
|
|
|
( "class" |
27
|
|
|
, "file" |
28
|
|
|
, "global" |
29
|
|
|
, "function" |
30
|
|
|
, "method" |
31
|
|
|
, "language construct" |
32
|
|
|
); |
33
|
|
|
return in_array($t, $types); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
private $name; |
40
|
|
|
|
41
|
90 |
|
public function __construct($name = null) { |
42
|
90 |
|
assert('is_string($name) || ($name === null)'); |
43
|
90 |
|
$this->name = $name; |
44
|
90 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
183 |
|
public function name() { |
50
|
183 |
|
return $this->name; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
32 |
|
public function withName($name) { |
58
|
32 |
|
assert('is_string($name)'); |
59
|
32 |
|
$clone = clone $this; |
60
|
32 |
|
$clone->name = $name; |
61
|
32 |
|
return $clone; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the meaning of the variable. |
66
|
|
|
* |
67
|
|
|
* In opposite to name, this gives insight in the structure of this variable. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
abstract public function meaning(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Compile the variable to a condition on a graph node. |
75
|
|
|
* |
76
|
|
|
* TODO: Maybe negate can go away. |
77
|
|
|
* |
78
|
|
|
* @param bool $negate |
79
|
|
|
* @return \Closure Node -> bool |
80
|
|
|
*/ |
81
|
|
|
abstract public function compile($negate = false); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|