Implements indexing, currently can only be used with strings
This commit is contained in:
@@ -34,6 +34,10 @@ public:
|
||||
virtual std::string* EvaluateString(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as string.");
|
||||
}
|
||||
|
||||
virtual EvalValue* IndexValue(EvalValue* val){
|
||||
throw EvaluationException("Can't index this EvalValue");
|
||||
}
|
||||
};
|
||||
|
||||
class BooleanEvalValue : public EvalValue{
|
||||
|
||||
@@ -32,6 +32,12 @@ public:
|
||||
shared_ptr<EvalValue> Clone() final{
|
||||
return make_shared<StringEvalValue>(_value);
|
||||
}
|
||||
|
||||
EvalValue* IndexValue(EvalValue* val) final{
|
||||
// Porygon is 1-indexed, so we convert to that.
|
||||
auto l = val->EvaluateInteger() - 1;
|
||||
return new StringEvalValue(string(1, _value[l]));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpressio
|
||||
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return dynamic_pointer_cast<NumericEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
||||
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
||||
case BoundExpressionKind ::Index: return dynamic_pointer_cast<NumericEvalValue>(this->EvaluateIndexExpression(expression));
|
||||
|
||||
case BoundExpressionKind ::LiteralString:
|
||||
case BoundExpressionKind ::LiteralBool:
|
||||
@@ -104,6 +105,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *
|
||||
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return dynamic_pointer_cast<BooleanEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
||||
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
||||
case BoundExpressionKind ::Index: return dynamic_pointer_cast<BooleanEvalValue>(this->EvaluateIndexExpression(expression));
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
@@ -122,6 +124,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
|
||||
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return dynamic_pointer_cast<StringEvalValue>(this->GetVariable((BoundVariableExpression*)expression));
|
||||
case BoundExpressionKind ::FunctionCall: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateFunctionCallExpression(expression));
|
||||
case BoundExpressionKind ::Index: return dynamic_pointer_cast<StringEvalValue>(this->EvaluateIndexExpression(expression));
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
@@ -190,4 +193,11 @@ EvalValue* Evaluator::EvaluateFunction(ScriptFunctionEvalValue *function, vector
|
||||
}
|
||||
this->EvaluateBlockStatement(function->GetInnerBlock().get());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(BoundExpression *expression) {
|
||||
auto indexExpression = (BoundIndexExpression*)expression;
|
||||
auto index = this -> EvaluateExpression(indexExpression->GetIndexExpression());
|
||||
auto indexable = this -> EvaluateExpression(indexExpression->GetIndexableExpression());
|
||||
return shared_ptr<EvalValue>(indexable -> IndexValue(index.get()));
|
||||
}
|
||||
@@ -41,6 +41,7 @@ class Evaluator {
|
||||
shared_ptr<NumericEvalValue> EvaluateIntegerUnary(BoundUnaryExpression* expression);
|
||||
shared_ptr<BooleanEvalValue> EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
||||
shared_ptr<EvalValue> EvaluateFunctionCallExpression(BoundExpression *expression);
|
||||
shared_ptr<EvalValue> EvaluateIndexExpression(BoundExpression* expression);
|
||||
|
||||
shared_ptr<EvalValue> GetVariable(BoundVariableExpression *expression);
|
||||
public:
|
||||
@@ -56,10 +57,6 @@ public:
|
||||
void Evaluate(BoundScriptStatement* statement);
|
||||
EvalValue* EvaluateFunction(ScriptFunctionEvalValue* func, vector<EvalValue*> parameters);
|
||||
|
||||
EvaluationScope* GetScope(){
|
||||
return _evaluationScope;
|
||||
}
|
||||
|
||||
EvalValue* GetLastValue(){
|
||||
return _lastValue.get();
|
||||
}
|
||||
|
||||
@@ -34,4 +34,3 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBooleanUnary(BoundUnaryExpressio
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user