d-u-d-e / c-compiler

GNU General Public License v3.0
3 stars 0 forks source link

Define AST nodes for the parsing stage #15

Open d-u-d-e opened 1 week ago

albertoursino commented 1 week ago
class Program(TreeNode):
    def __init__(self):
        super().__init__()

class FunctionDefinition(TreeNode):
    def __init__(self, parent: Program, **kwargs):
        super().__init__(parent, kwargs)

class Statement(TreeNode):
    @abstractmethod
    def __init__(self, parent: FunctionDefinition, **kwargs):
        super().__init__(parent, kwargs)

class Return(Statement):
    def __init__(self, parent: FunctionDefinition, value: "Exp"):
        super().__init__(parent, value=value)

class Exp(TreeNode):
    @abstractmethod
    def __init__(self, parent: Return, **kwargs):
        super().__init__(parent, kwargs)

class Constant(Exp):
    def __init__(self, parent: Return, value: int):
        super().__init__(parent, value=value)

class Identifier(Exp):
    def __init__(self, parent: Return, value: str):
        super().__init__(parent, value=value)

I have specified the type of the parent node an AST node wants because I think it's more clear this way. For example: since the FunctionDefinition AST node can only have the Program AST node as a parent (for now), I have forced the __init__ method of FunctionDefinition to receive a Program AST node as a parent.

class FunctionDefinition(TreeNode):
    def __init__(self, parent: Program, **kwargs):
        super().__init__(parent, kwargs)

I would leave it like this, and we can generalize in the future. What do you think @d-u-d-e?

@CremaLuca would you pass the generic TreeNode type instead of a specific one? This looks like the principle of clean code you told me, but in this case I don't think it fully applies.

d-u-d-e commented 1 week ago

Not sure about it. This is not like a true type system. For example it may be used by your IDE for auto completion, etc. It needs to be updated frequently if we add more types (e.g. a constant can be integral, float, ...), so the risk is becoming stale.

d-u-d-e commented 1 week ago

I think there is a typo there: an identifier is not an expression.

albertoursino commented 1 week ago

True!

In ASDL, identifier is a built-in type that represents function and variable names; they’re basically strings, but we want to distinguish them from string literals like "Hello, World!" because they appear in different parts of an AST. Since identifier is a built-in type, it has no children

It's an AST node that extends from a TreeNode