Implements complex tables
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-06-12 15:19:28 +02:00
parent ba4fe888fa
commit c022c91777
21 changed files with 272 additions and 50 deletions

View File

@@ -4,6 +4,7 @@
#include <string>
#include "EvalValue.hpp"
#include "../../Utilities/HashedString.hpp"
using namespace std;
@@ -14,8 +15,8 @@ class StringEvalValue : public EvalValue{
public:
explicit StringEvalValue(string s){
_value = move(s);
_type = std::make_shared<ScriptType>(TypeClass::String);
_hash = std::hash<string>{}(_value);
_hash = HashedString::ConstHash (_value.c_str());
_type = std::make_shared<StringScriptType>(true, _hash);
}
std::shared_ptr<ScriptType> GetType() final{

View File

@@ -1,5 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_TABLEEVALVALUE_HPP
#define PORYGONLANG_TABLEEVALVALUE_HPP
#include <utility>
@@ -45,6 +43,11 @@ public:
auto hash = val->GetHashCode();
return this -> _table->at(hash);
}
shared_ptr<EvalValue> IndexValue(const char* val){
auto hash = HashedString::ConstHash(val);
return this -> _table -> at(hash);
}
};

View File

@@ -2,7 +2,7 @@
#include "EvaluationScope.hpp"
#include <memory>
EvaluationScope::EvaluationScope(unordered_map<int, shared_ptr<EvalValue>> *scriptVariables, int deepestScope) {
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int deepestScope) {
_scriptScope = scriptVariables;
_localScope = vector<shared_ptr<unordered_map<int, shared_ptr<EvalValue>>>>(deepestScope);
for (int i = 0; i < deepestScope; i++){
@@ -42,6 +42,7 @@ EvaluationScope* EvaluationScope::CreateBranchingScope(int index){
for (int i = 0; i < index; i++){
scope->_localScope[i] = this->_localScope[i];
}
scope->_currentScope = index;
return scope;
}
@@ -49,9 +50,11 @@ void EvaluationScope::OuterScope() {
_currentScope++;
}
void EvaluationScope::InnerScope() {
auto scope = this->_localScope[_currentScope];
scope->clear();
void EvaluationScope::InnerScope(bool clearScope) {
if (clearScope){
auto scope = this->_localScope[_currentScope];
scope->clear();
}
_currentScope--;
}

View File

@@ -7,17 +7,17 @@
#include "../EvalValues/EvalValue.hpp"
class EvaluationScope {
unordered_map<int, shared_ptr<EvalValue>>* _scriptScope;
unordered_map<size_t, shared_ptr<EvalValue>>* _scriptScope;
vector<shared_ptr<unordered_map<int, shared_ptr<EvalValue>>>> _localScope;
int _currentScope;
public:
explicit EvaluationScope(unordered_map<int, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
explicit EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
~EvaluationScope() = default;
void CreateVariable(int scope, int id, const shared_ptr<EvalValue>& value);
void SetVariable(int scope, int id, const shared_ptr<EvalValue>& value);
void OuterScope();
void InnerScope();
void InnerScope(bool clearScope = true);
shared_ptr<EvalValue> GetVariable(int scope, int id);
EvaluationScope* CreateBranchingScope(int index);

View File

@@ -6,12 +6,14 @@
#include "EvaluationScope/EvaluationScope.hpp"
#include "EvalValues/ScriptFunctionEvalValue.hpp"
#include "EvalValues/TableEvalValue.hpp"
#include "../Binder/BoundExpressions/BoundTableExpression.hpp"
#include "../TableScriptType.hpp"
using namespace std;
void Evaluator::Evaluate(BoundScriptStatement *statement) {
this->_evaluationScope = new EvaluationScope(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
EvaluateBlockStatement(statement);
EvaluateBlockStatement(statement, false);
}
void Evaluator::EvaluateStatement(BoundStatement *statement) {
@@ -19,7 +21,7 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
return;
switch (statement->GetKind()){
case BoundStatementKind ::Script: throw; // Should never happen
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement);
case BoundStatementKind ::Block: return this -> EvaluateBlockStatement((BoundBlockStatement*)statement, true);
case BoundStatementKind ::Expression: return this -> EvaluateExpressionStatement((BoundExpressionStatement*)statement);
case BoundStatementKind ::Assignment: return this -> EvaluateAssignmentStatement((BoundAssignmentStatement*)statement);
case BoundStatementKind ::FunctionDeclaration: return this->EvaluateFunctionDeclarationStatement((BoundFunctionDeclarationStatement*)statement);
@@ -31,14 +33,14 @@ void Evaluator::EvaluateStatement(BoundStatement *statement) {
}
}
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement) {
void Evaluator::EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope) {
this->_evaluationScope->OuterScope();
for (auto s: statement->GetStatements()){
this -> EvaluateStatement(s);
if (this->_hasReturned)
break;
}
this->_evaluationScope->InnerScope();
this->_evaluationScope->InnerScope(clearScope);
}
void Evaluator::EvaluateExpressionStatement(BoundExpressionStatement *statement) {
@@ -122,6 +124,7 @@ shared_ptr<NumericEvalValue> Evaluator::EvaluateIntegerExpression(BoundExpressio
case BoundExpressionKind ::LiteralBool:
case BoundExpressionKind ::Bad:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
}
@@ -140,6 +143,7 @@ shared_ptr<BooleanEvalValue> Evaluator::EvaluateBoolExpression(BoundExpression *
case BoundExpressionKind::LiteralFloat:
case BoundExpressionKind::LiteralString:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
@@ -161,6 +165,7 @@ shared_ptr<StringEvalValue> Evaluator::EvaluateStringExpression(BoundExpression
case BoundExpressionKind::LiteralBool:
case BoundExpressionKind::Unary:
case BoundExpressionKind::NumericalTable:
case BoundExpressionKind::Table:
throw;
}
@@ -187,8 +192,9 @@ shared_ptr<EvalValue> Evaluator::EvaluateTableExpression(BoundExpression * expre
case BoundExpressionKind ::Variable: return this->GetVariable((BoundVariableExpression*)expression);
case BoundExpressionKind ::Index: return this->EvaluateIndexExpression(expression);
case BoundExpressionKind ::NumericalTable: return this-> EvaluateNumericTableExpression(expression);
case BoundExpressionKind ::Table: return this -> EvaluateComplexTableExpression(expression);
default:
return nullptr;
throw;
}
}
@@ -222,8 +228,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunctionCallExpression(BoundExpression*
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->_evaluationScope->OuterScope();
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
@@ -237,6 +242,12 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto type = std::dynamic_pointer_cast<FunctionScriptType>(function->GetType());
auto parameterTypes = type->GetParameterTypes();
auto parameterKeys = type->GetParameterKeys();
auto scope = type -> GetScopeIndex();
auto originalScope = this->_evaluationScope;
auto functionScope = this->_evaluationScope->CreateBranchingScope(scope);
this->_evaluationScope = functionScope;
for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){
auto parameter = parameters[i];
auto requiredType = parameterTypes.at(i);
@@ -246,7 +257,9 @@ shared_ptr<EvalValue> Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct
auto key = parameterKeys.at(i);
this->_evaluationScope->CreateVariable(key->GetScopeId(), key->GetIdentifier(), parameter->Clone());
}
this->EvaluateBlockStatement(function->GetInnerBlock().get());
this->EvaluateBlockStatement(function->GetInnerBlock().get(), true);
delete this->_evaluationScope;
this->_evaluationScope = originalScope;
this->_hasReturned = false;
auto r = this -> _returnValue;
this -> _returnValue = nullptr;
@@ -272,3 +285,18 @@ shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(BoundExpression
return make_shared<TableEvalValue>(valuesPointer, tableExpression->GetType());
}
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) {
auto tableExpression = (BoundTableExpression*)expression;
auto type = dynamic_pointer_cast<TableScriptType>(tableExpression->GetType());
auto declaredVars = type -> GetValues();
auto variables = make_shared<unordered_map<size_t, shared_ptr<EvalValue>>>(declaredVars->size());
for (auto i : *declaredVars){
variables->insert({i.first, nullptr});
}
auto evaluator = new EvaluationScope(variables.get(), type -> GetDeepestScope());
auto currentEvaluator = this -> _evaluationScope;
this -> _evaluationScope = evaluator;
this -> EvaluateBlockStatement(tableExpression->GetBlock(), false);
this -> _evaluationScope = currentEvaluator;
return shared_ptr<TableEvalValue>(new TableEvalValue(variables, tableExpression->GetType()));
}

View File

@@ -23,7 +23,7 @@ class Evaluator {
EvaluationScope* _evaluationScope;
void EvaluateStatement(BoundStatement* statement);
void EvaluateBlockStatement(BoundBlockStatement* statement);
void EvaluateBlockStatement(BoundBlockStatement* statement, bool clearScope);
void EvaluateExpressionStatement(BoundExpressionStatement* statement);
void EvaluateAssignmentStatement(BoundAssignmentStatement* statement);
void EvaluateFunctionDeclarationStatement(BoundFunctionDeclarationStatement *statement);
@@ -67,6 +67,8 @@ public:
EvalValue* GetLastValue(){
return _lastValue.get();
}
shared_ptr<EvalValue> EvaluateComplexTableExpression(BoundExpression *expression);
};