From 1af7ef3005159125ac725b5df858b589a0564dd4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Mon, 6 Sep 2010 05:16:55 -0400 Subject: [PATCH] - Moved variable type detection (whether it is an array or object) to the compiler class rather than in the parser --- lib/Haanga/Compiler.php | 39 ++++++++++++++++++++++++++++++++++++--- lib/Haanga/Compiler/Parser.php | 2 +- lib/Haanga/Compiler/Parser.y | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/Haanga/Compiler.php b/lib/Haanga/Compiler.php index 56c740a..4e3e0fa 100644 --- a/lib/Haanga/Compiler.php +++ b/lib/Haanga/Compiler.php @@ -829,10 +829,22 @@ class Haanga_Compiler foreach ($variable as $id => $part) { if ($id != 0) { if (is_array($part) && isset($part['object'])) { + if (!isset($var->$part['object'])) { + $var = NULL; + break; + } $var = &$var->$part['object']; } else if (is_object($var)) { + if (!isset($var->$part)) { + $var = NULL; + break; + } $var = &$var->$part; } else { + if (!isset($var[$part])) { + $var = NULL; + break; + } $var = &$var[$part]; } } @@ -881,7 +893,8 @@ class Haanga_Compiler if (!$this->forid) { $this->Error("Invalid forloop reference outside of a loop"); } - switch ($variable[1]) { + + switch ($variable[1]['object']) { case 'counter': $this->forloop[$this->forid]['counter'] = TRUE; $variable = 'forcounter1_'.$this->forid; @@ -932,11 +945,31 @@ class Haanga_Compiler return Haanga_AST::str(self::$block_var); break; default: + /* choose array or objects */ + + for ($i=1; $i < count($variable); $i++) { + $var_part = array_slice($variable, 0, $i); + $def_arr = TRUE; + + if (is_array($variable[$i])) { + if (isset($variable[$i]['object'])) { + $def_arr = FALSE; + } + $variable[$i] = current($variable[$i]); + } + + $is_obj = $this->var_is_object($var_part, 'unknown'); + + if ( $is_obj === TRUE || ($is_obj == 'unknown' && !$def_arr)) { + $variable[$i] = array('object' => $variable[$i]); + } + } + break; } } else if (isset($this->var_alias[$variable])) { - $variable = $this->var_alias[$variable]; + $variable = $this->var_alias[$variable]['var']; } return hvar($variable)->getArray(); @@ -1270,7 +1303,7 @@ class Haanga_Compiler */ function generate_op_alias($details, &$body) { - $this->var_alias[ $details['as'] ] = $details['var']; + $this->var_alias[ $details['as'] ] = $this->generate_variable_name($details['var']); $this->generate_op_code($details['body'], $body); unset($this->var_alias[ $details['as'] ] ); } diff --git a/lib/Haanga/Compiler/Parser.php b/lib/Haanga/Compiler/Parser.php index e4f476e..1d86549 100644 --- a/lib/Haanga/Compiler/Parser.php +++ b/lib/Haanga/Compiler/Parser.php @@ -1851,7 +1851,7 @@ static public $yy_action = array( function yy_r74(){ if (!is_array($this->yystack[$this->yyidx + -2]->minor)) { $this->_retvalue = array($this->yystack[$this->yyidx + -2]->minor); } else { $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor; } $this->_retvalue[]=array('object' => $this->yystack[$this->yyidx + 0]->minor); } #line 1858 "lib/Haanga/Compiler/Parser.php" #line 357 "lib/Haanga/Compiler/Parser.y" - function yy_r75(){ if (!is_array($this->yystack[$this->yyidx + -2]->minor)) { $this->_retvalue = array($this->yystack[$this->yyidx + -2]->minor); } else { $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor; } $this->_retvalue[] = ($this->compiler->var_is_object($this->_retvalue)) ? array('object' => $this->yystack[$this->yyidx + 0]->minor) : $this->yystack[$this->yyidx + 0]->minor; } + function yy_r75(){ if (!is_array($this->yystack[$this->yyidx + -2]->minor)) { $this->_retvalue = array($this->yystack[$this->yyidx + -2]->minor); } else { $this->_retvalue = $this->yystack[$this->yyidx + -2]->minor; } $this->_retvalue[] = array('object' => $this->yystack[$this->yyidx + 0]->minor); } #line 1861 "lib/Haanga/Compiler/Parser.php" #line 358 "lib/Haanga/Compiler/Parser.y" function yy_r76(){ if (!is_array($this->yystack[$this->yyidx + -3]->minor)) { $this->_retvalue = array($this->yystack[$this->yyidx + -3]->minor); } else { $this->_retvalue = $this->yystack[$this->yyidx + -3]->minor; } $this->_retvalue[]=$this->yystack[$this->yyidx + -1]->minor; } diff --git a/lib/Haanga/Compiler/Parser.y b/lib/Haanga/Compiler/Parser.y index b6035e9..bc0fe88 100644 --- a/lib/Haanga/Compiler/Parser.y +++ b/lib/Haanga/Compiler/Parser.y @@ -359,7 +359,7 @@ expr(A) ::= fvar_or_string(B). { A = B; } /* Variable name */ varname(A) ::= varname(B) T_OBJ T_ALPHA(C). { if (!is_array(B)) { A = array(B); } else { A = B; } A[]=array('object' => C);} -varname(A) ::= varname(B) T_DOT T_ALPHA(C). { if (!is_array(B)) { A = array(B); } else { A = B; } A[] = ($this->compiler->var_is_object(A)) ? array('object' => C) : C;} +varname(A) ::= varname(B) T_DOT T_ALPHA(C). { if (!is_array(B)) { A = array(B); } else { A = B; } A[] = array('object' => C);} varname(A) ::= varname(B) T_BRACKETS_OPEN var_or_string(C) T_BRACKETS_CLOSE. { if (!is_array(B)) { A = array(B); } else { A = B; } A[]=C;} varname(A) ::= T_ALPHA(B). { A = B; } /* T_BLOCK|T_CUSTOM|T_CUSTOM_BLOCK are also T_ALPHA */ -- 2.11.4.GIT