<commit_before>// == Evaluates Expressions ==
// Perhaps add config item to specify optimizations
// This first version will be a preliminary test
//  and won't have very many features
// Class operators and tokens  will be abstracted so
//  changed to `class` won't be deterimental to the
//  existing code.
// This will also allow classes to be forged for testing

// == Info ==
// config:link contains class linktage to
//  primitives, which functions as a basic
//  abstraction layer between the expression
//  parser and the tokenizer.
//   The CheddarClass itself provides   a
//   more thougrough abstraction between
//   the tokenizer and the rest of the code
//   itself

// Primitive <-> Class Links
import {PRIMITIVE_LINKS} from '../config/link';
import {TYPE as OP_TYPE} from '../../../tokenizer/consts/ops';

// Reference tokens
import CheddarPropertyToken from '../../../tokenizer/parsers/property';
import CheddarLiteral from '../../../tokenizer/parsers/any';
import CheddarOperatorToken from '../../../tokenizer/literals/op';
import CheddarArrayToken from '../../../tokenizer/parsers/array';
import CheddarVariableToken from '../../../tokenizer/literals/var';

import CheddarScope from '../env/scope';

import CheddarFunction from '../env/func';

import CheddarVariable from '../env/var';
import CheddarClass from '../env/class';
import NIL from '../consts/nil';

// Call stack wrapper
import CheddarCallStack from './callstack';

// Standard Error class
import CheddarError from '../consts/err';
import CheddarErrorDesc from '../consts/err_msg';

function to_value(variable, parent) {
    // Check if getter
    if (variable.Value) {
        return variable.Value;
    } else if (variable.getter) {
        return variable.getter.exec([], parent);
    } else {
        // ERROR INTEGRATE
        return `Attempted to accesses variable without value`;
    }
}

function set_value(value, child) {
    // The CheddarVariable() wrapping the value
    let variable = value.scope.accessor(value.Reference);

    // If the result is being set to a variable
    if (child instanceof CheddarVariable) {
        child = child.Value; // extract it's literal value
    }

    // If there's a setter
    if (variable.setter !== null) {
        // Run the setter.
        // Pass the target value (child) as an arg
        // Run in context of value (`self`)
        child = variable.setter.exec([child], value);
    }

    // Set the correct reference on the scope
    child.scope = value.scope;
    child.Reference = value.Reference;

    // Get the scope the LHS is in.
    let rep = value.scope.manage(
        // Change the var name
        value.Reference,
        // to the resulting value
        new CheddarVariable(child, {
            Writeable: true,
            StrictType: variable.StrictType
        })
    );

    if (rep !== true) {
        // ERROR INTEGRATE
        return `\`${value.Reference}\` is a reserved keyword`;
    }
}

