from Type import *

class Function:
    def __init__(self):
        self.isBody = False
        self.isComplete = False
        self.isSignature = False
        self.name = None
        self.parameters = []
        self.returnType = None
        self.statements = []

    def parseLine(self, ln):
        parts = ln.split(" ")
        count = len(parts)
        lastPart = parts[count - 1]

        # Beginning of signature.
        if (
                self.returnType is None and
                self.name is None
                and lastPart.endswith("(")
        ):
            self.isSignature = True

        # End of parameters.
        if (
                self.isSignature and
                ln.endswith(") {")
        ):
            self.isSignature = False
            isBody = True

#        # Parameter.
#        if (
#            isSignature and
#            not ln.endswith(") {")
#        ):
#              print(f"TODO argument: '{ln}'")

        # Return type and name.
        if (
            self.isSignature and
            lastPart.endswith("(")
        ):
            self.name = lastPart[:-1]
            rawReturnType = ln[:-len(lastPart) - 1] # -1 for space before function name
            self.returnType = Type(rawReturnType)

    def __repr__(self):
        return self.__str__()
    def __str__(self):
        return f"Function(n/rt: '{self.name}'/'{self.returnType}')"
