61 lines
2.2 KiB
C++
61 lines
2.2 KiB
C++
|
|
#include "Evaluator.hpp"
|
|
#include "EvaluationException.hpp"
|
|
#include "../Script.hpp"
|
|
|
|
void Evaluator::Evaluate(BoundScriptStatement *statement) {
|
|
EvaluateBlockStatement(statement);
|
|
}
|
|
|
|
void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
|
switch (statement->GetKind()){
|
|
case BoundStatementKind ::Script: throw; // Should never happen
|
|
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement);
|
|
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
|
|
}
|
|
}
|
|
|
|
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
|
|
for (auto s: statement->GetStatements()){
|
|
this -> EvaluateStatement(s);
|
|
}
|
|
}
|
|
|
|
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
|
|
this->_scriptData->_lastValue = this -> EvaluateExpression(statement->GetExpression());
|
|
}
|
|
|
|
EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
|
|
auto type = expression -> GetType();
|
|
switch (type->GetClass()){
|
|
case TypeClass ::Number: return this -> EvaluateIntegerExpression(expression);
|
|
default: throw;
|
|
}
|
|
}
|
|
|
|
NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
|
|
auto exprType = expression->GetType();
|
|
if (exprType->GetClass() != TypeClass::Number){
|
|
throw EvaluationException("Can't evaluate expression as integer, it will not return a number.");
|
|
}
|
|
switch (expression->GetKind()){
|
|
case BoundExpressionKind ::LiteralInteger: return new IntegerEvalValue(((BoundLiteralIntegerExpression*)expression)->GetValue());
|
|
case BoundExpressionKind ::LiteralFloat: return new FloatEvalValue(((BoundLiteralFloatExpression*)expression)->GetValue());
|
|
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
|
|
|
case BoundExpressionKind ::LiteralString:
|
|
case BoundExpressionKind ::LiteralBool:
|
|
case BoundExpressionKind ::Bad:
|
|
throw;
|
|
}
|
|
}
|
|
|
|
EvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression) {
|
|
return nullptr;
|
|
}
|
|
|
|
EvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression) {
|
|
return nullptr;
|
|
}
|
|
|