Skip to content

Conversation

@thekid
Copy link
Member

@thekid thekid commented Apr 7, 2023

This PR:

  • Makes $this->{$member} distinguishable from $this->$member
  • Adds support for dynamic variables
  • Introduces a new Expression class, which encapsulates {<expr>} syntax (see below for where this is applicable)
  • Changes the Variable class to be able to contain other variables or expressions
Syntax Result Note
Variables
$var Variable("var") Const
$$var Variable(Variable("var"))
${$var} Variable(Expression(Variable("var")))
Properties
$this->var Instance(Variable("this"), Literal("var")) Const
$this->$var Instance(Variable("this"), Variable("var")
$this->{$var} Instance(Variable("this"), Expression(Variable("var"))
Static properties
self::$var Scope("self", Variable("var") Const
self::$$var Scope("self", Variable(Variable("var"))
self::${$var} Scope("self", Variable(Expression(Variable("var")))
Constants
self::var Scope("self", Literal("var")) Const
self::{$var} Scope("self", Expression(Variable("var")) See xp-framework/compiler#161

⚠️ This creates a BC break and would result in a major version release if merged

$line+= substr_count($string, "\n");
} else if ('$' === $token) {
yield new Token($language->symbol('(variable)'), 'variable', '$'.$next(self::DELIMITERS), $line);
yield new Token($language->symbol('(variable)'), 'variable', '$', $line);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For any parser implementation, it's now necessary to forward once more to get the variable name!

@thekid
Copy link
Member Author

thekid commented Apr 8, 2023

The xp-framework/reflection library requires this patch:

diff --git a/composer.json b/composer.json
index b024e0d..c064b7d 100755
--- a/composer.json
+++ b/composer.json
@@ -7,7 +7,7 @@
   "keywords": ["module", "xp"],
   "require" : {
     "xp-framework/core": "^11.0 | ^10.13",
-    "xp-framework/ast": "^9.0 | ^8.0 | ^7.6",
+    "xp-framework/ast": "^10.0 | ^9.0 | ^8.0 | ^7.6",
     "php" : ">=7.0.0"
   },
   "suggest" : {
diff --git a/src/main/php/lang/meta/FromSyntaxTree.class.php b/src/main/php/lang/meta/FromSyntaxTree.class.php
index 8adcb65..55b0cba 100755
--- a/src/main/php/lang/meta/FromSyntaxTree.class.php
+++ b/src/main/php/lang/meta/FromSyntaxTree.class.php
@@ -56,9 +56,10 @@ class FromSyntaxTree {
         do {
           switch ($parse->token->value) {
             case '(': $b++; break;
-            case ')': $b--; if ($b < 0) break 2;
+            case ')': $b--; if ($b < 0) break 2; else break;
             case '[': $c++; break;
-            case ']': $c--; if ($c < 0) break 2;
+            case ']': $c--; if ($c < 0) break 2; else break;
+            case '$': $parse->forward(); $parse->token->value= '$'.$parse->token->value; break;
           }
           $code.= ' '.$parse->token->value;
           $parse->forward();
@@ -86,6 +87,9 @@ class FromSyntaxTree {
             $b++;
           } else if ('}' === $parse->token->value) {
             if (0 === --$b) break;
+          } else if ('$' === $parse->token->value) {
+            $parse->forward();
+            $parse->token->value= '$'.$parse->token->value;
           }
           $code.= ' '.$parse->token->value;
           $parse->forward();
diff --git a/src/main/php/lang/meta/SyntaxTree.class.php b/src/main/php/lang/meta/SyntaxTree.class.php
index 43cd132..695c5e1 100755
--- a/src/main/php/lang/meta/SyntaxTree.class.php
+++ b/src/main/php/lang/meta/SyntaxTree.class.php
@@ -96,7 +96,7 @@ class SyntaxTree extends Visitor {
     // Use PHP reflection API to access members' runtime values. We cannot use
     // getStaticPropertyValue() as it cannot get non-public members in PHP 7.0
     if ($self->member instanceof Variable) {
-      $p= (new \ReflectionClass($c))->getProperty($self->member->name);
+      $p= (new \ReflectionClass($c))->getProperty($self->member->pointer ?? $self->member->name);
       $p->setAccessible(true);
       return $p->getValue();
     } else if ($self->member instanceof Literal) {

The statement $self->member->pointer ?? $self->member->name ensures compatibility with both Variable class versions.

thekid added a commit to xp-framework/reflection that referenced this pull request Apr 8, 2023
@thekid thekid merged commit b1cbb8f into master Apr 8, 2023
@thekid thekid deleted the feature/expressions branch April 8, 2023 16:47
@thekid
Copy link
Member Author

thekid commented Apr 8, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants