PHP通用的读取-评估-打印循环

1.0.0 2023-03-20 10:57 UTC

This package is not auto-updated.

Last update: 2024-09-17 16:16:23 UTC


README

GreenCape REPL 是一个用 PHP 编写的简单的读取-评估-打印循环 (REPL)。它可以与任意评估器结合使用。

安装

composer require greencape/repl

使用

创建一个实现 GreenCape\REPL\EvaluatorInterface 接口的评估器,并将其传递给 REPL

use GreenCape\REPL\EvaluatorInterface;
use GreenCape\REPL\ReadEvalPrintLoop;

class MyEvaluator implements EvaluatorInterface
{
    public function init(): void {}
    public function exit(): void {}
    public function eval(string $input): string
    {
        // Handle the input and return the result
        return $result;
    }
}

$evaluator = new MyEvaluator();
$repl      = new ReadEvalPrintLoop($evaluator);

$repl->run();

使用 init()exit() 可以实现 REPL 的设置和清理。

循环会在 '> ' 提示符后读取您的输入,并将其发送到评估器的 eval() 方法。结果将打印到控制台。您可以输入多行输入,每行以反斜杠结束。继续行由 '>> ' 提示符表示。

要终止循环,请输入 exit