tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

eval.php (1454B)


      1 <?php
      2 
      3 declare(strict_types=1);
      4 
      5 error_reporting(E_ALL);
      6 ini_set('display_errors', '1');
      7 
      8 if (!extension_loaded('ffi')) {
      9     http_response_code(500);
     10     echo "Error: PHP FFI extension is not loaded.\n";
     11     echo "If you are using the Nix build, run the included server script:\n";
     12     echo "  ./result/bin/tricu-php-server\n";
     13     exit;
     14 }
     15 
     16 require __DIR__ . '/../src/common.php';
     17 
     18 use function Arboricx\{ctx_init, ctx_free, loadBundleDefault, ofNumber, ofString, app, reduce, decode, findLib, readBundle};
     19 
     20 header('Content-Type: text/plain; charset=utf-8');
     21 
     22 try {
     23     if (!isset($_FILES['bundle']) || $_FILES['bundle']['error'] !== UPLOAD_ERR_OK) {
     24         throw new \RuntimeException('Bundle upload failed.');
     25     }
     26 
     27     $args = [];
     28     for ($i = 0; $i < 5; $i++) {
     29         $v = $_POST["arg$i"] ?? '';
     30         if ($v !== '') {
     31             $args[] = $v;
     32         }
     33     }
     34 
     35     $libPath = findLib();
     36     $ctx = ctx_init($libPath);
     37     try {
     38         $term = loadBundleDefault($ctx, readBundle($_FILES['bundle']['tmp_name']));
     39 
     40         foreach ($args as $arg) {
     41             $argTree = preg_match('/^\d+$/', $arg) ? ofNumber($ctx, (int)$arg) : ofString($ctx, $arg);
     42             $term = app($ctx, $term, $argTree);
     43         }
     44 
     45         $result = reduce($ctx, $term, 1_000_000_000);
     46         echo decode($ctx, $result);
     47     } finally {
     48         ctx_free($ctx);
     49     }
     50 } catch (\Throwable $e) {
     51     http_response_code(500);
     52     echo 'Error: ' . $e->getMessage();
     53 }