PHP code example of tommyknocker / interpreter

1. Go to this page and download the library: Download tommyknocker/interpreter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tommyknocker / interpreter example snippets



tommyknocker\interpreter\Interpreter;

$interpreter = new Interpreter();

$example = <<<CODE
(json,
    (map,
        (array, "message"),
        (array,
            (concat, "Hello, ",
                (getArg, 0)
            )
        )
    )
)
CODE;
$interpreter->setArgs(['world']);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "message": "Hello, world"
 * }
 */
echo $result . PHP_EOL;

$example = <<<CODE
(json,
    (map,
        (array, "message", "count", "active"),
        (array,
            (concat, "Hello, ", (getArg, 0)),
            42,
            true
        )
    )
)
CODE;
$interpreter->setArgs(['world']);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "message": "Hello, world",
 *      "count": 42,
 *      "active": true
 * }
 */
echo $result . PHP_EOL;

$example = <<<CODE
(json,
    (map,
        (array, "title", "description", "rating", "comment"),
        (array,
            (concat, "Report for ", (getArg, 0)),
            (concat, "Summary: ", (getArg, 1)),
            2.4,
            null
        )
    )
)
CODE;
$interpreter->setArgs(['Alice', 'All systems operational']);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "title": "Report for Alice",
 *      "description": "Summary: All systems operational",
 *      "rating": 2.4,
 *      "comment": null
 * }
 */
echo $result . PHP_EOL;

$example = <<<CODE
(json,
    (map,
        (array, "original", "uppercased", "status", "score"),
        (array,
            (getArg, 0),
            (upper, (getArg, 0)),
            true,
            42.24
        )
    )
)
CODE;
$interpreter->registerFunction('upper', fn($args) => strtoupper($args));
$interpreter->setArgs(['hello world']);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "original": "hello world",
 *      "uppercased": "HELLO WORLD",
 *      "status": true,
 *      "score": 42.24
 * }
 */
echo $result . PHP_EOL;

$example = <<<CODE
(json,
    (map,
        (array, "message", "status"),
        (array,
            (if,
                (getArg, 0),
                (concat, "Hello, ", (getArg, 1)),
                "Error: user not found"
            ),
            (getArg, 0)
        )
    )
)
CODE;
$interpreter->registerFunction('if', function($condition, $thenExpr, $elseExpr) use ($interpreter) {
    return $interpreter->evaluate($condition) ?
        $interpreter->evaluate($thenExpr) :
        $interpreter->evaluate($elseExpr);
});
$interpreter->setArgs([true, "Alice"]);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "message": "Hello, Alice",
 *      "status": true
 * }
 */
echo $result . PHP_EOL;

$interpreter->setArgs([false, "Alice"]);
$ast = $interpreter->parse($example);
$result = $interpreter->evaluate($ast);
/**
 * Result
 * {
 *      "message": "Error: user not found",
 *      "status": false
 * }
 */
echo $result . PHP_EOL;
bash
./vendor/bin/phpunit --bootstrap vendor/autoload.php tests