<commit_before><?php

namespace PHPPHP;

use PHPPHP\Engine\Zval;

class PHP {

    protected $executor;

    public function __construct() {
        $functions = new Engine\FunctionStore;
        $constants = new Engine\ConstantStore;

        $this->executor = new Engine\Executor($functions, $constants);

        $this->registerCoreFunctions($functions);
        $this->registerCustomFunctions($functions);
        $this->registerCoreConstants($constants);
    }

    public function execute($code) {
        $opCodes = $this->executor->compile($code);
        $retval = $this->executor->execute($opCodes);
        if ($retval) {
            return $retval->value;
        }
        return null;
    }

    public function executeFile($file) {
        if (empty($file)) {
            return;
        }
        $this->executor->executorGlobals->cwd = dirname($file);
        $code = file_get_contents($file);
        $this->execute($code);
    }

    protected function registerCoreConstants(Engine\ConstantStore $constants) {
        $coreConstants = array(
            'caseInsensitive' => array(
                'null'  => null,
                'true'  => true,
                'false' => false,
            ),
            'caseSensitive' => array(
                'PHP_SAPI' => 'cli',
                'PHP_OS' => PHP_OS,
            ),
        );

        foreach ($coreConstants['caseInsensitive'] as $name => $value) {
            $constants->register($name, Zval::factory($value), false);
        }
        foreach ($coreConstants['caseSensitive'] as $name => $value) {
            $constants->register($name, Zval::factory($value));
        }

    }

    protected function registerCoreFunctions(Engine\FunctionStore $functions) {
        $coreFunctions = array(
            'implode',
            'join',
            'php_uname',
            'phpversion',
            'realpath',
            'strlen',
            'var_dump',
            'zend_version',
        );

        foreach ($coreFunctions as $funcName) {
            $functions->register($funcName, new Engine\FunctionData\Internal($funcName));
        }
    }

    protected function registerCustomFunctions(Engine\FunctionStore $functions) {
        $executor = $this->executor;
        $self = $this;
        $customFunctions = array(
            'get_cfg_var' => function($var = null) {
                if ($var) {
                    return null;
                } else {
                    return array();
                }
            },
            'get_loaded_extensions' => function() {
                return array();
            },
            'function_exists' => function($funcName) use ($executor) {
                return $executor->getFunctionStore()->exists($funcName);
            },
        );
<commit_msg>Add error reporting dummy function<commit_after><?php

namespace PHPPHP;

use PHPPHP\Engine\Zval;

class PHP {

    protected $executor;

    public function __construct() {
        $functions = new Engine\FunctionStore;
        $constants = new Engine\ConstantStore;

        $this->executor = new Engine\Executor($functions, $constants);

        $this->registerCoreFunctions($functions);
        $this->registerCustomFunctions($functions);
        $this->registerCoreConstants($constants);
    }

    public function execute($code) {
        $opCodes = $this->executor->compile($code);
        $retval = $this->executor->execute($opCodes);
        if ($retval) {
            return $retval->value;
        }
        return null;
    }

    public function executeFile($file) {
        if (empty($file)) {
            return;
        }
        $this->executor->executorGlobals->cwd = dirname($file);
        $code = file_get_contents($file);
        $this->execute($code);
    }

    protected function registerCoreConstants(Engine\ConstantStore $constants) {
        $coreConstants = array(
            'caseInsensitive' => array(
                'null'  => null,
                'true'  => true,
                'false' => false,
            ),
            'caseSensitive' => array(
                'PHP_SAPI' => 'cli',
                'PHP_OS' => PHP_OS,
            ),
        );

        foreach ($coreConstants['caseInsensitive'] as $name => $value) {
            $constants->register($name, Zval::factory($value), false);
        }
        foreach ($coreConstants['caseSensitive'] as $name => $value) {
            $constants->register($name, Zval::factory($value));
        }

    }

    protected function registerCoreFunctions(Engine\FunctionStore $functions) {
        $coreFunctions = array(
            'implode',
            'join',
            'php_uname',
            'phpversion',
            'realpath',
            'strlen',
            'var_dump',
            'zend_version',
        );

        foreach ($coreFunctions as $funcName) {
            $functions->register($funcName, new Engine\FunctionData\Internal($funcName));
        }
    }

    protected function registerCustomFunctions(Engine\FunctionStore $functions) {
        $executor = $this->executor;
        $self = $this;
        $customFunctions = array(
            'error_reporting' => function($level = 0) {
                return 0;
            },
            'get_cfg_var' => function($var = null) {
                if ($var) {
                    return null;
                } else {
                    return array();
                }
            },
            'get_loaded_extensions' => function() {
                return array();
            },
            'function_exists' => function($funcName) use ($executor) {
                return $executor->getFunctionStore()->exists($funcName);
            },
        );

{'fraction_non_alphanumeric': 0.13095238095238096, 'fraction_numerical': 0.00035014005602240897, 'mean_word_length': 1.7625725338491296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3697070', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1481, 'n_words': 279}