src/Evaluate/Expression.php
/**
	 * Evaluates the expression and returns the result.
	 * If an error occurs, prints a warning and returns false.
	 * If $expr is a function assignment, returns true on success.
	 *
	 * @param $expr
	 *
	 * @throws ReflectionException
	 * @return bool|mixed|null
	 */
Evaluates the expression and returns the result.
If an error occurs, prints a warning and returns false.
If $expr is a function assignment, returns true on success.

@param $expr

@throws ReflectionException
@return bool|mixed|null
Evaluates the expression and returns the result . If an error occurs prints a warning and returns false . If $expr is a function assignment returns true on success . @param $expr @throws ReflectionException @return bool|mixed|null
public function evaluate( $expr ) {
		$this->last_error = null;
		$expr = trim( $expr );
		if ( substr( $expr, - 1, 1 ) == ';' ) {
			$expr = substr( $expr, 0, strlen( $expr ) - 1 );
		}
				if ( preg_match( '/^\s*([a-z]\w*)\s*=(?!~|=)\s*(.+)$/', $expr, $matches ) ) {
			if ( in_array( $matches[1], $this->vb ) ) { 
				return $this->trigger( "Cannot assign to constant '{$matches[1]}'." );
			}
			$tmp                    = $this->pfx( $this->nfx( $matches[2] ) );
			$this->v[ $matches[1] ] = $tmp; 
			return $this->v[ $matches[1] ]; 
					} elseif ( preg_match( '/^\s*([a-z]\w*)\s*\((?:\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*)?\)\s*=(?!~|=)\s*(.+)$/', $expr, $matches ) ) {
			$fnn = $matches[1];
			if ( in_array( $matches[1], $this->fb ) ) {
				return $this->trigger( "Cannot redefine built-in function '{$matches[1]}()'." );
			}
			if ( $matches[2] != "" ) {
				$args = explode( ",", preg_replace( "/\s+/", "", $matches[2] ) ); 
			} else {
				$args = array();
			}
			if ( ( $stack = $this->nfx( $matches[3] ) ) === false ) {
				return false;
			}
			for ( $i = 0; $i < count( $stack ); $i ++ ) {
				$token = $stack[ $i ];
				if ( preg_match( '/^[a-z]\w*$/', $token ) and ! in_array( $token, $args ) ) {
					if ( array_key_exists( $token, $this->v ) ) {
						$stack[ $i ] = $this->v[ $token ];
					} else {
						return $this->trigger( "Undefined variable '{$token}' in function definition." );
					}
				}
			}
			$this->f[ $fnn ] = array( 'args' => $args, 'func' => $stack );
			return true;
		} else {
			return $this->pfx( $this->nfx( $expr ) );
		}
	}
public function evaluate ( $ expr ) { $ this -> last_error = null ; $ expr = trim ( $ expr ) ; if ( substr ( $ expr , - 1 , 1 ) == ';' ) { $ expr = substr ( $ expr , 0 , strlen ( $ expr ) - 1 ) ; } if ( preg_match ( '/^\s*([a-z]\w*)\s*=(?!~|=)\s*(.+)$/' , $ expr , $ matches ) ) { if ( in_array ( $ matches [ 1 ] , $ this -> vb ) ) { return $ this -> trigger ( " Cannot assign to constant ' { $ matches [ 1 ] } '. " ) ; } $ tmp = $ this -> pfx ( $ this -> nfx ( $ matches [ 2 ] ) ) ; $ this -> v [ $ matches [ 1 ] ] = $ tmp ; return $ this -> v [ $ matches [ 1 ] ] ; } elseif ( preg_match ( '/^\s*([a-z]\w*)\s*\((?:\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*)?\)\s*=(?!~|=)\s*(.+)$/' , $ expr , $ matches ) ) { $ fnn = $ matches [ 1 ] ; if ( in_array ( $ matches [ 1 ] , $ this -> fb ) ) { return $ this -> trigger ( " Cannot redefine built-in function ' { $ matches [ 1 ] } ()'. " ) ; } if ( $ matches [ 2 ] != " " ) { $ args = explode ( " , " , preg_replace ( " /\s+/ " , " " , $ matches [ 2 ] ) ) ; } else { $ args = array ( ) ; } if ( ( $ stack = $ this -> nfx ( $ matches [ 3 ] ) ) === false ) { return false ; } for ( $ i = 0 ; $ i < count ( $ stack ) ; $ i ++ ) { $ token = $ stack [ $ i ] ; if ( preg_match ( '/^[a-z]\w*$/' , $ token ) and ! in_array ( $ token , $ args ) ) { if ( array_key_exists ( $ token , $ this -> v ) ) { $ stack [ $ i ] = $ this -> v [ $ token ] ; } else { return $ this -> trigger ( " Undefined variable ' { $ token } ' in function definition. " ) ; } } } $ this -> f [ $ fnn ] = array ( 'args' => $ args , 'func' => $ stack ) ; return true ; } else { return $ this -> pfx ( $ this -> nfx ( $ expr ) ) ; } }
Evaluates the expression and returns the result.
Evaluates the expression and returns the result .
// Strip semicolons at the end. // is it a variable assignment?
 // make sure we're not assigning to a constant // if so, stick it in the variable array // and return the resulting value // is it a function assignment?
 // get the function name // make sure it isn't built in // get the arguments // see if it can be converted to postfix // freeze the state of the non-argument variables // straight up evaluation, woo