Work on making userdata work through extern C entry points
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-14 22:21:33 +02:00
parent 2c313791d9
commit 7c345d85e8
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
22 changed files with 128 additions and 51 deletions

View File

@ -83,11 +83,16 @@ BoundStatement *Binder::BindIndexAssignmentStatement(const ParsedStatement *stat
}
std::shared_ptr<ScriptType> ParseTypeIdentifier(HashedString s){
switch (s.GetHash()){
auto hash = s.GetHash();
switch (hash){
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<StringScriptType>(false, 0);
default: return std::make_shared<UserDataScriptType>(s.GetHash());
default:
if (!UserDataStorage::HasUserDataType(hash)){
return nullptr;
}
return std::make_shared<UserDataScriptType>(hash);
}
}
@ -102,6 +107,10 @@ BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *
for (int i = 0; i < parameters->size(); i++){
auto var = parameters -> at(i);
auto parsedType = ParseTypeIdentifier(var->GetType());
if (parsedType == nullptr){
this -> _scriptData -> Diagnostics -> LogError(DiagnosticCode::InvalidTypeName, statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
}
parameterTypes.at(i) = parsedType;
auto parameterAssignment = this->_scope->CreateExplicitLocal(var->GetIdentifier().GetHash(), parsedType);
if (parameterAssignment.GetResult() == VariableAssignmentResult::Ok){
@ -442,7 +451,7 @@ BoundExpression* Binder::BindNumericalTableExpression(const ParsedNumericalTable
}
BoundExpression *Binder::BindTableExpression(const ParsedTableExpression *expression) {
auto tableScope = new unordered_map<int, BoundVariable*>();
auto tableScope = new unordered_map<uint32_t, BoundVariable*>();
auto innerScope = new BoundScope(tableScope);
auto currentScope = this -> _scope;
this -> _scope = innerScope;

View File

@ -3,11 +3,11 @@
#include "BoundScope.hpp"
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *tableScope) {
BoundScope::BoundScope(unordered_map<uint32_t, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
auto localUpmostScope = new unordered_map<uint32_t, BoundVariable*>();
_localScope.push_back(localUpmostScope);
}
@ -24,7 +24,7 @@ void BoundScope::GoInnerScope() {
_lastCreatedScope++;
_currentScope = _lastCreatedScope;
if (_localScope.size() < _currentScope){
auto innerScope = new unordered_map<int, BoundVariable*>();
auto innerScope = new unordered_map<uint32_t, BoundVariable*>();
_localScope.push_back(innerScope);
}
}
@ -53,7 +53,7 @@ int BoundScope::Exists(int key) {
return -1;
}
BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
BoundVariable *BoundScope::GetVariable(uint32_t scope, uint32_t identifier) {
if (scope == 0){
auto find = this -> _tableScope->find(identifier);
if (find != _tableScope->end()){
@ -70,7 +70,7 @@ BoundVariable *BoundScope::GetVariable(int scope, int identifier) {
}
}
VariableAssignment BoundScope::CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type) {
VariableAssignment BoundScope::CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type) {
auto scope = this->_localScope.at(this->_currentScope - 1);
if (scope -> find(identifier) != scope -> end()){
return VariableAssignment(VariableAssignmentResult::ExplicitLocalVariableExists, nullptr);
@ -79,7 +79,7 @@ VariableAssignment BoundScope::CreateExplicitLocal(int identifier, std::shared_p
return VariableAssignment(VariableAssignmentResult::Ok, new BoundVariableKey(identifier, this->_currentScope, true));
}
VariableAssignment BoundScope::AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type) {
VariableAssignment BoundScope::AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type) {
int exists = this->Exists(identifier);
if (exists == -1){
// Creation

View File

@ -14,21 +14,21 @@
using namespace std;
class BoundScope {
unordered_map<int, BoundVariable*>* _tableScope;
vector<unordered_map<int, BoundVariable*>*> _localScope;
unordered_map<uint32_t, BoundVariable*>* _tableScope;
vector<unordered_map<uint32_t, BoundVariable*>*> _localScope;
int _currentScope;
int _lastCreatedScope;
public:
explicit BoundScope(unordered_map<int, BoundVariable*> *tableScope);
explicit BoundScope(unordered_map<uint32_t, BoundVariable*> *tableScope);
~BoundScope();
void GoInnerScope();
void GoOuterScope();
int Exists(int key);
BoundVariable* GetVariable(int scope, int identifier);
VariableAssignment CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type);
BoundVariable* GetVariable(uint32_t scope, uint32_t identifier);
VariableAssignment CreateExplicitLocal(uint32_t identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(uint32_t identifier, const std::shared_ptr<ScriptType>& type);
size_t GetLocalVariableCount(){
return _localScope.size();

View File

@ -22,6 +22,7 @@ enum class DiagnosticCode{
InvalidReturnType,
ConditionNotABool,
InvalidTableValueType,
InvalidTypeName,
};
#endif //PORYGONLANG_DIAGNOSTICCODE_HPP

View File

@ -7,15 +7,15 @@
using namespace std;
class TableEvalValue : public EvalValue {
shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> _table;
shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> _table;
size_t _hash;
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, size_t hash){
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table, size_t hash){
_table = std::move(table);
_hash = hash;
}
public:
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table){
explicit TableEvalValue(shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>> table){
_table = std::move(table);
_hash = rand();
}

View File

@ -2,7 +2,7 @@
#include "EvaluationScope.hpp"
#include <memory>
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
EvaluationScope::EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
_scriptScope = scriptVariables;
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
}

View File

@ -7,10 +7,10 @@
#include "../EvalValues/EvalValue.hpp"
class EvaluationScope {
unordered_map<size_t, shared_ptr<EvalValue>>* _scriptScope;
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptScope;
unordered_map<uint64_t, shared_ptr<EvalValue>> _localScope;
public:
explicit EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
explicit EvaluationScope(unordered_map<uint32_t, shared_ptr<EvalValue>>* scriptVariables, int deepestScope);
~EvaluationScope() = default;
void CreateVariable(const BoundVariableKey* key, const shared_ptr<EvalValue>& value);

View File

@ -279,12 +279,12 @@ shared_ptr<EvalValue> Evaluator::EvaluateIndexExpression(const BoundExpression *
shared_ptr<EvalValue> Evaluator::EvaluateNumericTableExpression(const BoundExpression *expression) {
auto tableExpression = (BoundNumericalTableExpression*)expression;
auto valueExpressions = tableExpression->GetExpressions();
auto values = new unordered_map<size_t, shared_ptr<EvalValue>>(valueExpressions->size());
auto values = new unordered_map<uint32_t, shared_ptr<EvalValue>>(valueExpressions->size());
for (int i = 0; i < valueExpressions->size(); i++){
auto val = this -> EvaluateExpression(valueExpressions -> at(i));
values -> insert({i + 1, val});
}
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
auto valuesPointer = shared_ptr<unordered_map<uint32_t, shared_ptr<EvalValue>>>(values);
return make_shared<TableEvalValue>(valuesPointer);
}
@ -292,7 +292,7 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(const BoundExpre
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());
auto variables = make_shared<unordered_map<uint32_t, shared_ptr<EvalValue>>>(declaredVars->size());
for (auto i : *declaredVars){
variables->insert({i.first, nullptr});
}

View File

@ -17,7 +17,7 @@ Script::Script() {
Diagnostics = new DiagnosticsHolder();
_evaluator = new Evaluator(this);
_boundScript = nullptr;
_scriptVariables = new unordered_map<size_t, shared_ptr<EvalValue>>(0);
_scriptVariables = new unordered_map<uint32_t, shared_ptr<EvalValue>>(0);
}
EvalValue* Script::Evaluate() {
@ -42,7 +42,7 @@ void Script::Parse(const string& script) {
}
lexResult.clear();
if (!Diagnostics->HasErrors()){
unordered_map<int, BoundVariable*> scriptScope;
unordered_map<uint32_t, BoundVariable*> scriptScope;
auto bindScope = new BoundScope(&scriptScope);
this->_boundScript = Binder::Bind(this, parseResult, bindScope);
for (const auto& v : scriptScope){
@ -72,9 +72,9 @@ bool Script::HasFunction(const string &key) {
return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function;
}
shared_ptr<EvalValue> Script::CallFunction(const string &key, vector<EvalValue *> variables) {
shared_ptr<EvalValue> Script::CallFunction(const string &key, const vector<EvalValue *>& variables) {
auto var = (ScriptFunctionEvalValue*)GetVariable(key);
return this->_evaluator->EvaluateFunction(var, std::move(variables));
return this->_evaluator->EvaluateFunction(var, variables);
}
extern "C" {

View File

@ -18,7 +18,7 @@ class Script {
friend class Evaluator;
Evaluator* _evaluator;
unordered_map<size_t, shared_ptr<EvalValue>>* _scriptVariables;
unordered_map<uint32_t, shared_ptr<EvalValue>>* _scriptVariables;
BoundScriptStatement* _boundScript;
shared_ptr<ScriptType> _returnType;
@ -45,7 +45,7 @@ public:
EvalValue* GetVariable(const string& key);
bool HasVariable(const string& key);
shared_ptr<EvalValue> CallFunction(const string& key, vector<EvalValue*> variables);
shared_ptr<EvalValue> CallFunction(const string& key, const vector<EvalValue*>& variables);
bool HasFunction(const string& key);
};

View File

@ -11,3 +11,17 @@ shared_ptr<ScriptType> ScriptType::GetIndexedType(ScriptType *indexer) {
}
return make_shared<ScriptType>(TypeClass::Error);
}
extern "C"{
ScriptType* CreateScriptType(TypeClass t){
return new ScriptType(t);
}
ScriptType* CreateNumericScriptType(bool isAware, bool isFloat){
return new NumericScriptType(isAware, isFloat);
}
ScriptType* CreateStringScriptType(bool knownAtBind, uint32_t hash){
return new StringScriptType(knownAtBind, hash);
}
}

View File

@ -76,9 +76,9 @@ public:
class StringScriptType : public ScriptType{
bool _isKnownAtBind;
int _hashValue;
uint32_t _hashValue;
public:
explicit StringScriptType(bool knownAtBind, int hashValue): ScriptType(TypeClass::String){
explicit StringScriptType(bool knownAtBind, uint32_t hashValue): ScriptType(TypeClass::String){
_isKnownAtBind = knownAtBind;
_hashValue = hashValue;
}
@ -87,7 +87,7 @@ public:
return _isKnownAtBind;
}
int GetHashValue(){
uint32_t GetHashValue(){
return _hashValue;
}
};

View File

@ -5,10 +5,10 @@
#include "Binder/BoundVariables/BoundVariable.hpp"
class TableScriptType : public ScriptType{
const unordered_map<int, BoundVariable*>* _values;
const unordered_map<uint32_t, BoundVariable*>* _values;
const int _localVariableCount;
public:
explicit TableScriptType(unordered_map<int, BoundVariable*>* values, int localVariableCount)
explicit TableScriptType(unordered_map<uint32_t, BoundVariable*>* values, int localVariableCount)
: ScriptType(TypeClass::Table),
_values(values),
_localVariableCount(localVariableCount)
@ -33,7 +33,7 @@ public:
throw "TODO: indexing with dynamic keys";
}
const unordered_map<int, BoundVariable*>* GetValues(){
const unordered_map<uint32_t, BoundVariable*>* GetValues(){
return _values;
}

View File

@ -1,2 +1,20 @@
#include "UserData.hpp"
#include "UserDataStorage.hpp"
extern "C"{
void RegisterUserDataType(uint32_t id){
auto ud = new UserData({});
UserDataStorage::RegisterType(id, ud);
}
void RegisterUserDataField(uint32_t typeId, uint32_t fieldId, UserDataField* field){
auto ud = UserDataStorage::GetUserDataType(typeId);
ud -> CreateField(fieldId, field);
}
int32_t GetUserDataFieldCount(uint32_t typeId){
auto ud = UserDataStorage::GetUserDataType(typeId);
return ud ->GetFieldCount();
}
}

View File

@ -6,19 +6,27 @@
#include "UserDataField.hpp"
class UserData {
std::unordered_map<int, UserDataField*> _fields;
std::unordered_map<uint32_t, UserDataField*> _fields;
public:
explicit UserData(std::unordered_map<int, UserDataField*> fields){
explicit UserData(std::unordered_map<uint32_t, UserDataField*> fields){
_fields = std::move(fields);
}
bool ContainsField(int fieldId){
bool ContainsField(uint32_t fieldId){
return _fields.find(fieldId) != _fields.end();
}
UserDataField* GetField(int fieldId){
UserDataField* GetField(uint32_t fieldId){
return _fields[fieldId];
}
void CreateField(uint32_t fieldId, UserDataField* field){
_fields.insert({fieldId, field});
}
int32_t GetFieldCount(){
return _fields.size();
}
};

View File

@ -1,2 +1,8 @@
#include "UserDataField.hpp"
extern "C"{
UserDataField* CreateUserDataField(ScriptType* type, EvalValue* (*getter)(void* obj), void (*setter)(void* obj, EvalValue* val)){
return new UserDataField(type, getter, setter);
}
}

View File

@ -4,6 +4,7 @@
#include "../Evaluator/EvalValues/EvalValue.hpp"
#include "../Evaluator/EvalValues/NumericEvalValue.hpp"
class UserDataField {
shared_ptr<ScriptType> _type;

View File

@ -1,8 +1,9 @@
#ifndef PORYGONLANG_USERDATASCRIPTTYPE_HPP
#define PORYGONLANG_USERDATASCRIPTTYPE_HPP
#include <utility>
#include "../ScriptType.hpp"
#include "UserData.hpp"
#include "UserDataStorage.hpp"
@ -10,12 +11,21 @@
class UserDataScriptType : public ScriptType{
shared_ptr<UserData> _userData;
public:
explicit UserDataScriptType(int id) : ScriptType(TypeClass::UserData){
explicit UserDataScriptType(uint32_t id) : ScriptType(TypeClass::UserData){
_userData = UserDataStorage::GetUserDataType(id);
}
explicit UserDataScriptType(shared_ptr<UserData> ud) : ScriptType(TypeClass::UserData){
_userData = std::move(ud);
}
bool CanBeIndexedWith(ScriptType* indexer) final{
return indexer->GetClass() == TypeClass ::String;
if (indexer->GetClass() != TypeClass ::String){
return false;
}
auto str = (StringScriptType*)indexer;
if (!str->IsKnownAtBind())
return false;
return _userData->ContainsField(str->GetHashValue());
}
shared_ptr<ScriptType> GetIndexedType(ScriptType* indexer) final{

View File

@ -8,16 +8,20 @@
class UserDataStorage {
class _internalDataStorage{
public:
std::unordered_map<int, shared_ptr<UserData>> _userData;
std::unordered_map<uint32_t, shared_ptr<UserData>> _userData;
};
static _internalDataStorage _internal;
public:
static void RegisterType(int i, UserData* ud){
static void RegisterType(uint32_t i, UserData* ud){
UserDataStorage::_internal._userData.insert({i, shared_ptr<UserData>(ud)});
}
static shared_ptr<UserData> GetUserDataType(int i){
static bool HasUserDataType(uint32_t i){
return UserDataStorage::_internal._userData.find(i) != UserDataStorage::_internal._userData.end();
}
static shared_ptr<UserData> GetUserDataType(uint32_t i){
return UserDataStorage::_internal._userData[i];
}
};

View File

@ -1,2 +1,8 @@
#include "UserDataValue.hpp"
extern "C"{
UserDataValue* CreateUserDataEvalValue(uint32_t typeHash, void* obj){
return new UserDataValue(typeHash, obj);
}
}

View File

@ -17,7 +17,7 @@ public:
_obj = obj;
}
UserDataValue(int userDataId, void* obj){
UserDataValue(uint32_t userDataId, void* obj){
_userData = UserDataStorage::GetUserDataType(userDataId);
_obj = obj;
}

View File

@ -5,7 +5,7 @@
#include <string>
class HashedString{
const int _hash;
const uint32_t _hash;
public:
explicit HashedString(const std::string& s) : _hash(ConstHash(s.c_str())){
}
@ -13,13 +13,13 @@ public:
}
HashedString(const HashedString& b) = default;
static unsigned constexpr ConstHash(char const *input) {
static uint32_t constexpr ConstHash(char const *input) {
return *input ?
static_cast<unsigned int>(*input) + 33 * ConstHash(input + 1) :
static_cast<uint32_t>(*input) + 33 * ConstHash(input + 1) :
5381;
}
const int GetHash() const{
const uint32_t GetHash() const{
return _hash;
}