export default class CheddarEval extends CheddarCallStack {
    // To iterativaley evaluate the expression
    //  individual repeated steps would be taken
    //  which would also allow the debugger to
    //  function on the same foundation
    step() {
        const Operation = this.next();

        let OPERATOR,
            TOKEN,
            DATA,
            NAME,
            REFERENCE = null;

        // Handle Operator
        if (Operation instanceof CheddarOperatorToken) {

            TOKEN = this.shift(); // Get the value to operate upon

            // SPECIAL BEHAVIOR FOR REsASSIGNMENT
            if (Operation.tok(0) === "=") {
                DATA = this.shift();

                if ((
                    !(DATA.scope instanceof CheddarScope) ||
                    DATA.Reference === null
                ) || Operation.tok(1) === OP_TYPE.UNARY) {
                    return CheddarErrorDesc.get(CheddarError.NOT_A_REFERENCE);
                }

                if (DATA.scope.accessor(DATA.Reference).Writeable === false) {
                    // ERROR INTEGRATE
                    return `Cannot override constant ${DATA.Reference}`;
                }

                // Call `set_value` function
                DATA = set_value(DATA, TOKEN);

                // If it errored
                if (DATA !== true) {
                    return DATA;
                }

                OPERATOR = TOKEN;

            } else if (!TOKEN.constructor.Operator.has(Operation.Tokens[0])) {
                // Ensure behavior exists for the types
                OPERATOR = CheddarError.NO_OP_BEHAVIOR;
            } else if (Operation.Tokens[1] === OP_TYPE.UNARY) {
                // Check if unary or binary operator, then execute
                OPERATOR = (
                    TOKEN.constructor.Operator ||
                    TOKEN.Operator
                ).get(Operation.Tokens[0])(null, TOKEN);
            } else {
                DATA = this.shift(); // Get the other arg
                OPERATOR = (
                    DATA.constructor.Operator ||
                    DATA.Operator
                ).get(Operation.Tokens[0])(DATA, TOKEN);<commit_msg>fix [binary]: dyads' op behavior was unensured<commit_after>// == Evaluates Expressions ==
// Perhaps add config item to specify optimizations
// This first version will be a preliminary test
//  and won't have very many features
// Class operators and tokens  will be abstracted so
//  changed to `class` won't be deterimental to the
//  existing code.
// This will also allow classes to be forged for testing

// == Info ==
// config:link contains class linktage to
//  primitives, which functions as a basic
//  abstraction layer between the expression
//  parser and the tokenizer.
//   The CheddarClass itself provides   a
//   more thougrough abstraction between
//   the tokenizer and the rest of the code
//   itself

// Primitive <-> Class Links
import {PRIMITIVE_LINKS} from '../config/link';
import {TYPE as OP_TYPE} from '../../../tokenizer/consts/ops';

// Reference tokens
import CheddarPropertyToken from '../../../tokenizer/parsers/property';
import CheddarLiteral from '../../../tokenizer/parsers/any';
import CheddarOperatorToken from '../../../tokenizer/literals/op';
import CheddarArrayToken from '../../../tokenizer/parsers/array';
import CheddarVariableToken from '../../../tokenizer/literals/var';

import CheddarScope from '../env/scope';

import CheddarFunction from '../env/func';

import CheddarVariable from '../env/var';
import CheddarClass from '../env/class';
import NIL from '../consts/nil';

// Call stack wrapper
import CheddarCallStack from './callstack';

// Standard Error class
import CheddarError from '../consts/err';
import CheddarErrorDesc from '../consts/err_msg';

function to_value(variable, parent) {
    // Check if getter
    if (variable.Value) {
        return variable.Value;
    } else if (variable.getter) {
        return variable.getter.exec([], parent);
    } else {
        // ERROR INTEGRATE
        return `Attempted to accesses variable without value`;
    }
}

function set_value(value, child) {
    // The CheddarVariable() wrapping the value
    let variable = value.scope.accessor(value.Reference);

    // If the result is being set to a variable
    if (child instanceof CheddarVariable) {
        child = child.Value; // extract it's literal value
    }

    // If there's a setter
    if (variable.setter !== null) {
        // Run the setter.
        // Pass the target value (child) as an arg
        // Run in context of value (`self`)
        child = variable.setter.exec([child], value);
    }

    // Set the correct reference on the scope
    child.scope = value.scope;
    child.Reference = value.Reference;

    // Get the scope the LHS is in.
    let rep = value.scope.manage(
        // Change the var name
        value.Reference,
        // to the resulting value
        new CheddarVariable(child, {
            Writeable: true,
            StrictType: variable.StrictType
        })
    );

    if (rep !== true) {
        // ERROR INTEGRATE
        return `\`${value.Reference}\` is a reserved keyword`;
    }
}

export default class CheddarEval extends CheddarCallStack {
    // To iterativaley evaluate the expression
    //  individual repeated steps would be taken
    //  which would also allow the debugger to
    //  function on the same foundation
    step() {
        const Operation = this.next();

        let OPERATOR,
            TOKEN,
            DATA,
            NAME,
            REFERENCE = null;

        // Handle Operator
        if (Operation instanceof CheddarOperatorToken) {

            TOKEN = this.shift(); // Get the value to operate upon

            // SPECIAL BEHAVIOR FOR REsASSIGNMENT
            if (Operation.tok(0) === "=") {
                DATA = this.shift();

                if ((
                    !(DATA.scope instanceof CheddarScope) ||
                    DATA.Reference === null
                ) || Operation.tok(1) === OP_TYPE.UNARY) {
                    return CheddarErrorDesc.get(CheddarError.NOT_A_REFERENCE);
                }

                if (DATA.scope.accessor(DATA.Reference).Writeable === false) {
                    // ERROR INTEGRATE
                    return `Cannot override constant ${DATA.Reference}`;
                }

                // Call `set_value` function
                DATA = set_value(DATA, TOKEN);

                // If it errored
                if (DATA !== true) {
                    return DATA;
                }

                OPERATOR = TOKEN;

            } else if (Operation.Tokens[1] === OP_TYPE.UNARY) {
                // It is an Unary operator use TOKEN as RHS, null as LHS
                if (TOKEN.constructor.Operator.has(Operation.Tokens[0])) {
                    OPERATOR = (
                        TOKEN.constructor.Operator ||
                        TOKEN.Operator
                    ).get(Operation.Tokens[0])(null, TOKEN);
                } else {
                    OPERATOR = CheddarError.NO_OP_BEHAVIOR;
                }
            } else {
                // Binary operator. DATA is RHS, TOKEN is LHS
                DATA = this.shift(); // Get the other arg

                if (DATA.constructor.Operator.has(Operation.Tokens[0])) {
                    OPERATOR = (
                        DATA.constructor.Operator ||
                        DATA.Operator
                    ).get(Operation.Tokens[0])(DATA, TOKEN);
                } else {
                    OPERATOR = CheddarError.NO_OP_BEHAVIOR;
                }
{'fraction_non_alphanumeric': 0.1093454407869845, 'fraction_numerical': 0.0012296632614453272, 'mean_word_length': 1.830032119914347, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3499468', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2778, 'n_words': 882}