<commit_before>from thinglang.lexer.lexical_definitions import REVERSE_OPERATORS
from thinglang.lexer.operators.binary import LexicalAddition, LexicalSubtraction, LexicalMultiplication, \
    LexicalDivision, SecondOrderLexicalBinaryOperation, FirstOrderLexicalBinaryOperation
from thinglang.lexer.operators.comparison import LexicalEquals, LexicalGreaterThan, LexicalLessThan, LexicalComparison
from thinglang.parser.nodes.base_node import BaseNode
from thinglang.parser.rule import ParserRule
from thinglang.utils.type_descriptors import ValueType


class BinaryOperation(BaseNode, ValueType):
    """
    Represents binary operations (arithmetic and logic)
    """

    OPERATIONS = {
        LexicalAddition: lambda lhs, rhs: lhs + rhs,
        LexicalMultiplication: lambda lhs, rhs: lhs * rhs,
        LexicalSubtraction: lambda lhs, rhs: lhs - rhs,
        LexicalDivision: lambda lhs, rhs: lhs / rhs,
        LexicalEquals: lambda lhs, rhs: lhs == rhs,
        LexicalGreaterThan: lambda lhs, rhs: lhs > rhs,
        LexicalLessThan: lambda lhs, rhs: lhs < rhs
    }

    def __init__(self, operator, lhs, rhs):
        super(BinaryOperation, self).__init__([operator, lhs, rhs])
        self.lhs, self.rhs = lhs, rhs
        self.operator = type(operator)

    def evaluate(self):
        return self.OPERATIONS[self.operator](self.lhs.evaluate(), self.rhs.evaluate())
<commit_msg>Have BinaryOperation compile directly to a method call<commit_after>from thinglang.compiler.buffer import CompilationBuffer
from thinglang.lexer.lexical_definitions import REVERSE_OPERATORS
from thinglang.lexer.operators.binary import LexicalAddition, LexicalSubtraction, LexicalMultiplication, \
    LexicalDivision, SecondOrderLexicalBinaryOperation, FirstOrderLexicalBinaryOperation
from thinglang.lexer.operators.comparison import LexicalEquals, LexicalGreaterThan, LexicalLessThan, LexicalComparison
from thinglang.lexer.values.identifier import Identifier
from thinglang.parser.nodes.base_node import BaseNode
from thinglang.parser.rule import ParserRule
from thinglang.parser.values.method_call import MethodCall
from thinglang.parser.values.named_access import NamedAccess
from thinglang.utils.type_descriptors import ValueType, CallSite


class BinaryOperation(BaseNode, ValueType, CallSite):
    """
    Represents binary operations (arithmetic and logic)
    """

    OPERATIONS = {
        LexicalAddition: lambda lhs, rhs: lhs + rhs,
        LexicalMultiplication: lambda lhs, rhs: lhs * rhs,
        LexicalSubtraction: lambda lhs, rhs: lhs - rhs,
        LexicalDivision: lambda lhs, rhs: lhs / rhs,
        LexicalEquals: lambda lhs, rhs: lhs == rhs,
        LexicalGreaterThan: lambda lhs, rhs: lhs > rhs,
        LexicalLessThan: lambda lhs, rhs: lhs < rhs
    }

    def __init__(self, operator, lhs, rhs):
        super(BinaryOperation, self).__init__([operator, lhs, rhs])
        self.lhs, self.rhs = lhs, rhs
        self.operator = type(operator)

    def compile(self, context: CompilationBuffer):
        method_call = MethodCall(NamedAccess.extend(self.lhs, Identifier(self.operator.transpile())), [self.rhs])
        return method_call.compile(context)

    def evaluate(self):
        return self.OPERATIONS[self.operator](self.lhs.evaluate(), self.rhs.evaluate())

{'fraction_non_alphanumeric': 0.08495736906211937, 'fraction_numerical': 0.0, 'mean_word_length': 4.306946688206785, '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': '613', 'n_tokens_mistral': 976, 'n_tokens_neox': 949, 'n_words': 243}