<reponame>mastersneeza/Timid
#imports
from TokenType import *
from Types import *

#begin Interpreter class
class Interpreter:
    #Utility functions

    #define evaluate function
    def evaluate(self, node):
        methodName = node.__class__.__name__ #Get method name
        method = getattr(self, methodName, self.noVisit) #Get method
        return method(node) #Return method's result
    #end evaluate function

    #define interpret function
    def interpret(self, expr):
        #begin try
        try:
            result = self.evaluate(expr)
            print(result)
        except Error.RTError as e: #Report any runtime errors
            Error.reportRTError(e)
        except RecursionError: #Catch recursion errors
            Error.reportRTError(Error.RecursionError(expr.pos_start, expr.pos_end, "Maximum recursion depth exceeded"))
        #end try
    #end interpret function

    #define noVisit function
    def noVisit(self, node):
        #begin raise
        raise Exception(f"No visit method defined for node {node.__class__.__name__}")
        #end raise
    #end noVisit function

    #Expressions

    #define BinaryNode function
    def BinaryNode(self, node): #Binary operations
        left = self.evaluate(node.left) #Evaluate left and right operands
        right = self.evaluate(node.right)

        operator = node.operator.type #Get the operator type

        result, error = None, None

        #Checking operator's type and seeing if there are any matches
        operations = {
            PLUS: left.opAdd
        }

        func = operations.get(operator, None) #Get function

        #begin if
        if func == None: #If there is no function associated with the operator raise an error
            result, error = None, Error.RTError(node.operator.pos_start, node.operator.pos_end, f"Operator '{operator}' has not been implemented")
        else:
            result, error = func(right)
        #end if

        #begin if
        if error: #If error raise it
            #begin raise
            raise error
            #end raise
        #end if
        
        return result.setPos(node.pos_start, node.pos_end)

    #end BinaryNode function

    #define LiteralNode function
    def LiteralNode(self, node):
        #begin if
        if node.token.type == INT: #If int return Integer
            result =  Integer(int(node.token.lexeme))
        elif node.token.type == FLOAT: #If float return Float
            result = Float(float(node.token.lexeme))
        #end if

        return result.setPos(node.pos_start, node.pos_end) #Set literal's position

    #end LiteralNode function
#end Interpreter class
#imports
from TokenType import *
from Types import *

#begin Interpreter class
class Interpreter:
    #Utility functions

    #define evaluate function
    def evaluate(self, node):
        methodName = node.__class__.__name__ #Get method name
        method = getattr(self, methodName, self.noVisit) #Get method
        return method(node) #Return method's result
    #end evaluate function

    #define interpret function
    def interpret(self, expr):
        #begin try
        try:
            result = self.evaluate(expr)
            print(result)
        except Error.RTError as e: #Report any runtime errors
            Error.reportRTError(e)
        except RecursionError: #Catch recursion errors
            Error.reportRTError(Error.RecursionError(expr.pos_start, expr.pos_end, "Maximum recursion depth exceeded"))
        #end try
    #end interpret function

    #define noVisit function
    def noVisit(self, node):
        #begin raise
        raise Exception(f"No visit method defined for node {node.__class__.__name__}")
        #end raise
    #end noVisit function

    #Expressions

    #define BinaryNode function
    def BinaryNode(self, node): #Binary operations
        left = self.evaluate(node.left) #Evaluate left and right operands
        right = self.evaluate(node.right)

        operator = node.operator.type #Get the operator type

        result, error = None, None

        #Checking operator's type and seeing if there are any matches
        operations = {
            PLUS: left.opAdd
        }

        func = operations.get(operator, None) #Get function

        #begin if
        if func == None: #If there is no function associated with the operator raise an error
            result, error = None, Error.RTError(node.operator.pos_start, node.operator.pos_end, f"Operator '{operator}' has not been implemented")
        else:
            result, error = func(right)
        #end if

        #begin if
        if error: #If error raise it
            #begin raise
            raise error
            #end raise
        #end if
        
        return result.setPos(node.pos_start, node.pos_end)

    #end BinaryNode function

    #define LiteralNode function
    def LiteralNode(self, node):
        #begin if
        if node.token.type == INT: #If int return Integer
            result =  Integer(int(node.token.lexeme))
        elif node.token.type == FLOAT: #If float return Float
            result = Float(float(node.token.lexeme))
        #end if

        return result.setPos(node.pos_start, node.pos_end) #Set literal's position

    #end LiteralNode function
#end Interpreter class
#imports #begin Interpreter class #Utility functions #define evaluate function #Get method name #Get method #Return method's result #end evaluate function #define interpret function #begin try #Report any runtime errors #Catch recursion errors #end try #end interpret function #define noVisit function #begin raise #end raise #end noVisit function #Expressions #define BinaryNode function #Binary operations #Evaluate left and right operands #Get the operator type #Checking operator's type and seeing if there are any matches #Get function #begin if #If there is no function associated with the operator raise an error #end if #begin if #If error raise it #begin raise #end raise #end if #end BinaryNode function #define LiteralNode function #begin if #If int return Integer #If float return Float #end if #Set literal's position #end LiteralNode function #end Interpreter class