phrep

Check-in [6c2466e1ae]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Set current as '0.2.9' (no release).
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:6c2466e1ae287b08b17f454c60bd2e20d9abdcfc
User & Date: mario 2015-04-28 23:09:25
Context
2015-04-28
23:20
Added a few silly development tests. check-in: 58129a2013 user: mario tags: trunk
23:09
Set current as '0.2.9' (no release). check-in: 6c2466e1ae user: mario tags: trunk
23:08
Use array_merge() instad of += adjoing operator on updating ->pragma stack. check-in: e2ad430ee0 user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cli.php.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/**
 * api: phrep
 * type: main
 * title: PHreProcessor
 * description: Rewrites preprocessing statements in PHP source files
 * url: http://fossil.include-once.org/phrep/
 * version: 0.2.7
 * license: MITL
 * author: mario@include-once.org
 * category: devel
 * depends: php (>= 5.4)
 * suggests: bin:make
 * config: { type: env list colon, name: PHP_INCLUDE_PATH, description: Additional include search dirs. }
 * architecture: all







|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
/**
 * api: phrep
 * type: main
 * title: PHreProcessor
 * description: Rewrites preprocessing statements in PHP source files
 * url: http://fossil.include-once.org/phrep/
 * version: 0.2.9
 * license: MITL
 * author: mario@include-once.org
 * category: devel
 * depends: php (>= 5.4)
 * suggests: bin:make
 * config: { type: env list colon, name: PHP_INCLUDE_PATH, description: Additional include search dirs. }
 * architecture: all

Added examples/config/named.php.























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
# api: phrep
# title: NAMED@ parameters
# description: Constructs :named=>parameter function calls.
#
# Permits a $var=>value and Ruby-style :name=>value parameter list
# for function calls. Converts named parameters into array as last
# argument (for variadic funcs).
#
# Use via:
#    NAMED@(func($x, $y, :z=>$z, w=>$w))
#
# Alternative:
#    NAMED@(func, arg, arg, ...)
#
# Becomes:
#    func($x, $y, ["z" => $z, "w" => $w]);
#
# Also permits literal `$arg=123` or `raw=>123` or just `x=5` named params.
#

namespace macro;
$proc->defines["NAMED@"] = [["args..."], "return macro\\NAMED(\$args, \$token);"];

/**
 *
 */
function NAMED($args, $token) {

    // Syntax NAMED@(funcname(...))
    if (count($args) == 1) {
        $name = array_shift($token)[1];
        array_shift($token);  // opening '('
        array_pop($token);    // closing ')'
        $args = \io\Token::args($token);
        $args = array_map('\\io\\Token::join', $args);
    }
    // Already a NAMED(func, arg, arg...) list
    else {
        $name = array_shift($args);
    }
    
    // Assemble param list
    $list = [];
    $named = [];
    foreach ($args as $a) {
        if (preg_match("/^[\$\:]?(\w+)\s*=>?\s*(.+)$/s", $a, $uu)) {
            $named[] = "'$uu[1]' => $uu[2]";
        }
        else {
            $list[] = $a;
        }
    }
    if ($named) { // if not empty
        $list[] = "[" . join(", ", $named) . "]";
    }
    return "$name(" . join(", ", $list) . ")";
}

Added examples/config/qw.php.















































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
# api: phrep
# title: Simple qw@ macro
# description: Expands a word list into an array
#
# Similar to Perls qw() this macro expands a space-delimited list
# of words into a standard array.
#
# Use via:
#    qw@(foo bar baz)
#
# Generates:
#    ['foo', 'bar', 'baz']
#
# As exception to the rule uses a lowercase macro name.
# Also available within `defaults.ph` as compacter #define rule.


namespace macro;
$proc->defines["qw"] = [["words"], "return macro\\qw(\$token);"];

/**
 * Expand word list into array.
 *
 */
function qw($token) {
    return "[" . join(", ",
        array_map(
             function($word) {
                 return var_export($word, TRUE);
             },
             preg_split(
                 "/\s+/",
                 trim(\io\Token::join($token))
             )
         )
    ) . "]";
}

