<commit_before>from thinglang.compiler.references import Reference
from thinglang.lexer.operators.casts import LexicalCast
from thinglang.lexer.values.identifier import Identifier
from thinglang.parser.definitions.cast_tag import CastTag
from thinglang.parser.nodes 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


class CastOperation(BaseNode):
    """
    Explicitly cast from one type to another
    Expects a conversion method on the source class
    """

    def __init__(self, value, target_type: Identifier, stack_args=False):
        super().__init__([value])
        self.value, self.target_type, self.stack_args = value, target_type, stack_args

    def compile(self, context):
        actual_value = self.value.compile(context.optional())
        if actual_value.type.untyped == self.target_type.untyped:  # TODO: why is this untyped?
            return self.value.compile(context)
        return MethodCall(NamedAccess.extend(self.value, CastTag(self.target_type)), stack_args=self.stack_args).compile(context)

    @staticmethod
    @ParserRule.mark
    def parse_inline_cast_op(value: ValueType, _: LexicalCast, target_type: Identifier):
        return CastOperation(value, target_type)<commit_msg>Explicitly test whether a cast operation is necessary<commit_after>from thinglang.compiler.references import Reference
from thinglang.lexer.operators.casts import LexicalCast
from thinglang.lexer.values.identifier import Identifier
from thinglang.parser.definitions.cast_tag import CastTag
from thinglang.parser.nodes 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.symbols.symbol import Symbol
from thinglang.utils.type_descriptors import ValueType


class CastOperation(BaseNode):
    """
    Explicitly cast from one type to another
    Expects a conversion method on the source class
    """

    def __init__(self, value, target_type: Identifier, stack_args=False, stack_target=False):
        super().__init__([value])
        self.value, self.target_type, self.stack_args, self.stack_target = value, target_type, stack_args, stack_target

    def compile(self, context):
        if not self.necessary(context):
            return self.value.compile(context)

        return MethodCall(NamedAccess.extend(self.value, CastTag(self.target_type)), stack_args=self.stack_args, stack_target=self.stack_target).compile(context)

    def replace_references(self, replacements):
        return CastOperation(replacements[self.value], self.target_type, stack_args=self.stack_args)

    @staticmethod
    @ParserRule.mark
    def parse_inline_cast_op(value: ValueType, _: LexicalCast, target_type: Identifier):
        return CastOperation(value, target_type)

    def necessary(self, context):  #TODO test this
        actual_value = self.value if isinstance(self.value, Reference) else self.value.compile(context.optional())
        return actual_value.type.untyped != self.target_type.untyped #and actual_type.convention is not Symbol.INTERNAL  # TODO: why is this untyped?
{'fraction_non_alphanumeric': 0.08069858476362542, 'fraction_numerical': 0.0, 'mean_word_length': 4.7874564459930316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2718', 'n_tokens_mistral': 935, 'n_tokens_neox': 920, 'n_words': 232}