Implements if, elseif and else statements
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-08 14:25:15 +02:00
parent f4a3918947
commit e233616b8e
10 changed files with 187 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ BoundStatement* Binder::BindStatement(ParsedStatement* statement){
case ParsedStatementKind::Assignment: return this -> BindAssignmentStatement(statement);
case ParsedStatementKind ::FunctionDeclaration: return this->BindFunctionDeclarationStatement(statement);
case ParsedStatementKind::Return: return this -> BindReturnStatement(statement);
case ParsedStatementKind::Conditional: return this -> BindConditionalStatement(statement);
case ParsedStatementKind::Bad: return new BoundBadStatement();
}
@@ -131,6 +132,21 @@ BoundStatement *Binder::BindReturnStatement(ParsedStatement* statement){
return new BoundReturnStatement(boundExpression);
}
BoundStatement *Binder::BindConditionalStatement(ParsedStatement* statement) {
auto conditionalStatement = (ParsedConditionalStatement*)statement;
auto boundCondition = this -> BindExpression(conditionalStatement -> GetCondition());
if (boundCondition->GetType() -> GetClass() != TypeClass::Bool){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::ConditionNotABool, statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
}
auto boundBlock = this -> BindStatement(conditionalStatement->GetBlock());
BoundStatement* elseStatement = nullptr;
if (conditionalStatement->GetElseStatement() != nullptr){
elseStatement = this -> BindStatement(conditionalStatement->GetElseStatement());
}
return new BoundConditionalStatement(boundCondition, boundBlock, elseStatement);
}
BoundExpression* Binder::BindExpression(ParsedExpression* expression){
switch (expression -> GetKind()){
case ParsedExpressionKind ::LiteralInteger:
@@ -351,3 +367,4 @@ BoundExpression *Binder::BindIndexExpression(IndexExpression *expression) {
auto resultType = shared_ptr<ScriptType>(indexer->GetType()->GetIndexedType(index->GetType().get()));
return new BoundIndexExpression(indexer, index, resultType, expression->GetStartPosition(), expression->GetLength());
}

View File

@@ -20,6 +20,7 @@ class Binder {
BoundStatement *BindAssignmentStatement(ParsedStatement *statement);
BoundStatement *BindFunctionDeclarationStatement(ParsedStatement * statement);
BoundStatement *BindReturnStatement(ParsedStatement *statement);
BoundStatement *BindConditionalStatement(ParsedStatement *statement);
BoundExpression *BindExpression(ParsedExpression *expression);
BoundExpression *BindVariableExpression(VariableExpression *expression);

View File

@@ -18,6 +18,7 @@ enum class BoundStatementKind{
Assignment,
FunctionDeclaration,
Return,
Conditional,
};
class BoundStatement{
@@ -135,6 +136,41 @@ public:
}
};
class BoundConditionalStatement : public BoundStatement{
BoundExpression* _condition;
BoundStatement* _block;
BoundStatement* _elseStatement;
public:
explicit BoundConditionalStatement(BoundExpression* condition, BoundStatement* block, BoundStatement* next){
_condition = condition;
_block = block;
_elseStatement = next;
}
~BoundConditionalStatement() final{
delete _condition;
delete _block;
delete _elseStatement;
}
BoundStatementKind GetKind() final{
return BoundStatementKind ::Conditional;
}
BoundExpression* GetCondition(){
return _condition;
}
BoundStatement* GetBlock(){
return _block;
}
BoundStatement* GetElseStatement(){
return _elseStatement;
}
};
#include "BoundFunctionDeclarationStatement.hpp"
#endif //PORYGONLANG_BOUNDSTATEMENT_HPP