<commit_before><?php namespace Jenny\Evaluator;

use Jenny\Lexer\Token;

class Evaluator {

    /**
     * The Container instance
     *
     * @var \Jenny\Evaluator\Container
     */
    protected $container;

    /**
     * The ExpressionBuilder instance
     *
     * @var \Jenny\Evaluator\ExpressionBuilder
     */
    protected $builder;

    /**
     * The constructor
     *
     * @param \Jenny\Evaluator\Container              $container
     * @param \Jenny\Evaluator\ExpressionBuilder|null $builder
     * @return Evaluator
     */
    public function __construct(Container $container,
        ExpressionBuilder $builder = null)
    {
        $this->container = $container;

        // nice trick, still testable but makes it easier to create an instance
        $this->builder = $builder ?: new ExpressionBuilder($container);
    }

    /**
     * Evaluate the given array of tokens
     *
     * @param  array        $tokens
     * @return mixed|null
     */
    public function evaluateLine(array $tokens)
    {
        if ($this->containsAssignment($tokens))
        {
            return $this->handleAssignment($tokens);
        }

        return $this->buildAndEvaluate($tokens);
    }

    /**
     * Make the expression compatible with the PHP syntax
     * So it can passed to eval()
     *
     * @param  string $code
     * @return string
     */
    public function makeCompatible($code)
    {
        // 2^2 => pow(2,2)
        $code = preg_replace(
            '/(-?[0-9\.]+)\^(-?[0-9]+)/', // could be simpler
            'pow($1,$2)',
            $code
        );

        return $code;
    }

    /**
     * Determine whether the given array of tokens contains an assignment
     *
     * @param  array   $tokens
     * @return boolean
     */
    protected function containsAssignment(array $tokens)
    {
        // foo = 23 + 10
        //     ^

        if ( ! isset($tokens[1])) return false;

        $token = $tokens[1];

        return $token->is(Token::OPERATOR) and $token->getValue() == '=';
    }

    /**
     * Create a new key in the container and calculate its value
     *
     * @param  array  $tokens
     * @return string
     */
    protected function handleAssignment(array $tokens)
    {
        // foo = 23 + 10
        // ^^^

        $key = $tokens[0]->getValue();

        $value = $this->buildAndEvaluate(array_slice($tokens, 2));

        $this->container->set($key, $value);

        return "[$key] equals to $value";
    }

    /**
     * Build an expression from the given array of tokens and evaluate it
     *
     * @param  array $tokens
     * @return mixed
     */
    protected function buildAndEvaluate(array $tokens)
    {
        $expression = $this->builder->build($tokens);

        return $this->evaluate($expression);
    }

    /**
     * Evaluate the given code using built-in eval() function
     *
     * @param  string $code
     * @return mixed
     */
    protected function evaluate($code)
    {
        $code = $this->makeCompatible($code);

        return eval("return $code;");
    }<commit_msg>refactor Evaluator<commit_after><?php namespace Jenny\Evaluator;

use Jenny\Lexer\Token;

class Evaluator {

    /**
     * The Container instance.
     *
     * @var Container
     */
    protected $container;

    /**
     * The ExpressionBuilder instance.
     *
     * @var ExpressionBuilder
     */
    protected $builder;

    /**
     * The constructor.
     *
     * @param Container $container
     * @param ExpressionBuilder|null $builder
     * @return Evaluator
     */
    public function __construct(Container $container, ExpressionBuilder $builder = null)
    {
        $this->container = $container;

        $this->builder = $builder ?: new ExpressionBuilder($container);
    }

    /**
     * Evaluate the given array of tokens.
     *
     * @param array $tokens
     * @return mixed
     */
    public function evaluateLine(array $tokens)
    {
        if ($this->containsAssignment($tokens))
        {
            return $this->handleAssignment($tokens);
        }

        return $this->buildAndEvaluate($tokens);
    }

    /**
     * Make the expression compatible with the PHP syntax so it can be eval()'ed.
     *
     * @param string $code
     * @return string
     */
    public function makeCompatible($code)
    {
        // 2^2 => pow(2,2)
        $code = \preg_replace('/(-?[0-9\.]+)\^(-?[0-9]+)/', 'pow($1,$2)', $code);

        return $code;
    }

    /**
     * Determine whether the given array of tokens contains an assignment.
     *
     * @param array $tokens
     * @return boolean
     */
    protected function containsAssignment(array $tokens)
    {
        // foo = 23 + 10
        //     ^

        if ( ! isset($tokens[1]))
        {
            return false;
        }

        $token = $tokens[1];

        return $token->is(Token::OPERATOR) and ($token->getValue() == '=');
    }

    /**
     * Create a new key in the container and calculate its value.
     *
     * @param array $tokens
     * @return string
     */
    protected function handleAssignment(array $tokens)
    {
        // foo = 23 + 10
        // ^^^

        $key = $tokens[0]->getValue();

        $value = $this->buildAndEvaluate(\array_slice($tokens, 2));

        $this->container->set($key, $value);

        return "[{$key}] equals to {$value}";
    }

    /**
     * Build an expression from the given array of tokens and evaluate it.
     *
     * @param array $tokens
     * @return mixed
     */
    protected function buildAndEvaluate(array $tokens)
    {
        $expression = $this->builder->build($tokens);

        return $this->evaluate($expression);
    }

    /**
     * Evaluate the given code using the built-in eval() function.
     *
     * @param string $code
     * @return mixed
     */
    protected function evaluate($code)
    {
        $code = $this->makeCompatible($code);

        return eval("return {$code};");
    }
{'fraction_non_alphanumeric': 0.13359099311040162, 'fraction_numerical': 0.0073937153419593345, 'mean_word_length': 1.9048316251830162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '3810469', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1676, 'n_words': 433}