File modules/dsl/ast/DslAssignment.class.php

Last commit: Fri Jul 1 18:09:05 2022 +0200	Jan Dankert	New: Bugfixes and much more string and array functions for the DSL.
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 }
Download modules/dsl/ast/DslAssignment.class.php
History Fri, 1 Jul 2022 18:09:05 +0200 Jan Dankert New: Bugfixes and much more string and array functions for the DSL. Mon, 6 Jun 2022 13:35:56 +0200 Jan Dankert Change: Completely ignoring let,var,const statements. Wed, 1 Jun 2022 00:11:40 +0200 Jan Dankert New: DSL as a filter for number values Sun, 29 May 2022 16:56:40 +0200 Jan Dankert New: DSL with support for functions with return values, full arithmetic, object properties Sat, 28 May 2022 18:00:13 +0200 Jan Dankert New: DSL with a much better syntax parsing and support for assignments, conditions, ... Wed, 25 May 2022 22:47:17 +0200 Jan Dankert New: DSL (domain specific language) for code elements. The old way with PHP code ist not sandboxed and unsecure. This approach is a minimalistic, javascript-like, scripting engine. For now only simple function calls are possible, for example: alert("example");