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

@@ -1,6 +1,6 @@
#include <memory>
#include "Binder.hpp"
#include "../TableScriptType.hpp"
#include "BoundExpressions/BoundTableExpression.hpp"
#include <memory>
BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, BoundScope* scriptScope) {
@@ -71,7 +71,7 @@ std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
switch (s.GetHash()){
case HashedString::ConstHash("number"): return std::make_shared<NumericScriptType>(false, false);
case HashedString::ConstHash("bool"): return std::make_shared<ScriptType>(TypeClass::Bool);
case HashedString::ConstHash("string"): return std::make_shared<ScriptType>(TypeClass::String);
case HashedString::ConstHash("string"): return std::make_shared<StringScriptType>(false, 0);
default: return std::make_shared<ScriptType>(TypeClass::Error); // todo: change to userdata
}
}
@@ -178,6 +178,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
return this->BindIndexExpression((IndexExpression*)expression);
case ParsedExpressionKind::NumericalTable:
return this -> BindNumericalTableExpression((ParsedNumericalTableExpression*)expression);
case ParsedExpressionKind ::Table:
return this -> BindTableExpression((ParsedTableExpression*)expression);
case ParsedExpressionKind ::Bad:
return new BoundBadExpression(expression->GetStartPosition(), expression-> GetLength());
@@ -220,7 +222,8 @@ BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
expression->GetStartPosition(), expression->GetLength());
}
} else if (boundLeftType->GetClass() == TypeClass::String){
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared<ScriptType>(TypeClass::String),
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation, std::make_shared<StringScriptType>(false,
0),
expression->GetStartPosition(), expression->GetLength());
}
break;
@@ -412,7 +415,19 @@ BoundExpression* Binder::BindNumericalTableExpression(ParsedNumericalTableExpres
if (valueType == nullptr){
valueType = std::make_shared<ScriptType>(TypeClass::Nil);
}
auto keyType = std::make_shared<ScriptType>(TypeClass::Number);
auto tableType = std::make_shared<TableScriptType>(keyType, valueType);
auto tableType = std::make_shared<NumericalTableScriptType>(valueType);
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(), expression->GetLength());
}
}
BoundExpression *Binder::BindTableExpression(ParsedTableExpression *expression) {
auto tableScope = new unordered_map<int, BoundVariable*>();
auto innerScope = new BoundScope(tableScope);
auto currentScope = this -> _scope;
this -> _scope = innerScope;
auto block = this -> BindBlockStatement(expression -> GetBlock());
this -> _scope = currentScope;
auto tableType = shared_ptr<TableScriptType>(new TableScriptType(tableScope, innerScope->GetDeepestScope()));
return new BoundTableExpression((BoundBlockStatement*)block, tableType, expression->GetStartPosition(), expression->GetLength());
}

View File

@@ -5,6 +5,8 @@
#include "BoundStatements/BoundStatement.hpp"
#include "../Script.hpp"
#include "BoundVariables/BoundScope.hpp"
#include "../Parser/ParsedExpressions/ParsedTableExpression.hpp"
using namespace std;
class Binder {
@@ -29,6 +31,7 @@ class Binder {
BoundExpression *BindFunctionCall(FunctionCallExpression *expression);
BoundExpression *BindIndexExpression(IndexExpression *expression);
BoundExpression *BindNumericalTableExpression(ParsedNumericalTableExpression *expression);
BoundExpression *BindTableExpression(ParsedTableExpression * expression);
public:
static BoundScriptStatement* Bind(Script* script, ParsedScriptStatement* s, BoundScope* scriptScope);

View File

@@ -25,6 +25,7 @@ enum class BoundExpressionKind{
FunctionCall,
Index,
NumericalTable,
Table,
};
class BoundExpression{
@@ -102,7 +103,7 @@ class BoundLiteralStringExpression : public BoundExpression{
string _value;
public:
BoundLiteralStringExpression(string value, unsigned int start, unsigned int length)
: BoundExpression(start, length, make_shared<ScriptType>(TypeClass::String)){
: BoundExpression(start, length, make_shared<StringScriptType>(true, HashedString::ConstHash(value.c_str()))){
_value = std::move(value);
}
@@ -276,11 +277,11 @@ class BoundNumericalTableExpression : public BoundExpression{
vector<BoundExpression*> _expressions;
public:
BoundNumericalTableExpression(vector<BoundExpression*> expressions, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, type){
: BoundExpression(start, length, std::move(type)){
_expressions = std::move(expressions);
}
~BoundNumericalTableExpression(){
~BoundNumericalTableExpression() final{
for (auto e: _expressions){
delete e;
}

View File

@@ -0,0 +1,32 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDTABLEEXPRESSION_HPP
#define PORYGONLANG_BOUNDTABLEEXPRESSION_HPP
#include "../BoundStatements/BoundStatement.hpp"
class BoundTableExpression : public BoundExpression{
BoundBlockStatement* _block;
public:
BoundTableExpression(BoundBlockStatement* block, shared_ptr<ScriptType> type, unsigned int start, unsigned int length)
: BoundExpression(start, length, std::move(type)){
_block = block;
}
~BoundTableExpression() final{
delete _block;
}
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Table;
}
BoundBlockStatement* GetBlock(){
return _block;
}
};
#include "BoundExpression.hpp"
#endif //PORYGONLANG_BOUNDTABLEEXPRESSION_HPP

View File

@@ -3,8 +3,8 @@
#include "BoundScope.hpp"
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *scriptScope) {
_scriptScope = scriptScope;
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_deepestScope = 1;
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
@@ -41,8 +41,8 @@ void BoundScope::GoOuterScope() {
}
int BoundScope::Exists(int key) {
auto found = this -> _scriptScope -> find(key);
if (found != _scriptScope -> end()){
auto found = this -> _tableScope -> find(key);
if (found != _tableScope -> end()){
return 0;
}
for (int i = _currentScope - 1; i >= 0; i--){
@@ -57,8 +57,8 @@ int BoundScope::Exists(int key) {
BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
if (scope == 0){
auto find = this -> _scriptScope->find(identifier);
if (find != _scriptScope->end()){
auto find = this -> _tableScope->find(identifier);
if (find != _tableScope->end()){
return find -> second;
}
return nullptr;
@@ -85,7 +85,7 @@ VariableAssignment BoundScope::AssignVariable(int identifier, const std::shared_
int exists = this->Exists(identifier);
if (exists == -1){
// Creation
_scriptScope->insert({identifier, new BoundVariable(type)});
_tableScope->insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, 0, true));
} else{
// Assigning

View File

@@ -14,12 +14,12 @@
using namespace std;
class BoundScope {
unordered_map<int, BoundVariable*>* _scriptScope;
unordered_map<int, BoundVariable*>* _tableScope;
vector<unordered_map<int, BoundVariable*>*> _localScope;
int _currentScope;
int _deepestScope;
public:
explicit BoundScope(unordered_map<int, BoundVariable*> *scriptScope);
explicit BoundScope(unordered_map<int, BoundVariable*> *tableScope);
~BoundScope();
void GoInnerScope();