summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2025-01-04 12:54:36 +0100
committerDaniel Weipert <git@mail.dweipert.de>2025-01-04 12:54:36 +0100
commit7667162a20ebdedac14de88260df018b961548d4 (patch)
treee997ce1fa6a688fbec3ecc44b1f81cf76b1ec64e
parent57e8f84c1465c0fd8daf78d4178c6b18a65e9158 (diff)
intermediate compiler commitHEADmain
-rwxr-xr-xmnml6
-rw-r--r--src/Interpreter/Interpreter.php330
-rw-r--r--src/Parser/Parser.php63
-rw-r--r--test/const-array.mnml6
-rw-r--r--test/const-function-if.mnml3
-rw-r--r--test/const-map.mnml7
-rw-r--r--test/const-simple.mnml2
7 files changed, 404 insertions, 13 deletions
diff --git a/mnml b/mnml
index 33f7ca0..32937d5 100755
--- a/mnml
+++ b/mnml
@@ -4,6 +4,7 @@
require "vendor/autoload.php";
+use Mnml\Interpreter\Interpreter;
use Mnml\Lexer\Lexer;
use Mnml\Parser\Parser;
@@ -14,4 +15,7 @@ $tokens = $lexer->lex();
$parser = new Parser($tokens);
$nodes = $parser->parse();
-$parser->printTree();
+#$parser->printTree();
+
+$compiler = new Interpreter($nodes);
+$compiler->compile([array_slice($argv, 1)]);
diff --git a/src/Interpreter/Interpreter.php b/src/Interpreter/Interpreter.php
new file mode 100644
index 0000000..7d18551
--- /dev/null
+++ b/src/Interpreter/Interpreter.php
@@ -0,0 +1,330 @@
+<?php
+
+namespace Mnml\Interpreter;
+
+use Mnml\Lexer\Lexer;
+use Mnml\Lexer\Token;
+use Mnml\Lexer\TokenType;
+use Mnml\Parser\ArrayMapAccessNode;
+use Mnml\Parser\ArrayNode;
+use Mnml\Parser\CompareExpression;
+use Mnml\Parser\Condition;
+use Mnml\Parser\ConstVariableDeclaration;
+use Mnml\Parser\FunctionCall;
+use Mnml\Parser\FunctionCallParameter;
+use Mnml\Parser\FunctionDefinition;
+use Mnml\Parser\FunctionReturn;
+use Mnml\Parser\IdentifierNode;
+use Mnml\Parser\IfArm;
+use Mnml\Parser\IfNode;
+use Mnml\Parser\MapNode;
+use Mnml\Parser\Node;
+use Mnml\Parser\NumberNode;
+use Mnml\Parser\OperatorExpression;
+use Mnml\Parser\Parenthesis;
+use Mnml\Parser\Parser;
+use Mnml\Parser\StringNode;
+
+class Interpreter
+{
+ private array $nodes;
+
+ /**
+ * @param Node[] $nodes
+ */
+ public function __construct(array $nodes)
+ {
+ $this->nodes = $nodes;
+ }
+
+ public function compile(array $parameters): Scope
+ {
+ $scope = new Scope();
+
+ $import = function (string $library) {
+ $input = file_get_contents(dirname(__FILE__) . "/" . $library);
+ $lexer = new Lexer($input);
+ $tokens = $lexer->lex();
+ $parser = new Parser($tokens);
+ $nodes = $parser->parse();
+ };
+ $scope->add(new IdentifierNode(new Token(TokenType::Identifier, "import", "import", 0, 0)), $import);
+
+ foreach ($this->nodes as $currentNode) {
+ $value = $this->evaluate($currentNode, $scope);
+
+ if ($currentNode instanceof FunctionCall) {
+ var_dump($value);
+ }
+ }
+
+ return $scope;
+ }
+
+ private function evaluate(Node $currentNode, Scope $scope): mixed
+ {
+ if ($currentNode instanceof ConstVariableDeclaration) {
+ $identifier = $currentNode->identifier;
+
+ if ($currentNode->expression instanceof NumberNode or $currentNode->expression instanceof StringNode) {
+ $value = $currentNode->expression;
+ } else {
+ $value = $this->evaluate($currentNode->expression, $scope);
+ }
+
+ $scope->add($identifier, $value);
+
+ return true;
+ }
+
+ else if ($currentNode instanceof NumberNode) {
+ return $currentNode->value;
+ }
+
+ else if ($currentNode instanceof StringNode) {
+ return $currentNode->token->value;
+ }
+
+ else if ($currentNode instanceof IdentifierNode) {
+ $variable = $scope->get($currentNode);
+
+ return $this->evaluate($variable, $scope);
+ }
+
+ else if ($currentNode instanceof ArrayNode) {
+ return $currentNode;
+ }
+
+ else if ($currentNode instanceof MapNode) {
+ return $currentNode;
+ }
+
+ else if ($currentNode instanceof ArrayMapAccessNode) {
+ return $this->evaluateArrayOrMapAccess($currentNode, $scope);
+ }
+
+ else if ($currentNode instanceof Parenthesis) {
+ return $this->evaluate($currentNode->content, $scope);
+ }
+
+ else if ($currentNode instanceof OperatorExpression) {
+ $left = $this->evaluate($currentNode->left, $scope);
+ $right = $this->evaluate($currentNode->right, $scope);
+
+ if ($currentNode->operator->value == "+") {
+ if (is_string($left) and is_string($right)) {
+ return $left . $right;
+ }
+
+ return $left + $right;
+ }
+
+ else if ($currentNode->operator->value == "-") {
+ return $left - $right;
+ }
+
+ else if ($currentNode->operator->value == "*") {
+ return $left * $right;
+ }
+
+ else if ($currentNode->operator->value == "**") {
+ return pow($left, $right);
+ }
+
+ else if ($currentNode->operator->value == "/") {
+ return $left / $right;
+ }
+ }
+
+ else if ($currentNode instanceof CompareExpression) {
+ $left = $this->evaluate($currentNode->left, $scope);
+ $right = $this->evaluate($currentNode->right, $scope);
+
+ if ($currentNode->operator->value == "<") {
+ return $left < $right;
+ }
+
+ else if ($currentNode->operator->value == ">") {
+ return $left > $right;
+ }
+
+ else if ($currentNode->operator->value == "<=") {
+ return $left <= $right;
+ }
+
+ else if ($currentNode->operator->value == ">=") {
+ return $left >= $right;
+ }
+
+ else if ($currentNode->operator->value == "==") {
+ return $left == $right;
+ }
+
+ else if ($currentNode->operator->value == "!=") {
+ return $left != $right;
+ }
+ }
+
+ else if ($currentNode instanceof IfNode) {
+ foreach ($currentNode->arms as $arm) {
+ /**@var IfArm $arm*/
+ if ($this->evaluate($arm->condition, $scope)) {
+ return $this->evaluateIfArmBody($arm, $scope);
+ }
+ }
+
+ return null;
+ }
+
+ else if ($currentNode instanceof Condition) {
+ $left = $this->evaluate($currentNode->left, $scope);
+ $right = $this->evaluate($currentNode->right, $scope);
+
+ if ($currentNode->operator == "and") {
+ return $left and $right;
+ }
+
+ else if ($currentNode->operator == "or") {
+ return $left or $right;
+ }
+ }
+
+ else if ($currentNode instanceof FunctionDefinition) {
+ return $currentNode;
+ }
+
+ else if ($currentNode instanceof FunctionCall) {
+ $functionDefinition = $scope->get($currentNode->identifier);
+
+ return $this->evaluateFunction($functionDefinition, $currentNode->parameters, $scope);
+ }
+
+ else if (is_callable($currentNode)) {
+ return true;
+ }
+
+ else {
+ echo sprintf("Compiler Error: Unhandled Node type %s." . PHP_EOL, get_class($currentNode));
+ }
+ }
+
+ private function evaluateArrayOrMapAccess(ArrayMapAccessNode $arrayOrMapAccess, Scope $scope): Node
+ {
+ $arrayOrMap = $arrayOrMapAccess->arrayOrMap;
+ if ($arrayOrMap instanceof IdentifierNode) {
+ $arrayOrMap = $this->evaluate($arrayOrMap, $scope);
+ }
+
+ if ($arrayOrMap instanceof ArrayNode) {
+ $value = $arrayOrMap->values;
+
+ foreach ($arrayOrMapAccess->items as $item) {
+ $evaluation = $this->evaluate($item, $scope);
+ $value = $value[$evaluation];
+ }
+
+ return $value;
+ }
+
+ else if ($arrayOrMap instanceof MapNode) {
+ $values = $arrayOrMap->values;
+
+ foreach ($arrayOrMapAccess->items as $item) {
+ $evaluation = $this->evaluate($item, $scope);
+
+ foreach ($values as $value) {
+ $key = $this->evaluate($value->key, $scope);
+ if ($key == $evaluation) {
+ $values = $this->evaluate($value->value, $scope);
+ # TODO: array inside map => create ArrayOrMapAccessItem with $left and $right
+ }
+ }
+ }
+
+ return $values;
+ }
+ }
+
+ /**
+ * @param FunctionCallParameter[] $parameters
+ */
+ private function evaluateFunction(FunctionDefinition $function, array $parameters, Scope $outerScope): mixed
+ {
+ $scope = new Scope($outerScope);
+
+ foreach ($parameters as $parameter) {
+ /**@var FunctionCallParameter $parameter*/
+ $scope->add($parameter->identifier, $parameter->value);
+ }
+
+ foreach ($function->body as $currentNode) {
+ if ($currentNode instanceof FunctionReturn) {
+ return $this->evaluate($currentNode->returnValue, $scope);
+ }
+
+ else if ($currentNode instanceof IfNode) {
+ $returnValue = $this->evaluate($currentNode, $scope);
+
+ // return if "if" had a return inside
+ if ($returnValue) {
+ return $returnValue;
+ }
+ }
+
+ else {
+ $this->evaluate($currentNode, $scope);
+ }
+ }
+
+ echo sprintf("Function %s missing return value." . PHP_EOL);
+ }
+
+ private function evaluateIfArmBody(IfArm $arm, Scope $outerScope): mixed
+ {
+ $scope = new Scope($outerScope);
+
+ foreach ($arm->body as $currentNode) {
+ if ($currentNode instanceof FunctionReturn) {
+ return $this->evaluate($currentNode->returnValue, $scope);
+ }
+
+ else {
+ $this->evaluate($currentNode, $scope);
+ }
+ }
+
+ return null;
+ }
+}
+
+class Scope
+{
+ private ?Scope $outerScope;
+ public array $values;
+ public function __construct(?Scope $outerScope = null)
+ {
+ $this->outerScope = $outerScope;
+ }
+ public function add(IdentifierNode $identifier, mixed $value): void
+ {
+ $key = $identifier->token->value;
+
+ $this->values[$key] = $value;
+ }
+ public function get(IdentifierNode $identifier): mixed
+ {
+ $key = $identifier->token->value;
+
+ if (isset($this->values[$key])) {
+ return $this->values[$key];
+ }
+
+ if (! isset($this->outerScope)) {
+ echo sprintf("Undefined variable \"%s\" at %d:%d" . PHP_EOL, $key, $identifier->token->line, $identifier->token->column);
+
+ return null;
+ }
+
+ return $this->outerScope->get($identifier);
+ }
+}
diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php
index 05d3726..1ce092c 100644
--- a/src/Parser/Parser.php
+++ b/src/Parser/Parser.php
@@ -44,8 +44,11 @@ class Parser
$this->advance(1);
}
+ else if ($currentToken->type == TokenType::Identifier) {
+ $currentStatement = $this->parseFunctionCall();
+ }
+
else {
- #$currentStatement = $this->parseFunctionCall();
$error = sprintf("Unexpected %s at %d:%d" . PHP_EOL, $currentToken->value, $currentToken->line, $currentToken->column);
$this->addError($error);
@@ -182,7 +185,7 @@ class Parser
// skip const
$this->anticipateTokenAndSkip("const");
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip :
@@ -324,6 +327,10 @@ class Parser
return new Condition($currentExpression, $nextToken, $this->parseExpression(shouldBeMap: $shouldBeMap));
}
+ else if ($nextToken->literal == "[") {
+ return $this->parseArrayOrMapAccess($currentExpression);
+ }
+
else if ($nextToken->literal == "=>") {
$this->advance(1);
@@ -419,6 +426,28 @@ class Parser
}
}
+ private function parseArrayOrMapAccess(Node $arrayOrMap): Node
+ {
+ $items = [];
+
+ while ($this->getCurrentToken()->literal == "[") {
+ // skip current [
+ $this->anticipateTokenAndSkip("[");
+
+ $item = $this->parseExpression();
+
+ // skip last ]
+ $this->anticipateTokenAndSkip("]");
+
+ $items[] = $item;
+ }
+
+ return new ArrayMapAccessNode(
+ $arrayOrMap,
+ $items,
+ );
+ }
+
private function parseNumber(): Node
{
$currentToken = $this->getCurrentToken();
@@ -470,7 +499,7 @@ class Parser
continue;
}
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip :
@@ -558,7 +587,7 @@ class Parser
private function parseFunctionCall(): Node
{
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(1);
// skip first (
@@ -590,7 +619,7 @@ class Parser
// if "=" then identifier is name
if ($this->getNextToken()->literal == "=") {
- $identifier = $this->getCurrentToken();
+ $identifier = new IdentifierNode($this->getCurrentToken());
$this->advance(2);
$value = $this->parseExpression();
@@ -696,7 +725,7 @@ class Tree extends Node
class ConstVariableDeclaration extends Node
{
public function __construct(
- public Token $identifier,
+ public Node $identifier,
public Node|Token $type,
public Node|Token $expression,
) {}
@@ -744,6 +773,7 @@ class MapNode extends Node
public array $values,
) {}
}
+
class MapItemNode extends Node
{
public function __construct(
@@ -752,6 +782,17 @@ class MapItemNode extends Node
) {}
}
+class ArrayMapAccessNode extends Node
+{
+ public function __construct(
+ public ArrayNode|IdentifierNode $arrayOrMap,
+ /**
+ * @param Node[] $items
+ */
+ public array $items,
+ ) {}
+}
+
class OperatorExpression extends Node
{
public function __construct(
@@ -822,7 +863,7 @@ class FunctionDefinition extends Node
* @param FunctionDefinitionParameter[] $parameters
*/
public array $parameters,
- public TypeDeclaration $returnType,
+ public TypeDeclaration|ArrayTypeDeclaration|MapTypeDeclaration $returnType,
/**
* @param Node[] $body
*/
@@ -833,8 +874,8 @@ class FunctionDefinition extends Node
class FunctionDefinitionParameter extends Node
{
public function __construct(
- public Token $identifier,
- public TypeDeclaration $type,
+ public Node $identifier,
+ public TypeDeclaration|ArrayTypeDeclaration|MapTypeDeclaration $type,
public ?Node $defaultValue = null,
) {}
}
@@ -849,7 +890,7 @@ class FunctionReturn extends Node
class FunctionCall extends Node
{
public function __construct(
- public Token $identifier,
+ public Node $identifier,
/**
* @param FunctionCallParameter[] $parameters
*/
@@ -860,7 +901,7 @@ class FunctionCall extends Node
class FunctionCallParameter extends Node
{
public function __construct(
- public ?Token $identifier,
+ public ?Node $identifier,
public Node|Token $value,
) {}
}
diff --git a/test/const-array.mnml b/test/const-array.mnml
index 03c7163..e5f30b8 100644
--- a/test/const-array.mnml
+++ b/test/const-array.mnml
@@ -1,2 +1,8 @@
const array: [integer] = [1, 2, 3]
const array2: [integer] = [3, 4, 5]
+
+const main: function = (array3: [integer], array4: [integer]): integer {
+ return array4[1]
+}
+
+main(array3 = array, array4 = array2)
diff --git a/test/const-function-if.mnml b/test/const-function-if.mnml
index 11883cd..2b62f8e 100644
--- a/test/const-function-if.mnml
+++ b/test/const-function-if.mnml
@@ -1,6 +1,7 @@
const main: function = (input: string): string {
if (input == "hello") {
const bye: string = "bye"
+ return input + bye
}
return input
@@ -11,3 +12,5 @@ const main2: function = (input: string): string {
return main(input = "hello")
}
+
+main(input = "hello")
diff --git a/test/const-map.mnml b/test/const-map.mnml
index b63b68b..6942bba 100644
--- a/test/const-map.mnml
+++ b/test/const-map.mnml
@@ -8,4 +8,11 @@ const map: [string][string or integer or bool] = [
},*/
"sixth" = 20_000,
"seventh" = 20.02,
+ "eight" = [8, 9, 0],
]
+
+const main: function = (input: [string][string or integer or bool]): string {
+ return input["eight"][2]
+}
+
+main(input = map)
diff --git a/test/const-simple.mnml b/test/const-simple.mnml
index 5da0a0f..f7f597d 100644
--- a/test/const-simple.mnml
+++ b/test/const-simple.mnml
@@ -1,2 +1,2 @@
const henshin: integer = 2
-const new: integer = henshin + 5 * 10
+const new: integer = (henshin * 5) + 10