openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

DslAssignment.class.php (1774B)


      1 <?php
      2 
      3 namespace dsl\ast;
      4 
      5 use dsl\DslParserException;
      6 use dsl\DslRuntimeException;
      7 use dsl\DslToken;
      8 
      9 class DslAssignment implements DslStatement
     10 {
     11 	private $target = [];
     12 	private $value;
     13 
     14 	/**
     15 	 * DslAssignment constructor.
     16 	 * @param $target DslStatement
     17 	 * @param $value DslStatement
     18 	 * @throws DslParserException
     19 	 */
     20 	public function __construct( $target, $value )
     21 	{
     22 		//echo "<h5>Assignment:</h5><pre>"; var_export( $target ); var_export($value); echo "</pre>";
     23 
     24 		$this->target = $target;
     25 		$this->value  = $value;
     26 	}
     27 
     28 	/**
     29 	 * @param array $context
     30 	 * @return mixed|void
     31 	 * @throws DslRuntimeException
     32 	 */
     33 	public function execute( & $context ) {
     34 
     35 		$value = $this->value->execute( $context );
     36 
     37 		// if the variable is not already bound in this context it will be created.
     38 		// there is no need for a "var" or "let". they are completely obsolete.
     39 		//if   ( ! array_key_exists( $this->target->name,$context ) )
     40 		//	throw new DslRuntimeException('variable \''.$this->target->name.'\' does not exist');
     41 
     42 		$context[ $this->target->name ] = $value;
     43 
     44 		return $value;
     45 	}
     46 
     47 	public function parse($tokens)
     48 	{
     49 		$this->target = new DslExpression();
     50 		$this->value  = new DslExpression();
     51 
     52 		$assignmentOperatorFound = false;
     53 		$targetToken = [];
     54 		$valueToken  = [];
     55 
     56 		foreach( $tokens as $token ) {
     57 
     58 			if   ( $token->type == DslToken::T_OPERATOR && in_array($token->value,['=','+='.'-=']) ) {
     59 				$assignmentOperatorFound = true;
     60 				continue;
     61 			}
     62 
     63 			if   ( ! $assignmentOperatorFound )
     64 				$targetToken[] = $token;
     65 			else
     66 				$valueToken[] = $token;
     67 		}
     68 
     69 		if  ( $assignmentOperatorFound ) {
     70 			$this->target->parse( $targetToken );
     71 			$this->value ->parse( $valueToken );
     72 		} else 	{
     73 			$this->value->parse( $targetToken );
     74 			$this->target = null;
     75 		}
     76 	}
     77 }