diff options
Diffstat (limited to 'src/Parser/Parser.php')
-rw-r--r-- | src/Parser/Parser.php | 66 |
1 files changed, 58 insertions, 8 deletions
diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index a3a6839..b0b98f2 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -43,9 +43,14 @@ class Parser $currentStatement = new CommentNode($currentToken); $this->advance(1); } - + else if ($currentToken->type == TokenType::Identifier) { - $currentStatement = $this->parseFunctionCall(); + if ($this->getNextToken()->literal == "=") { + $currentStatement = $this->parseVariableAssignment(); + } + else { + $currentStatement = $this->parseFunctionCall(); + } } else { @@ -193,17 +198,39 @@ class Parser $type = $this->parseType(); + $expression = null; + if ($this->getCurrentToken()->literal == "=") { + // skip = + $this->anticipateTokenAndSkip("="); + + $expression = $this->parseExpression( + shouldBeMap: $type instanceof MapTypeDeclaration, + shouldBeFunction: $type instanceof TypeDeclaration and $type->left->literal == "function", + ); + } + + return new ConstVariableDeclaration( + $identifier, + $type, + $expression, + ); + } + + private function parseVariableAssignment(): Node + { + $identifier = new IdentifierNode($this->getCurrentToken()); + $this->advance(1); + // skip = $this->anticipateTokenAndSkip("="); - + $expression = $this->parseExpression( - shouldBeMap: $type instanceof MapTypeDeclaration, - shouldBeFunction: $type instanceof TypeDeclaration and $type->left->literal == "function", + #shouldBeMap: $type instanceof MapTypeDeclaration, + #shouldBeFunction: $type instanceof TypeDeclaration and $type->left->literal == "function", ); - return new ConstVariableDeclaration( + return new VariableAssignment( $identifier, - $type, $expression, ); } @@ -565,7 +592,12 @@ class Parser } else if ($currentToken->type == TokenType::Identifier) { - $body[] = $this->parseFunctionCall(); + if ($this->getNextToken()->literal == "=") { + $body[] = $this->parseVariableAssignment(); + } + else { + $body[] = $this->parseFunctionCall(); + } } else { @@ -732,6 +764,14 @@ class ConstVariableDeclaration extends Node public function __construct( public Node $identifier, public Node|Token $type, + public Node|Token|null $expression, + ) {} +} + +class VariableAssignment extends Node +{ + public function __construct( + public Node $identifier, public Node|Token $expression, ) {} } @@ -767,6 +807,11 @@ class ArrayNode extends Node public function __construct( public array $values, ) {} + + public function getValue(): array + { + return $this->values; + } } class MapNode extends Node @@ -777,6 +822,11 @@ class MapNode extends Node public function __construct( public array $values, ) {} + + public function getValue(): array + { + return $this->values; + } } class MapItemNode extends Node |