# Path: src/som/interpreter/ast/nodes/dispatch.py
# def lookup_and_send(receiver, selector_string, arguments, universe):
#     selector = universe.symbol_for(selector_string)
#     invokable = receiver.get_class(universe).lookup_invokable(selector)
#     return invokable.invoke(receiver, arguments)
# 
# Path: src/som/interpreter/ast/nodes/expression_node.py
# class ExpressionNode(Node):
# 
#     def __init__(self, source_section):
#         Node.__init__(self, source_section)
# 
#     def is_super_node(self):
#         return False


from .dispatch import lookup_and_send
from .expression_node import ExpressionNode
from som.vm.globals import nilObject, trueObject, falseObject


class UninitializedGlobalReadNode(ExpressionNode):

    _immutable_fields_ = ["_global_name", "_universe"]

    def __init__(self, global_name, universe, source_section = None):
        ExpressionNode.__init__(self, source_section)
        self._global_name = global_name
        self._universe    = universe

    def execute(self, frame):
        if self._universe.has_global(self._global_name):
            return self._specialize().execute(frame)
        else:
            return self.send_unknown_global(
                frame.get_self(), self._global_name, self._universe)

    @staticmethod
    def send_unknown_global(receiver, global_name, universe):
        arguments = [global_name]

# Path: src/som/interpreter/ast/nodes/dispatch.py
# def lookup_and_send(receiver, selector_string, arguments, universe):
#     selector = universe.symbol_for(selector_string)
#     invokable = receiver.get_class(universe).lookup_invokable(selector)
#     return invokable.invoke(receiver, arguments)
# 
# Path: src/som/interpreter/ast/nodes/expression_node.py
# class ExpressionNode(Node):
# 
#     def __init__(self, source_section):
#         Node.__init__(self, source_section)
# 
#     def is_super_node(self):
#         return False


# Path: src/som/interpreter/ast/nodes/global_read_node.py
from .dispatch import lookup_and_send
from .expression_node import ExpressionNode
from som.vm.globals import nilObject, trueObject, falseObject



class UninitializedGlobalReadNode(ExpressionNode):

    _immutable_fields_ = ["_global_name", "_universe"]

    def __init__(self, global_name, universe, source_section = None):
        ExpressionNode.__init__(self, source_section)
        self._global_name = global_name
        self._universe    = universe

    def execute(self, frame):
        if self._universe.has_global(self._global_name):
            return self._specialize().execute(frame)
        else:
            return self.send_unknown_global(
                frame.get_self(), self._global_name, self._universe)

    @staticmethod
    def send_unknown_global(receiver, global_name, universe):
        arguments = [global_name]

        return lookup_and_send(receiver, "unknownGlobal:", arguments, universe)