if statements work
This commit is contained in:
32
AST.py
32
AST.py
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user