Changes to macro.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
 * api: php
 * title: PHP Macro Preprocessor
 * description: Context-sensitive C-style #define/macro directive processing.
 * license: MITL
 * version: 0.2.8
 * state: beta
 * depends: php (>= 5.4.0)
 * type: rewrite
 * category: macro
 * url: http://fossil.include-once.org/phrep/
 * pack: macro.php=MacroProcessor.php
 *
................................................................................
        "interpolate" => "token", # Or "regex" mode, or "erb", "phpp", or "delim << >>" for custom delimiters.
        "dirs" => NULL,           # Append include -I search paths.
        "preserve" => "output,file,line",  # Pragma states to retain between #includes
        "strip_php_tags" => 1,    # Remove <?php open and close tokens from #include files
    ];

    // Updated via versionnum
    const PHREP_VERSION = "0.2.8";

    /**
     * Predefined and collected constants and macros.
     */
    var $defines = [

       #-- Predeclare some C-style constants






|







 







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
 * api: php
 * title: PHP Macro Preprocessor
 * description: Context-sensitive C-style #define/macro directive processing.
 * license: MITL
 * version: 0.2.9
 * state: beta
 * depends: php (>= 5.4.0)
 * type: rewrite
 * category: macro
 * url: http://fossil.include-once.org/phrep/
 * pack: macro.php=MacroProcessor.php
 *
................................................................................
        "interpolate" => "token", # Or "regex" mode, or "erb", "phpp", or "delim << >>" for custom delimiters.
        "dirs" => NULL,           # Append include -I search paths.
        "preserve" => "output,file,line",  # Pragma states to retain between #includes
        "strip_php_tags" => 1,    # Remove <?php open and close tokens from #include files
    ];

    // Updated via versionnum
    const PHREP_VERSION = "0.2.9";

    /**
     * Predefined and collected constants and macros.
     */
    var $defines = [

       #-- Predeclare some C-style constants

Changes to phrep.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
....
1189
1190
1191
1192
1193
1194
1195
1196
1197

1198
1199
1200
1201
1202
1203
1204

1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228

1229
1230
1231
1232
1233
1234
1235
....
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
....
1322
1323
1324
1325
1326
1327
1328
1329


1330

1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353




1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366

1367
1368
1369
1370
1371
1372
1373
1374
1375
....
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
<?php
/**
 * api: phrep
 * type: main
 * title: PHreProcessor
 * description: Rewrites preprocessing statements in PHP source files
 * url: http://fossil.include-once.org/phrep/
 * version: 0.2.7
 * license: MITL
 * author: mario@include-once.org
 * category: devel
 * depends: php (>= 5.4)
 * suggests: bin:make
 * config: { type: env list colon, name: PHP_INCLUDE_PATH, description: Additional include search dirs. }
 * architecture: all
................................................................................
} # global namespace

/**
 * api: php
 * title: PHP Macro Preprocessor
 * description: Context-sensitive C-style #define/macro directive processing.
 * license: MITL
 * version: 0.2.7
 * state: beta
 * depends: php (>= 5.4.0)
 * type: rewrite
 * category: macro
 * url: http://fossil.include-once.org/phrep/
 * pack: macro.php=MacroProcessor.php
 *
................................................................................
        "interpolate" => "token", # Or "regex" mode, or "erb", "phpp", or "delim << >>" for custom delimiters.
        "dirs" => NULL,           # Append include -I search paths.
        "preserve" => "output,file,line",  # Pragma states to retain between #includes
        "strip_php_tags" => 1,    # Remove <?php open and close tokens from #include files
    ];

    // Updated via versionnum
    const PHREP_VERSION = "0.2.7";

    /**
     * Predefined and collected constants and macros.
     */
    var $defines = [

       #-- Predeclare some C-style constants
................................................................................

        // Change some states between includes (output,file,line)
        $preserve = array_intersect_key($this->pragma, array_flip(str_getcsv($this->pragma["preserve"])));
        $this->pragma += ["file" => $fn, "line" => 0, "output" => fnmatch($fn, $this->pragma["omit"], GLOB_BRACE)];

        // Run through preprocessor phase, and reset kept states
        $src = $this->block($src);
        $this->pragma += $preserve;
        return $src;
    }


    /**
     * Search ->dirs for #include $fn with automatic extension probing.
     *
................................................................................
 * error reporting, and operator/func precedence is incomplete/untuned..
 *
 */
class MacroExpression {

    # Expression tokens
    const RX_EXPR = "{
         (?: [\"\'](?<string>.*?)[\"\'])        # plain strings, no escape checking
       | (?<func>    defined|pragma|strpos|stripos|strstr|stristr|is_int|is_float|max|min|file_exists)

       | (?<op>      >=|<=|==|!=|=~|\&\&|\|\| | [-+/*^~&|(),%!<>?:] )     # operators
       | (?<number>  [+-]?\d+(?:\.\d+)*)        # can be multi-tuple version string
       | (?<literal> [\w_]+@?)                  # defined constants
    }mx";


    // Inherit from parent for constant/pragma lookups

    function __construct($defines=[], $pragma=[]) {
        $this->defines = $defines;
        $this->pragma = $pragma;
        preg_match("/func>\s+([\w|]+)/", self::RX_EXPR, $func)
        and $this->func = array_flip(explode("|", $func[1]));
    }
    public $defines = [];
    public $pragma = [];
    protected $func = [];

    // Run expression through `intval` etc.
    public static function __callStatic($name, $args) {
        if (isset($args[1]) and is_object($args[1])) {
            $x = new self($args[1]->defines, $args[1]->pragma);
        }
        else {
            $x = new self;
        }
        return call_user_func($name, $x->expr($args[0]));
    }


    /**
     * Tokenize expression,  into a list of [type, value] pairs.

     *
     */
    public function expr($src) {
        // Shortcuts
        if ($src === "1" or $src === "0") {
            return intval($src);
        }
................................................................................
     * Run through tokens, spool values+operators into reverse polish notation.
     * [2, 3, 5, *, +]  or for example  [CONST, defined, 1, >=]
     *
     */
    function group($tokens) {

        // operator precedence list
        $prec = array_flip(explode(" ", "( ) , || && == >= > <= < == =~ != ! ? : & + - | ^ * / % ~"));
        $var = [];   // operand queue
        $ops = [];   // operator stack

        // .iteritems() over token list
        while ($t = array_shift($tokens)) {
            $type = key($t);
            $val = current($t);

            // literals
            if ($type == "literal" or $type == "number" or $type == "string") {
                $var[] = $val;#[$type, $val];
            }

            // parenthesis grouping
            elseif (($val == "(") or ($type == "func")) {
                $ops[] = $val;#[$type, $val];
            }
            elseif ($val == ")") {
                // push all ops in parens
                while (count($ops) && (end($ops) != "(")) {
                   $var[] = $val = array_pop($ops);
                }
                array_pop($ops);

                // check for preceding function name
                if (isset($this->func[end($ops)])) {
                   $var[] = array_pop($ops);
                }
            }

            // add to operator queue
            else {
                while (count($ops) and $last = end($ops)
                  and (isset($prec[$val], $prec[$last]) and $prec[$val] < $prec[$last]))
                {
                    $var[] = array_pop($ops);
                }
                $ops[] = $val;#[$type, $val];
            }
#printf("=TOK=%10s  VAR=%26s OPS=%26s\n", json_encode($val), json_encode($var), json_encode($ops));
        }

        // append remaining operators
        $var = array_merge($var, array_reverse($ops));
        return $var;
    }

................................................................................
            $v = array_splice($sum, -$prev, 1, []);
            return current($v);
        };

        // Dispatch value literals as encountered operations
        // (Rather should carry the token types over. Would also simplify ordering operands.)
        while (count($opvals)) {
            $e = array_shift($opvals);


            switch ($e) {

                case "!":  $r = !$pop();  break;
                case "~":  $r = ~intval($pop());  break;
                case "+":  $r = $pop(1) + $pop(1);  break;
                case "-":  $r = -$pop(); $r += $pop();  break;
                case "*":  $r = $pop(1) * $pop(1);  break;
                case "/":  $r = $pop(2) / $pop(1);  break;
                case "^":  $r = intval($pop(1)) ^ intval($pop(1));  break;
                case "&":  $r = $pop(1) & $pop(1);  break;
                case "|":  $r = $pop(1) | $pop(1);  break;
                case "%":  $r = $pop(1) % $pop(1);  break;
                case "&&": $x = $pop(); $y = $pop(); $r = intval($x && $y); break;
                case "||": $x = $pop(); $y = $pop(); $r = intval($x || $y); break;
                case "==": $r = $pop(1) == $pop(1);  break;
                case "!=": $r = $pop(1) != $pop(1);  break;
                case "?":  $r = $pop(1); break;
                case ":":  $y = $pop(1); $x = $pop(1); $c = $pop(1); $r = $c ? $x : $y;  break;
                case ">":  $r = version_compare($pop(), $pop()) < 0;   break;
                case "<":  $r = version_compare($pop(), $pop()) > 0;   break;
                case ">=": $r = version_compare($pop(), $pop()) <= 0;  break;
                case "<=": $r = version_compare($pop(), $pop()) >= 0;  break;
                case "=~": $r = preg_match("\1{$pop()}\1sim", $pop());   break;
                case ",":  $r = array_merge((array)$pop(), (array)$pop()); break;
                default:




                    // Run function calls.  Any previous params might have been arrayified through `,` operator
                    if ($this->allowed_func($e)) {
                        $r = call_user_func_array($e, array_reverse( (array)$pop() ) );
                    }
                    // Eagerly expand literals if constant name. But skip if `defined` operation follows.
                    elseif (isset($this->defines[$e]) and (empty($opvals) or reset($opvals) !== "defined")) {
                        $r = $this->defines[$e][1];
                    }
                    // Just numbers, strings or constants
                    else {
                        $r = $e;
                    }
            }

            $sum[] = $r;
#           printf(" %26s ➫ %-8s 🔛  %16s ← %4s\n", join(" ", $opvals), "{$e}", json_encode($sum), json_encode($r));
        }

        // Only one remaining data element if expression correctly processed
        if (count($sum) == 1) {
            return current($sum);
        }
        else {
................................................................................
     * → defined(CONST) || pragma(optionval)
     *
     */
    protected function defined($name) {
        return !is_null($name) && isset($this->defines[$name]);
    }
    protected function pragma($name) {
        return isset($this->pragma) ? $this->pragma[$name] : NULL;
    }
    
    /**
     * Check for allowed functions; either global function or method (rewrite [$e] param).
     *
     */
    protected function allowed_func(&$e) {
        if (!isset($this->func[$e])) {
            return FALSE;
        }
        if (is_callable([$this, $e])) {
            $e = [$this, $e];
        }
        if (is_callable($e)) {
            return TRUE;
        }
    }
}


/*







|







 







|







 







|







 







|







 







|
|
>
|





|
>


|
<
<

<
<
<



|
<
<
<
|
<
|




|
>







 







|









|
|



|
|



|
|




|







|



|

<







 







|
>
>
|
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
>

<







 







|



|


|
<
<
<
|
|

|







2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
...
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
....
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209


1210



1211
1212
1213
1214



1215

1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
....
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295

1296
1297
1298
1299
1300
1301
1302
....
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348

1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367

1368
1369
1370
1371
1372
1373
1374
....
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397



1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
<?php
/**
 * api: phrep
 * type: main
 * title: PHreProcessor
 * description: Rewrites preprocessing statements in PHP source files
 * url: http://fossil.include-once.org/phrep/
 * version: 0.2.9
 * license: MITL
 * author: mario@include-once.org
 * category: devel
 * depends: php (>= 5.4)
 * suggests: bin:make
 * config: { type: env list colon, name: PHP_INCLUDE_PATH, description: Additional include search dirs. }
 * architecture: all
................................................................................
} # global namespace

/**
 * api: php
 * title: PHP Macro Preprocessor
 * description: Context-sensitive C-style #define/macro directive processing.
 * license: MITL
 * version: 0.2.9
 * state: beta
 * depends: php (>= 5.4.0)
 * type: rewrite
 * category: macro
 * url: http://fossil.include-once.org/phrep/
 * pack: macro.php=MacroProcessor.php
 *
................................................................................
        "interpolate" => "token", # Or "regex" mode, or "erb", "phpp", or "delim << >>" for custom delimiters.
        "dirs" => NULL,           # Append include -I search paths.
        "preserve" => "output,file,line",  # Pragma states to retain between #includes
        "strip_php_tags" => 1,    # Remove <?php open and close tokens from #include files
    ];

    // Updated via versionnum
    const PHREP_VERSION = "0.2.9";

    /**
     * Predefined and collected constants and macros.
     */
    var $defines = [

       #-- Predeclare some C-style constants
................................................................................

        // Change some states between includes (output,file,line)
        $preserve = array_intersect_key($this->pragma, array_flip(str_getcsv($this->pragma["preserve"])));
        $this->pragma += ["file" => $fn, "line" => 0, "output" => fnmatch($fn, $this->pragma["omit"], GLOB_BRACE)];

        // Run through preprocessor phase, and reset kept states
        $src = $this->block($src);
        $this->pragma = array_merge($this->pragma, $preserve);
        return $src;
    }


    /**
     * Search ->dirs for #include $fn with automatic extension probing.
     *
................................................................................
 * error reporting, and operator/func precedence is incomplete/untuned..
 *
 */
class MacroExpression {

    # Expression tokens
    const RX_EXPR = "{
         (?: ([\"\']) (?<string>.*?) \\1)       # plain strings, no escape checking
       | (?<func>    defined|pragma|strpos|stripos|strstr|stristr|is_int|is_float|is_numeric|
                     max|min|file_exists|function_exists|class_exists)
       | (?<op>      >=|<=|==|!=|=~|\&\&|\|\| | [@\-+/*^~&|(),%!<>?:] )     # operators
       | (?<number>  [+-]?\d+(?:\.\d+)*)        # can be multi-tuple version string
       | (?<literal> [\w_]+@?)                  # defined constants
    }mx";


    // Keep #define and #pragma values for reference
    public $defines, $pragma;
    function __construct($defines=[], $pragma=[]) {
        $this->defines = $defines;
        $this->pragma = $pragma;  //@T: just merge both?


    }




    // Run expression through `intval` etc.
    public static function __callStatic($name, $args) {
        list($expr, $proc) = $args;



        $x = new self($proc->defines, $proc->pragma);

        return call_user_func($name, $x->expr($expr));
    }


    /**
     * Tokenize expression into a list of type => value pairs.
     * Convert into RPN, then evaluate operands/operator list.
     *
     */
    public function expr($src) {
        // Shortcuts
        if ($src === "1" or $src === "0") {
            return intval($src);
        }
................................................................................
     * Run through tokens, spool values+operators into reverse polish notation.
     * [2, 3, 5, *, +]  or for example  [CONST, defined, 1, >=]
     *
     */
    function group($tokens) {

        // operator precedence list
        $precedence = array_flip(explode(" ", "( ) , || && == >= > <= < == =~ != ! ? : & + - @ | ^ * / % ~"));
        $var = [];   // operand queue
        $ops = [];   // operator stack

        // .iteritems() over token list
        while ($t = array_shift($tokens)) {
            $type = key($t);
            $val = current($t);

            // literals
            if ($type == 'literal' or $type == 'number' or $type == 'string') {
                $var[] = [$type, $val];
            }

            // parenthesis grouping
            elseif (($val == "(")) {
                $ops[] = [$type, $val];
            }
            elseif ($val == ")") {
                // push all ops in parens
                while (count($ops) and ['op',"("] !== end($ops)) {
                   $var[] = array_pop($ops);
                }
                array_pop($ops);

                // check for preceding function name
                while (end($ops)[0] == 'func') {
                   $var[] = array_pop($ops);
                }
            }

            // add to operator queue
            else {
                while (count($ops) and $last = end($ops)
                  and (isset($precedence[$val], $precedence[$last[1]]) and $precedence[$val] < $precedence[$last[1]]))
                {
                    $var[] = array_pop($ops);
                }
                $ops[] = [$type, $val];
            }

        }

        // append remaining operators
        $var = array_merge($var, array_reverse($ops));
        return $var;
    }

................................................................................
            $v = array_splice($sum, -$prev, 1, []);
            return current($v);
        };

        // Dispatch value literals as encountered operations
        // (Rather should carry the token types over. Would also simplify ordering operands.)
        while (count($opvals)) {
            list($type, $val) = array_shift($opvals);

            if ($type == 'op'){
                switch ($val) {
                    case "@":  continue 2;
                    case "!":  $r = !$pop();  break;
                    case "~":  $r = ~intval($pop());  break;
                    case "+":  $r = $pop(1) + $pop(1);  break;
                    case "-":  $r = -$pop(); $r += $pop();  break;
                    case "*":  $r = $pop(1) * $pop(1);  break;
                    case "/":  $r = $pop(2) / $pop(1);  break;
                    case "^":  $r = intval($pop(1)) ^ intval($pop(1));  break;
                    case "&":  $r = $pop(1) & $pop(1);  break;
                    case "|":  $r = $pop(1) | $pop(1);  break;
                    case "%":  $r = $pop(1) % $pop(1);  break;
                    case "&&": $x = $pop(); $y = $pop(); $r = intval($x && $y); break;
                    case "||": $x = $pop(); $y = $pop(); $r = intval($x || $y); break;
                    case "==": $r = $pop(1) == $pop(1);  break;
                    case "!=": $r = $pop(1) != $pop(1);  break;
                    case "?":  $r = $pop(1); break;
                    case ":":  $y = $pop(1); $x = $pop(1); $c = $pop(1); $r = $c ? $x : $y;  break;
                    case ">":  $r = version_compare($pop(), $pop()) < 0;   break;
                    case "<":  $r = version_compare($pop(), $pop()) > 0;   break;
                    case ">=": $r = version_compare($pop(), $pop()) <= 0;  break;
                    case "<=": $r = version_compare($pop(), $pop()) >= 0;  break;
                    case "=~": $r = preg_match("\1{$pop()}\1sim", $pop());   break;
                    case ",":  $r = array_merge((array)$pop(), (array)$pop()); break;

                    default: assert('false /* unhandled operator `$val` */');
                }
            }

            // Run function calls.  Any previous params might have been arrayified through `,` operator
            elseif ($type == 'func' and $this->allowed_func($val)) {
                $r = call_user_func_array($val, array_reverse( (array)$pop() ) );  /*p*/$val="FUNC";
            }
            // Eagerly expand literals if constant name. But skip if `defined` operation follows.
            elseif (isset($this->defines[$val]) and (empty($opvals) or reset($opvals) !== ['func',"defined"])) {
                $r = $this->defines[$val][1];
            }
            // Just numbers, strings or constants
            else {
                $r = $val;
            }

#           printf(" %26s ➫ %-8s 🔛  %16s ← %4s\n", json_encode($opvals), "{$val}", json_encode($sum), json_encode($r));
            $sum[] = $r;

        }

        // Only one remaining data element if expression correctly processed
        if (count($sum) == 1) {
            return current($sum);
        }
        else {
................................................................................
     * → defined(CONST) || pragma(optionval)
     *
     */
    protected function defined($name) {
        return !is_null($name) && isset($this->defines[$name]);
    }
    protected function pragma($name) {
        return isset($this->pragma[$name]) ? $this->pragma[$name] : NULL;
    }
    
    /**
     * Check for allowed functions; either global function or method (rewrite [$val] param).
     *
     */
    protected function allowed_func(&$val) {



        if (is_callable([$this, $val])) {
            $val = [$this, $val];
        }
        if (is_callable($val)) {
            return TRUE;
        }
    }
}


/*