-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathemulator-variables.php
More file actions
139 lines (137 loc) · 3.17 KB
/
emulator-variables.php
File metadata and controls
139 lines (137 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
use PhpParser\Node;
trait EmulatorVariables
{
/**
* Symbol table of the current scope (all variables)
* @var array
*/
public $variables=[];
/**
* Function used to return something when reference returning
* functions fail and have to return something.
* Can set an input variable to null for ease too.
* @var null
*/
protected function &null_reference(&$var=null)
{
$var=null;
// unset($this->null_reference);
$this->null_reference=null;
return $this->null_reference;
}
/**
* Set a value to a variable
* creates if not exists
* @param Node $node
* @param mixed $value
* @return mixed or null
*/
function variable_set($node,$value=null)
{
$r=&$this->symbol_table($node,$key,true);
if ($key!==null)
return $r[$key]=$value;
else
return null;
}
function variable_set_byref($node,&$ref)
{
$r=&$this->symbol_table($node,$key,true);
if ($key!==null)
return $r[$key]=&$ref;
else
return null;
}
/**
* Get the value of a variable
* @param Node $node
* @return mixed
*/
function variable_get($node)
{
$r=&$this->symbol_table($node,$key,false);
if ($key!==null)
if (is_string($r))
return $r[$key];
elseif (is_null($r)) //any access on null is null [https://bugs.php.net/bug.php?id=72786]
return null;
elseif (is_array($r)) //support for iterable objects
{
if (!array_key_exists($key, $r)) //only works for arrays, not strings
{
$this->notice("Undefined index: {$key}");
return null;
}
return $r[$key];
}
// elseif (is_object($r))
// {
// if ($r instanceof ArrayAccess)
// return $r[$key];
// else
// {
// if ($r instanceof EmulatorObject)
// $type=$r->classname;
// else
// $type=get_class($r);
// $this->error("Cannot use object of type {$type} as array");
// return null;
// }
// }
else
{
$this->warning("Using unknown type as array");
return $r[$key];
}
return null;
}
/**
* Check whether or not a variable exists
* @param Node $node
* @return bool
*/
function variable_isset($node)
{
$this->error_silence();
$r=$this->symbol_table($node,$key,false);
$this->error_restore();
return $key!==null and isset($r[$key]);
}
/**
* Deletes a variable
* @param Node $node
*/
function variable_unset($node)
{
$base=&$this->symbol_table($node,$key,false);
if ($key!==null)
unset($base[$key]);
}
/**
* Returns reference to a variable
* minimize uses of this, as it's very hard to behave properly when a variable does not exist
* @param Node $node
* @return reference
*/
function &variable_reference($node,&$success=null)
{
$r=&$this->symbol_table($node,$key,false); //this should NOT always create, e.g. static property fetch
//in fact it should never create, anywhere its needed, it is explicitly created by variable_set
if ($key===null) //not found or GLOBALS
{
$success=false;
return $this->null_reference();
}
elseif (is_array($r))
{
$success=true;
return $r[$key]; //if $r[$key] does not exist, will be created in byref use.
}
else
{
$success=false;
$this->error("Could not retrieve reference",$node);
}
}
}