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

@@ -23,6 +23,7 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
case BoundStatementKind::Return: return this -> EvaluateReturnStatement((BoundReturnStatement*)statement);
case BoundStatementKind::Conditional: return this -> EvaluateConditionalStatement((BoundConditionalStatement*)statement);
case BoundStatementKind::Bad:
throw;
@@ -76,6 +77,18 @@ void Evaluator::EvaluateReturnStatement(BoundReturnStatement* statement){
this -> _returnValue = value;
}
void Evaluator::EvaluateConditionalStatement(BoundConditionalStatement *statement) {
auto condition = statement->GetCondition();
if (EvaluateBoolExpression(condition) -> EvaluateBool()){
this -> EvaluateStatement(statement->GetBlock());
} else{
auto elseStatement = statement -> GetElseStatement();
if (elseStatement != nullptr){
this->EvaluateStatement(elseStatement);
}
}
}
shared_ptr<EvalValue> Evaluator::EvaluateExpression(BoundExpression *expression) {
auto type = expression -> GetType();
switch (type->GetClass()){
@@ -216,4 +229,5 @@ shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(BoundExpression *expres
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
return shared_ptr<EvalValue>(indexable -> IndexValue(index.get()));
}
}