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
|
<?php
namespace kch42\ste;
class Scope implements \ArrayAccess {
private $parent = NULL;
private $vars = array();
private static function parse_name($name) {
$remain = $name;
$fields = array();
while($remain !== "") {
$br_open = strpos($remain, '[');
if($br_open === false) {
$fields[] = $remain;
break;
}
$br_close = strpos($remain, ']', $br_open);
if($br_close === false) {
throw new RuntimeError("Invalid varname \"$name\". Missing closing \"]\".");
}
$fields[] = substr($remain, 0, $br_open);
$field = substr($remain, $br_open+1, $br_close-$br_open-1);
$more = substr($remain, $br_close+1);
if(strpos($field, '[') !== false) {
throw new RuntimeError("A variable field must not contain a '[' character.");
}
if((strlen($more) > 0) && ($more[0] !== '[')) {
// TODO: better error message, not very non-programmer friendly...
throw new RuntimeError("A variable name must be of format name('[' name ']')*.");
}
$remain = $field . $more;
}
return $fields;
}
private function &get_topvar_reference($name, $localonly) {
if(array_key_exists($name, $this->vars)) {
$ref = &$this->vars[$name];
return $ref;
}
if((!$localonly) && ($this->parent !== NULL)) {
$ref = &$this->parent->get_topvar_reference($name, $localonly);
return $ref;
}
throw new VarNotInScope();
}
public function &get_var_reference($name, $create_if_not_exist, $localonly=false) {
$nullref = NULL;
$fields = self::parse_name($name);
if(count($fields) == 0) {
return $nullref; // TODO: or should we throw an exception here?
}
$first = $fields[0];
$ref = NULL;
try {
$ref = &$this->get_topvar_reference($first, $localonly);
} catch(VarNotInScope $e) {
if($create_if_not_exist) {
$this->vars[$first] = (count($fields) > 0) ? array() : "";
$ref = &$this->vars[$first];
} else {
return $nullref;
}
}
for($i = 1; $i < count($fields); $i++) {
$field = $fields[$i];
if(!is_array($ref)) {
return $nullref;
}
if(!array_key_exists($field, $ref)) {
if(!$create_if_not_exist) {
return $nullref;
}
if($i < count($fields) - 1) {
$ref[$field] = array();
} else {
$ref[$field] = "";
}
}
$ref = &$ref[$field];
}
return $ref;
}
public function set_var_by_name($name, $val) {
$ref = &$this->get_var_reference($name, true);
$ref = $val;
}
public function set_local_var($name, $val) {
$ref = &$this->get_var_reference($name, true, true);
$ref = $val;
}
public function get_var_by_name($name) {
$ref = $this->get_var_reference($name, false);
return $ref === NULL ? "" : $ref;
}
public function new_subscope() {
$o = new self();
$o->parent = $this;
return $o;
}
/* implementing ArrayAccess */
public function offsetSet($offset, $value) {
$this->set_var_by_name($offset, $value);
}
public function offsetGet($offset) {
return $this->get_var_by_name($offset);
}
public function offsetExists($offset) {
try {
$this->get_topvar_reference($offset);
return true;
} catch(VarNotInScope $e) {
return false;
}
}
public function offsetUnset($offset) {
unset($this->vars[$offset]);
}
}
|