PHP code example of ffi / preprocessor

1. Go to this page and download the library: Download ffi/preprocessor 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/ */

    

ffi / preprocessor example snippets


$pre = new FFI\Preprocessor\Preprocessor();

echo $pre->process(<<<'C'
    #define SQUARE(x) ((x) * (x))

    int area = SQUARE(4);
    C);

// int area = ((4) * (4));

$result = $pre->process("#define A 1\n#warning careful\n");

$result->hasMacro('A');        // true, the macros as they are when the run ends
$result->findMacro('A');       // MacroInterface|null
$result->getMacros();          // iterable<MacroInterface>

$result->getFiles();           // the first file and the 

$pre->macros->define('DEBUG');                       // #define DEBUG 1
$pre->macros->define('LEVEL', '3');                  // #define LEVEL 3
$pre->macros->remove('DEBUG');                       // #undef DEBUG


// A closure with no parameters is an object-like macro, evaluated at each replacement
$pre->macros->define('REQUEST_ID', fn() => uniqid());

// A closure with parameters is a function-like macro, it gets the arguments as written
$pre->macros->define('MAX', fn(string $a, string $b): string
    => "(($a) > ($b) ? ($a) : ($b))"
);

> $pre = new Preprocessor(clock: $myClock);
> 

$pre = new FFI\Preprocessor\Preprocessor();

// Define a file by its contents
$pre->files->add('config.h', '#define VERSION 2');
$pre->files->add('example.h', new SplFileInfo('/path/to/file.h'));
$pre->files->add('some.h', fopen('/path/to/stream.h'));

// Make a real file visible under a virtual name
$pre->files->link('gl.h', '/usr/

use FFI\Preprocessor\Diagnostic\DiagnosticListener\StreamDiagnosticListener;

$pre = new Preprocessor(diagnostic: new StreamDiagnosticListener(STDERR));

// {entrypoint}:1:1: warning: #warning careful

use FFI\Preprocessor\Directive\DirectiveContext;
use FFI\Preprocessor\Directive\DirectiveHandlerInterface;
use FFI\Preprocessor\Runtime\Chunk\TaggedChunk\DirectiveChunk;
use FFI\Preprocessor\Runtime\Chunk\TaggedChunk\GeneratedChunk;

$pre->directives->add('note', new class implements DirectiveHandlerInterface {
    public function handle(DirectiveChunk $directive, DirectiveContext $context): iterable
    {
        yield new GeneratedChunk("/* {$directive->body} */\n", $directive->source, 0);
    }
});

// #note hi   =>   /* hi */

use FFI\Preprocessor\Directive\Handler\PragmaDirectiveHandler;
use FFI\Preprocessor\Directive\Handler\PragmaDirectiveHandler\OncePragmaProcessor;
use FFI\Preprocessor\Directive\Handler\PragmaDirectiveHandler\PragmaProcessorInterface;

final class PackPragmaProcessor implements PragmaProcessorInterface
{
    public function supports(string $body): bool
    {
        return \str_starts_with($body, 'pack');
    }

    public function apply(DirectiveChunk $directive, DirectiveContext $context): iterable
    {
        return []; // `#pragma pack(...)` leaves nothing in the output
    }
}

$pre->directives->replace('pragma', new PragmaDirectiveHandler([
    new OncePragmaProcessor(),
    new PackPragmaProcessor(),
]));