#!/usr/bin/env php [args...] * php run.php inspect */ require __DIR__ . '/src/common.php'; use function Arboricx\{ctx_init, ctx_free, loadBundleDefault, ofNumber, ofString, app, reduce, toString, toBool, toNumber, findLib, decode, decodeType, readBundle}; // ── Commands ───────────────────────────────────────────────────────────────── function bail(string $msg): void { fwrite(STDERR, "Error: $msg\n"); exit(1); } function cmdRun(string $libPath, string $bundlePath, array $args): void { $ctx = ctx_init($libPath); try { $term = loadBundleDefault($ctx, readBundle($bundlePath)); foreach ($args as $arg) { $argTree = preg_match('/^\d+$/', $arg) ? ofNumber($ctx, (int)$arg) : ofString($ctx, $arg); $term = app($ctx, $term, $argTree); } $result = reduce($ctx, $term, 1_000_000_000); echo decode($ctx, $result) . "\n"; } catch (\Throwable $e) { bail($e->getMessage()); } finally { ctx_free($ctx); } } function cmdInspect(string $libPath, string $bundlePath): void { $ctx = ctx_init($libPath); try { $bundle = readBundle($bundlePath); echo "Bundle: $bundlePath\nSize: " . strlen($bundle) . " bytes\n\nResult:\n"; $term = loadBundleDefault($ctx, $bundle); $result = reduce($ctx, $term, 1_000_000_000); $type = decodeType($ctx, $result); try { $value = decode($ctx, $result); } catch (\RuntimeException $e) { $value = '(raw tree)'; } echo " Type: $type\n Value: $value\n"; } catch (\Throwable $e) { bail($e->getMessage()); } finally { ctx_free($ctx); } } // ── Main ───────────────────────────────────────────────────────────────────── $argv = $_SERVER['argv'] ?? []; $argc = $_SERVER['argc'] ?? 0; if ($argc < 2) { echo "Arboricx PHP Host Shell (via libarboricx C ABI)\n\nUsage:\n"; echo " php run.php run [args...]\n"; echo " php run.php inspect \n"; exit(0); } $libPath = findLib(); $command = $argv[1]; switch ($command) { case 'run': if ($argc < 3) { fwrite(STDERR, "Usage: php run.php run [args...]\n"); exit(1); } cmdRun($libPath, $argv[2], array_slice($argv, 3)); break; case 'inspect': if ($argc < 3) { fwrite(STDERR, "Usage: php run.php inspect \n"); exit(1); } cmdInspect($libPath, $argv[2]); break; default: fwrite(STDERR, "Unknown command: $command\nUsage: php run.php run|inspect ...\n"); exit(1); }