forked from iangreenleaf/Scisr
/
VariableTypes.php
175 lines (158 loc) · 6.8 KB
/
VariableTypes.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Stores information about variable types
*
* Basically a very rudimentary static model
*/
class Scisr_Db_VariableTypes
{
/**
* Do any necessary setup
*
* Sets up the DB table we are going to use
*/
public static function init()
{
$db = Scisr_Db::getDb();
$create = <<<EOS
CREATE TABLE IF NOT EXISTS VariableTypes(filename text, scopeopen integer, variable text, type text, variable_pointer integer);
CREATE INDEX IF NOT EXISTS VariableTypes_index_filename ON VariableTypes (filename);
CREATE UNIQUE INDEX IF NOT EXISTS VariableTypes_index_filename_var_ptr ON VariableTypes (filename, variable_pointer);
CREATE INDEX IF NOT EXISTS VariableTypes_index_filename_var_scope ON VariableTypes (filename, scopeopen, variable);
EOS;
$db->exec($create);
$create = <<<EOS
CREATE TABLE GlobalVariables(filename text, scopeopen integer, variable text, variable_pointer integer);
CREATE INDEX IF NOT EXISTS GlobalVariables_index_filename ON GlobalVariables (filename);
CREATE UNIQUE INDEX IF NOT EXISTS GlobalVariables_index_filename_var_ptr ON GlobalVariables (filename, variable_pointer);
CREATE INDEX IF NOT EXISTS GlobalVariables_index_filename_var_scope ON GlobalVariables (filename, scopeopen, variable);
EOS;
$db->exec($create);
}
/**
* Register a variable as holding a certain class type
* @param string $variable the name of the variable (including the dollar sign)
* @param string $type the name of the class that this variable holds
* @param string $filename the file we're in
* @param array $scopeOpen the stack pointer to the beginning of the current scope
* @param int $varPtr a pointer to the beginning of the variable
*/
public static function registerVariableType($variable, $type, $filename, $scopeOpen, $varPtr)
{
$db = Scisr_Db::getDb();
// First delete any previous assignments in this scope
$delete = <<<EOS
DELETE FROM VariableTypes WHERE filename = ? AND variable_pointer = ?
EOS;
$delSt = $db->prepare($delete);
$delSt->execute(array($filename, $varPtr));
// Update any partially typed entries
$select = <<<EOS
SELECT variable, variable_pointer FROM VariableTypes WHERE variable LIKE ? AND filename = ? AND scopeopen = ?
EOS;
$querySt = $db->prepare($select);
$querySt->execute(array($variable . '->_%', $filename, $scopeOpen));
while (($result = $querySt->fetch()) !== false) {
$varPtr = $result['variable_pointer'];
$varName = $result['variable'];
$newVarName = substr_replace($varName, $type, 0, strlen($variable));
$update = <<<EOS
UPDATE VariableTypes SET variable = ? WHERE filename = ? AND variable_pointer = ?
EOS;
$upSt = $db->prepare($update);
$upSt->execute(array($newVarName, $filename, $varPtr));
}
// Now insert this assignment
$insert = <<<EOS
INSERT INTO VariableTypes (filename, scopeopen, variable, type, variable_pointer) VALUES (?, ?, ?, ?, ?)
EOS;
$insSt = $db->prepare($insert);
$insSt->execute(array($filename, $scopeOpen, $variable, $type, $varPtr));
}
/**
* Register a variable as global
* @param string $variable the name of the variable (including the dollar sign)
* @param string $filename the file we're in
* @param array $scopeOpen the stack pointer to the beginning of the current scope
* @param int $varPtr a pointer to the beginning of the variable
*/
public static function registerGlobalVariable($variable, $filename, $scopeOpen, $varPtr)
{
$db = Scisr_Db::getDb();
// Now insert this assignment
$insert = <<<EOS
INSERT INTO GlobalVariables (filename, scopeopen, variable, variable_pointer) VALUES (?, ?, ?, ?)
EOS;
$insSt = $db->prepare($insert);
$insSt->execute(array($filename, $scopeOpen, $variable, $varPtr));
}
/**
* See if a type has already been registered for this particular place in
* the stack.
* @param string $filename the file we're in
* @param int $varPtr a pointer to the beginning of the variable
* @return string|null the type name registered at this location, or null if none
*/
public static function checkVariableDefinition($filename, $varPtr)
{
$db = Scisr_Db::getDb();
$sql = <<<EOS
SELECT type FROM VariableTypes WHERE filename = ? AND variable_pointer = ?
EOS;
$st = $db->prepare($sql);
$st->execute(array($filename, $varPtr));
$result = $st->fetch();
if ($result === false) {
return null;
} else {
return $result['type'];
}
}
/**
* Get the type of a variable
* @param string $variable the name of the variable (including the dollar sign)
* @param string $filename the file we're in
* @param array $scopeOpen the stack pointer to the beginning of the current scope
* @param int $varPtr a pointer to the beginning of the variable
* @return string|null the class name, or null if we don't know
*/
public static function getVariableType($variable, $filename, $scopeOpen, $varPtr)
{
$db = Scisr_Db::getDb();
$select = <<<EOS
SELECT type FROM VariableTypes WHERE filename = ? AND variable = ? AND scopeopen = ? AND variable_pointer <= ? ORDER BY variable_pointer DESC LIMIT 1
EOS;
$st = $db->prepare($select);
$st->execute(array($filename, $variable, $scopeOpen, $varPtr));
$result = $st->fetch();
// If nothing was found, we'll settle for a type found after this point in the file
if ($result === false) {
$select = <<<EOS
SELECT type FROM VariableTypes WHERE filename = ? AND variable = ? AND scopeopen = ? AND variable_pointer > ? ORDER BY variable_pointer DESC LIMIT 1
EOS;
$st = $db->prepare($select);
$st->execute(array($filename, $variable, $scopeOpen, $varPtr));
$result = $st->fetch();
}
return $result['type'];
}
/**
* See if a variable is in the global scope
* @param string $variable the name of the variable (including the dollar sign)
* @param string $filename the file we're in
* @param array $scopeOpen the stack pointer to the beginning of the current scope
* @param int $varPtr a pointer to the beginning of the variable
* @return boolean true if it is global
*/
public static function isGlobalVariable($variable, $filename, $scopeOpen, $varPtr)
{
$db = Scisr_Db::getDb();
$select = <<<EOS
SELECT COUNT(*) FROM GlobalVariables WHERE filename = ? AND variable = ? AND scopeopen = ? LIMIT 1
EOS;
$st = $db->prepare($select);
$st->execute(array($filename, $variable, $scopeOpen));
$result = $st->fetch();
return $result['COUNT(*)'] > 0;
}
}