Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New repl and new project structure #4

Merged
merged 22 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
.DS_Store
composer.lock
.php-cs-fixer.cache
/.phpunit.cache/
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ docker run --rm -v $(pwd):/monkey -w /monkey php:8.4-cli ./vendor/bin/pest
Running from a file contents of the examples folder:

```bash
docker run --rm -v (pwd):/monkey -w /monkey php:8.4-cli ./monkey run examples/fibo_while.monkey
docker run --rm -v (pwd):/monkey -w /monkey php:8.4-cli ./monkey --stats run examples/fibo_while.monkey
```

### Using the REPL
Expand All @@ -158,17 +158,23 @@ docker run -it --rm -v (pwd):/monkey -w /monkey php:8.4-cli ./monkey repl
Example:

```text
🐒 Monkey Programming Language v1.0.0
Type ':h' for help, ':c' for clear, ':q' to quit
+---------------------------------------+
| 🐒 Monkey Programming Language v1.0.0 |
| |
| Type :c for clear, :q to quit |
+---------------------------------------+


➜ let a = 20 + fn(x){ return x + 10; }(2);
32

```

Or, if you want to execute a file:

```bash
docker run --rm -v (pwd):/monkey -w /monkey php:8.4-cli ./monkey run examples/closure.monkey
docker run --rm -v (pwd):/monkey -w /monkey php:8.4-cli ./monkey --stats run examples/closure.monkey
```

### Contributing
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
},
"require": {
"php": "^8.4",
"ext-readline": "*"
"ext-readline": "*",
"symfony/console": "^7.2"
},
"autoload": {
"psr-4": {
"Monkey\\": "src"
"MonkeyLang\\": "src"
}
},
"autoload-dev": {
Expand Down
56 changes: 52 additions & 4 deletions monkey
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,63 @@ declare(strict_types=1);

require 'vendor/autoload.php';

use Monkey\Monkey;
use MonkeyLang\Monkey\Command\CommandFactory;
use MonkeyLang\Monkey\Command\CommandRunner;
use MonkeyLang\Monkey\Config\ConfigurationManager;
use MonkeyLang\Monkey\IO\ConsoleOutput;
use MonkeyLang\Monkey\IO\InputReader;
use MonkeyLang\Monkey\IO\OutputFormatter;
use MonkeyLang\Monkey\Monkey;
use MonkeyLang\Monkey\Performance\PerformanceTracker;
use MonkeyLang\Monkey\Repl\ReplManager;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\ArgvInput;

if (PHP_SAPI !== 'cli') {
exit;
}

if (!isset($GLOBALS['argv']) || !is_array($GLOBALS['argv'])) {
/**
* @var array<string> $argv
*/
$argv = (array) ($GLOBALS['argv'] ?? []);

if ($argv === []) {
exit(1);
}

$cli = new Monkey();
exit($cli->run($GLOBALS['argv']));
try {
$input = new ArgvInput();
$output = new ConsoleOutput();
$questionHelper = new QuestionHelper();

$outputFormatter = new OutputFormatter($output);
$inputReader = new InputReader($input, $output, $questionHelper);
$performanceTracker = new PerformanceTracker();

$replManager = new ReplManager(
inputReader: $inputReader,
outputFormatter: $outputFormatter,
performanceTracker: $performanceTracker
);

$commandFactory = new CommandFactory(
replManager: $replManager,
outputFormatter: $outputFormatter,
performanceTracker: $performanceTracker
);

$configManager = new ConfigurationManager();
$commandRunner = new CommandRunner($commandFactory);

$monkey = new Monkey(
commandRunner: $commandRunner,
configManager: $configManager
);

exit($monkey->run($argv));
} catch (Throwable $e) {
fwrite(STDERR, "Fatal error: {$e->getMessage()}\n");

exit(1);
}
4 changes: 4 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare(strict_types=1);

use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Set\ValueObject\LevelSetList;

return RectorConfig::configure()
Expand All @@ -29,6 +31,8 @@
])
->withSkip([
EncapsedStringsToSprintfRector::class,
PostIncDecToPreIncDecRector::class,
ReadOnlyPropertyRector::class,
])
->withPaths([
__DIR__ . '/src',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class BinaryExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

use function count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Ast\Node;
use MonkeyLang\Lang\Ast\Node;

abstract class Expression extends Node
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Token\Token;
use MonkeyLang\Lang\Token\Token;

final class IdentifierExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Ast\Statements\BlockStatement;
use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Statements\BlockStatement;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class IfExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class IndexExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class PostfixExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class UnaryExpression extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Monkey\Ast\Expressions;
namespace MonkeyLang\Lang\Ast\Expressions;

use Monkey\Ast\Statements\BlockStatement;
use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Statements\BlockStatement;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class WhileExpression extends Expression
{
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/Node.php → src/Lang/Ast/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Monkey\Ast;
namespace MonkeyLang\Lang\Ast;

use Stringable;

Expand Down
7 changes: 3 additions & 4 deletions src/Ast/Program.php → src/Lang/Ast/Program.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast;
namespace MonkeyLang\Lang\Ast;

use Monkey\Ast\Statements\Statement;

use Monkey\Support\StringBuilder;
use MonkeyLang\Lang\Ast\Statements\Statement;
use MonkeyLang\Lang\Support\StringBuilder;

use function count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Ast\Expressions\Expression;
use Monkey\Ast\Expressions\IdentifierExpression;
use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Expressions\Expression;
use MonkeyLang\Lang\Ast\Expressions\IdentifierExpression;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class AssignStatement extends Statement
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Ast\Statements;

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class BlockStatement extends Statement
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Ast\Expressions\Expression;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Expressions\Expression;
use MonkeyLang\Lang\Token\Token;

final class ExpressionStatement extends Statement
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Ast\Expressions\Expression;
use Monkey\Ast\Expressions\IdentifierExpression;
use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Expressions\Expression;
use MonkeyLang\Lang\Ast\Expressions\IdentifierExpression;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class LetStatement extends Statement
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Ast\Statements;

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Ast\Expressions\Expression;
use Monkey\Support\StringBuilder;
use Monkey\Token\Token;
use MonkeyLang\Lang\Ast\Expressions\Expression;
use MonkeyLang\Lang\Support\StringBuilder;
use MonkeyLang\Lang\Token\Token;

final class ReturnStatement extends Statement
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Monkey\Ast\Statements;
namespace MonkeyLang\Lang\Ast\Statements;

use Monkey\Ast\Node;
use MonkeyLang\Lang\Ast\Node;

abstract class Statement extends Node
{
Expand Down
Loading
Loading