| 1 | <?php |
| 2 | |
| 3 | require_once('Destructable.php'); |
| 4 | |
| 5 | class JavaScriptLiteral extends Destructable { |
| 6 | protected $value; |
| 7 | |
| 8 | public function __construct($value) { |
| 9 | $this->value = $value; |
| 10 | } |
| 11 | |
| 12 | public function __destruct() { |
| 13 | $this->mem_flush('value'); |
| 14 | } |
| 15 | |
| 16 | public function value() { |
| 17 | return $this->value; |
| 18 | } |
| 19 | |
| 20 | public function type() { |
| 21 | if (is_object($this->value)) { |
| 22 | if ($this->value->arity == 'this') { |
| 23 | return 'Object'; |
| 24 | } |
| 25 | elseif (strlen($this->value->id) > 1 && in_array($this->value->id{0}, array('!', '=', '<', '>'))) { |
| 26 | return 'bool'; |
| 27 | } |
| 28 | elseif (in_array($this->value->id, array('!', '>', '<', '&', '|'))) { |
| 29 | return 'bool'; |
| 30 | } |
| 31 | elseif ($this->value->id == '+' && !is_numeric($this->value->second->value)) { |
| 32 | return 'String'; |
| 33 | } |
| 34 | elseif (in_array($this->value->id, array('++', '--', '+', '-', '*', '/', '%'))) { |
| 35 | return 'Number'; |
| 36 | } |
| 37 | elseif ($this->value->id == '&&' || $this->value->id == '||') { |
| 38 | $first = $this->value->first; |
| 39 | if (is_array($first)) { |
| 40 | $first = $first[0]; |
| 41 | } |
| 42 | return $first->type(); |
| 43 | } |
| 44 | } |
| 45 | elseif (is_null($this->value) || $this->value == 'null') { |
| 46 | return 'Object'; |
| 47 | } |
| 48 | elseif (is_bool($this->value)) { |
| 49 | return 'bool'; |
| 50 | } |
| 51 | elseif ($this->value == 'arguments') { |
| 52 | return 'Array'; |
| 53 | } |
| 54 | throw new Exception("Unstringed literal type: {$this->value}"); |
| 55 | } |
| 56 | } |