Hoa central
Interpreter.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Ruler\Visitor;
38 
39 use Hoa\Ruler;
40 use Hoa\Visitor;
41 
50 class Interpreter implements Visitor\Visit
51 {
57  protected $_root = null;
58 
64  protected $_current = null;
65 
66 
67 
77  public function visit(Visitor\Element $element, &$handle = null, $eldnah = null)
78  {
79  $id = $element->getId();
80  $variable = false !== $eldnah;
81 
82  switch ($id) {
83  case '#expression':
84  $this->_root = new Ruler\Model();
85  $this->_root->expression = $element->getChild(0)->accept(
86  $this,
87  $handle,
88  $eldnah
89  );
90 
91  return $this->_root;
92 
93  case '#operation':
94  $children = $element->getChildren();
95  $left = $children[0]->accept($this, $handle, $eldnah);
96  $right = $children[2]->accept($this, $handle, $eldnah);
97  $name = $children[1]->accept($this, $handle, false);
98 
99  return $this->_root->_operator(
100  $name,
101  [$left, $right],
102  false
103  );
104 
105  case '#variable_access':
106  $children = $element->getChildren();
107  $name = $children[0]->accept($this, $handle, $eldnah);
108  array_shift($children);
109 
110  foreach ($children as $child) {
111  $_child = $child->accept($this, $handle, $eldnah);
112 
113  switch ($child->getId()) {
114  case '#array_access':
115  $name->index($_child);
116 
117  break;
118 
119  case '#attribute_access':
120  $name->attribute($_child);
121 
122  break;
123 
124  case '#method_access':
125  $name->call($_child);
126 
127  break;
128  }
129  }
130 
131  return $name;
132 
133  case '#array_access':
134  return $element->getChild(0)->accept($this, $handle, $eldnah);
135 
136  case '#attribute_access':
137  return $element->getChild(0)->accept($this, $handle, false);
138 
139  case '#method_access':
140  return $element->getChild(0)->accept($this, $handle, $eldnah);
141 
142  case '#array_declaration':
143  $out = [];
144 
145  foreach ($element->getChildren() as $child) {
146  $out[] = $child->accept($this, $handle, $eldnah);
147  }
148 
149  return $out;
150 
151  case '#function_call':
152  $children = $element->getChildren();
153  $name = $children[0]->accept($this, $handle, false);
154  array_shift($children);
155 
156  $arguments = [];
157 
158  foreach ($children as $child) {
159  $arguments[] = $child->accept($this, $handle, $eldnah);
160  }
161 
162  return $this->_root->_operator(
163  $name,
164  $arguments,
165  true
166  );
167 
168  case '#and':
169  case '#or':
170  case '#xor':
171  $name = substr($id, 1);
172  $children = $element->getChildren();
173  $left = $children[0]->accept($this, $handle, $eldnah);
174  $right = $children[1]->accept($this, $handle, $eldnah);
175 
176  return $this->_root->operation(
177  $name,
178  [$left, $right]
179  );
180 
181  case '#not':
182  return $this->_root->operation(
183  'not',
184  [$element->getChild(0)->accept($this, $handle, $eldnah)]
185  );
186 
187  case 'token':
188  $token = $element->getValueToken();
189  $value = $element->getValueValue();
190 
191  switch ($token) {
192  case 'identifier':
193  return
194  true === $variable
195  ? $this->_root->variable($value)
196  : $value;
197 
198  case 'true':
199  return true;
200 
201  case 'false':
202  return false;
203 
204  case 'null':
205  return null;
206 
207  case 'float':
208  return floatval($value);
209 
210  case 'integer':
211  return intval($value);
212 
213  case 'string':
214  return str_replace(
215  '\\' . $value[0],
216  $value[0],
217  substr($value, 1, -1)
218  );
219 
220  default:
221  throw new Ruler\Exception\Interpreter(
222  'Token %s is unknown.',
223  0,
224  $token
225  );
226  }
227 
228  break;
229 
230  default:
231  throw new Ruler\Exception\Interpreter(
232  'Element %s is unknown.',
233  1,
234  $id
235  );
236  }
237 
238  return;
239  }
240 
246  public function getRoot()
247  {
248  return $this->_root;
249  }
250 }
visit(Visitor\Element $element, &$handle=null, $eldnah=null)
Definition: Interpreter.php:77