summaryrefslogtreecommitdiff
path: root/src/ste/Scope.php
blob: dad1c423b6823d3155d843ac78588cafb548a6b0 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

namespace r7r\ste;

class Scope implements \ArrayAccess
{
    /** @var self|null */
    private $parent = null;

    /** @var array */
    public $vars = [];

    /**
     * @param string $name
     * @return string[]
     * @throws RuntimeError
     */
    private static function parse_name(string $name): array
    {
        $remain = $name;
        $fields = [];

        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;
    }

    /**
     * @param string $name
     * @param bool $localonly
     * @return mixed A reference to the resolved variable
     * @throws VarNotInScope
     */
    private function &get_topvar_reference(string $name, bool $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();
    }

    /**
     * @param string $name
     * @param bool $create_if_not_exist
     * @param bool $localonly
     * @return mixed A reference to the resolved variable
     * @throws RuntimeError
     */
    public function &get_var_reference(string $name, bool $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) ? [] : "";
                $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] = [];
                } else {
                    $ref[$field] = "";
                }
            }

            $ref = &$ref[$field];
        }

        return $ref;
    }

    /**
     * @param string $name
     * @param mixed $val
     * @throws RuntimeError
     */
    public function set_var_by_name(string $name, $val): void
    {
        $ref = &$this->get_var_reference($name, true);
        $ref = $val;
    }

    /**
     * @param string $name
     * @param mixed $val
     * @throws RuntimeError
     */
    public function set_local_var(string $name, $val): void
    {
        $ref = &$this->get_var_reference($name, true, true);
        $ref = $val;
    }

    /**
     * @param string $name
     * @return mixed Returns an empty string, if not found or unset
     * @throws RuntimeError
     */
    public function get_var_by_name(string $name)
    {
        $ref = $this->get_var_reference($name, false);
        return $ref === null ? "" : $ref;
    }

    /**
     * @return self
     */
    public function new_subscope(): self
    {
        $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, false);
            return true;
        } catch (VarNotInScope $e) {
            return false;
        }
    }
    public function offsetUnset($offset)
    {
        unset($this->vars[$offset]);
    }
}