Implements variable usage, tweaks and fixes for variable assignment
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
class EvalValue{
|
||||
public:
|
||||
EvalValue() = default;
|
||||
virtual ~EvalValue() = default;
|
||||
virtual ScriptType* GetType() = 0;
|
||||
|
||||
@@ -18,6 +19,8 @@ public:
|
||||
return ! (this->operator==(b));
|
||||
}
|
||||
|
||||
virtual EvalValue* Clone() = 0;
|
||||
|
||||
virtual long EvaluateInteger(){
|
||||
throw EvaluationException("Can't evaluate this EvalValue as integer.");
|
||||
}
|
||||
@@ -41,6 +44,10 @@ public:
|
||||
_type = new ScriptType(TypeClass::Bool);
|
||||
}
|
||||
|
||||
EvalValue* Clone() final{
|
||||
return new BooleanEvalValue(_value);
|
||||
}
|
||||
|
||||
~BooleanEvalValue() final{
|
||||
delete _type;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,10 @@ public:
|
||||
strs << _value;
|
||||
return strs.str();
|
||||
}
|
||||
|
||||
EvalValue* Clone() final{
|
||||
return new IntegerEvalValue(_value);
|
||||
}
|
||||
};
|
||||
|
||||
class FloatEvalValue : public NumericEvalValue{
|
||||
@@ -74,6 +78,10 @@ public:
|
||||
strs << _value;
|
||||
return strs.str();
|
||||
}
|
||||
|
||||
EvalValue* Clone() final{
|
||||
return new FloatEvalValue(_value);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //PORYGONLANG_NUMERICEVALVALUE_HPP
|
||||
|
||||
@@ -32,6 +32,9 @@ public:
|
||||
return _value;
|
||||
}
|
||||
|
||||
EvalValue* Clone() final{
|
||||
return new StringEvalValue(_value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
EvaluationScope::EvaluationScope(unordered_map<int, EvalValue *> *scriptVariables, int deepestScope) {
|
||||
_scriptScope = scriptVariables;
|
||||
_localScope = vector<unordered_map<int, EvalValue*>>(deepestScope);
|
||||
_currentScope = -1;
|
||||
}
|
||||
|
||||
EvaluationScope::~EvaluationScope() {
|
||||
@@ -27,5 +28,21 @@ void EvaluationScope::SetVariable(int scope, int id, EvalValue *value) {
|
||||
}
|
||||
|
||||
EvalValue *EvaluationScope::GetVariable(int scope, int id) {
|
||||
if (scope == 0){
|
||||
return _scriptScope->at(id);
|
||||
}
|
||||
return _localScope[scope - 1][id];
|
||||
}
|
||||
|
||||
void EvaluationScope::OuterScope() {
|
||||
_currentScope++;
|
||||
}
|
||||
|
||||
void EvaluationScope::InnerScope() {
|
||||
auto scope = this->_localScope[_currentScope];
|
||||
for (auto v: scope){
|
||||
delete v.second;
|
||||
}
|
||||
_currentScope--;
|
||||
|
||||
}
|
||||
|
||||
@@ -9,12 +9,15 @@
|
||||
class EvaluationScope {
|
||||
unordered_map<int, EvalValue*>* _scriptScope;
|
||||
vector<unordered_map<int, EvalValue*>> _localScope;
|
||||
int _currentScope;
|
||||
public:
|
||||
explicit EvaluationScope(unordered_map<int, EvalValue*>* scriptVariables, int deepestScope);
|
||||
~EvaluationScope();
|
||||
|
||||
void CreateVariable(int scope, int id, EvalValue* value);
|
||||
void SetVariable(int scope, int id, EvalValue* value);
|
||||
void OuterScope();
|
||||
void InnerScope();
|
||||
EvalValue* GetVariable(int scope, int id);
|
||||
};
|
||||
|
||||
|
||||
@@ -22,9 +22,11 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
|
||||
this->_evaluationScope->OuterScope();
|
||||
for (auto s: statement->GetStatements()){
|
||||
this -> EvaluateStatement(s);
|
||||
}
|
||||
this->_evaluationScope->InnerScope();
|
||||
}
|
||||
|
||||
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
|
||||
@@ -54,12 +56,17 @@ EvalValue *Evaluator::EvaluateExpression(BoundExpression *expression) {
|
||||
}
|
||||
}
|
||||
|
||||
EvalValue* Evaluator::GetVariable(BoundVariableExpression* expression){
|
||||
return this->_evaluationScope->GetVariable(expression->GetScope(), expression->GetId())->Clone();
|
||||
}
|
||||
|
||||
NumericEvalValue* Evaluator::EvaluateIntegerExpression(BoundExpression *expression) {
|
||||
switch (expression->GetKind()){
|
||||
case BoundExpressionKind ::LiteralInteger: return new IntegerEvalValue(((BoundLiteralIntegerExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind ::LiteralFloat: return new FloatEvalValue(((BoundLiteralFloatExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind::Unary: return this -> EvaluateIntegerUnary((BoundUnaryExpression*)expression);
|
||||
case BoundExpressionKind ::Binary: return this -> EvaluateIntegerBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (NumericEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
|
||||
case BoundExpressionKind ::LiteralString:
|
||||
case BoundExpressionKind ::LiteralBool:
|
||||
@@ -73,6 +80,7 @@ BooleanEvalValue* Evaluator::EvaluateBoolExpression(BoundExpression *expression)
|
||||
case BoundExpressionKind::LiteralBool: return new BooleanEvalValue(((BoundLiteralBoolExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind::Unary: return this -> EvaluateBooleanUnary((BoundUnaryExpression*)expression);
|
||||
case BoundExpressionKind::Binary: return this -> EvaluateBooleanBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (BooleanEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
@@ -89,6 +97,8 @@ StringEvalValue* Evaluator::EvaluateStringExpression(BoundExpression *expression
|
||||
return new StringEvalValue(((BoundLiteralStringExpression*)expression)->GetValue());
|
||||
case BoundExpressionKind::Binary:
|
||||
return this -> EvaluateStringBinary((BoundBinaryExpression*)expression);
|
||||
case BoundExpressionKind::Variable: return (StringEvalValue*)this->GetVariable((BoundVariableExpression*)expression);
|
||||
|
||||
|
||||
case BoundExpressionKind::Bad:
|
||||
case BoundExpressionKind::LiteralInteger:
|
||||
|
||||
@@ -35,6 +35,8 @@ class Evaluator {
|
||||
|
||||
NumericEvalValue* EvaluateIntegerUnary(BoundUnaryExpression* expression);
|
||||
BooleanEvalValue *EvaluateBooleanUnary(BoundUnaryExpression *expression);
|
||||
|
||||
EvalValue *GetVariable(BoundVariableExpression *expression);
|
||||
public:
|
||||
explicit Evaluator(Script* script){
|
||||
_scriptData = script;
|
||||
|
||||
Reference in New Issue
Block a user