if statements work

This commit is contained in:
SpookyDervish
2025-10-14 21:23:11 +11:00
parent 48e7488a63
commit 3d9208f0f8
7 changed files with 256 additions and 34 deletions

32
AST.py
View File

@@ -12,6 +12,7 @@ class NodeType(Enum):
FunctionStatement = "FunctionStatement"
BlockStatement = "BlockStatement"
ReturnStatement = "ReturnStatement"
IfStatement = "IfStatement"
# Expressions
InfixExpression = "InfixExpression"
@@ -20,6 +21,7 @@ class NodeType(Enum):
IntegerLiteral = "IntegerLiteral"
FloatLiteral = "FloatLiteral"
IdentifierLiteral = "IdentifierLiteral"
BooleanLiteral = "BooleanLiteral"
class Node:
@abstractmethod
@@ -88,6 +90,19 @@ class IdentifierLiteral(Expression):
"type": self.type().value,
"value": self.value
}
class BooleanLiteral(Expression):
def __init__(self, value: bool = None) -> None:
self.value: bool = value
def type(self) -> NodeType:
return NodeType.BooleanLiteral
def json(self) -> dict:
return {
"type": self.type().value,
"value": self.value
}
# endregion
# region Statements
@@ -180,6 +195,23 @@ class ReassignStatement(Statement):
"ident": self.ident.json(),
"right_value": self.right_value.json()
}
class IfStatement(Statement):
def __init__(self, condition: Expression = None, consequence: BlockStatement = None, alternative: BlockStatement = None) -> None:
self.condition = condition
self.consequence = consequence
self.alternative = alternative
def type(self) -> NodeType:
return NodeType.IfStatement
def json(self) -> dict:
return {
"type": self.type().value,
"condition": self.condition.json(),
"consequence": self.consequence.json(),
"alternative": self.alternative.json() if self.alternative is not None else None
}
# endregion
# region Expressions