Implements variable usage, tweaks and fixes for variable assignment

This commit is contained in:
2019-05-30 15:23:48 +02:00
parent 257eb942c7
commit 6fad5a0a7d
17 changed files with 145 additions and 4 deletions

View File

@@ -72,6 +72,8 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
return new BoundLiteralStringExpression(((LiteralStringExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind ::LiteralBool:
return new BoundLiteralBoolExpression(((LiteralBoolExpression*)expression)->GetValue(), expression->GetStartPosition(), expression->GetLength());
case ParsedExpressionKind ::Variable:
return this -> BindVariableExpression((VariableExpression*)expression);
case ParsedExpressionKind ::Binary:
return this -> BindBinaryOperator((BinaryExpression*)expression);
@@ -86,6 +88,18 @@ BoundExpression* Binder::BindExpression(ParsedExpression* expression){
}
}
BoundExpression* Binder::BindVariableExpression(VariableExpression* expression){
auto key = expression->GetValue();
auto scope = this->_scope->Exists(key.GetHash());
if (scope == -1){
this -> _scriptData -> Diagnostics->LogError(DiagnosticCode::VariableNotFound, expression->GetStartPosition(), expression->GetLength());
return new BoundBadExpression(expression->GetStartPosition(), expression->GetLength());
}
auto var = this->_scope->GetVariable(scope, key.GetHash());
auto type = var->GetType();
return new BoundVariableExpression(scope, key.GetHash(), type, expression->GetStartPosition(), expression->GetLength());
}
BoundExpression* Binder::BindBinaryOperator(BinaryExpression* expression){
auto boundLeft = this -> BindExpression(expression->GetLeft());
auto boundRight = this -> BindExpression(expression->GetRight());

View File

@@ -19,6 +19,7 @@ class Binder {
BoundStatement *BindAssignmentStatement(ParsedStatement *statement);
BoundExpression *BindExpression(ParsedExpression *expression);
BoundExpression *BindVariableExpression(VariableExpression *expression);
BoundExpression *BindBinaryOperator(BinaryExpression *expression);
BoundExpression *BindUnaryOperator(UnaryExpression *expression);
public:

View File

@@ -7,6 +7,7 @@
#include <string>
#include "../../ScriptType.hpp"
#include "../BoundOperators.hpp"
#include "../BoundVariables/BoundVariableKey.hpp"
using namespace std;
@@ -17,6 +18,7 @@ enum class BoundExpressionKind{
LiteralFloat,
LiteralString,
LiteralBool,
Variable,
Unary,
Binary,
@@ -129,6 +131,36 @@ public:
}
};
class BoundVariableExpression : public BoundExpression{
int _scope;
int _id;
ScriptType _type;
public:
BoundVariableExpression(int scope, int id, const ScriptType& type, unsigned int start, unsigned int length)
: BoundExpression(start, length, nullptr), _type(type){
_scope = scope;
_id = id;
}
~BoundVariableExpression() override = default;
ScriptType* GetType() final{
return &_type;
};
BoundExpressionKind GetKind() final{
return BoundExpressionKind ::Variable;
}
int GetScope(){
return _scope;
}
int GetId(){
return _id;
}
};
class BoundBinaryExpression : public BoundExpression {
BoundExpression* _left;
BoundExpression* _right;

View File

@@ -57,7 +57,7 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
}
return nullptr;
} else{
auto s = this->_localScope.at(scope);
auto s = this->_localScope.at(scope - 1);
auto find = s -> find(identifier);
if (find != s -> end()){
return find -> second;
@@ -72,7 +72,7 @@ VariableAssignment BoundScope::CreateExplicitLocal(int identifier, const ScriptT
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
}
scope -> insert({identifier, new BoundVariable(type)});
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope - 1, true));
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(int identifier, const ScriptType& type) {

View File

@@ -7,7 +7,7 @@
class BoundVariable{
ScriptType _type;
public:
explicit BoundVariable(ScriptType type) : _type(type){
explicit BoundVariable(const ScriptType& type) : _type(type){
}
~BoundVariable(){
}