Skip to content

Commit

Permalink
22121: Improves consistency and handling of data types, especially st…
Browse files Browse the repository at this point in the history
…rings, MAJOR (#302)
  • Loading branch information
howsohazard authored Nov 10, 2024
1 parent 94e22e9 commit 484e306
Show file tree
Hide file tree
Showing 36 changed files with 4,797 additions and 4,682 deletions.
2 changes: 1 addition & 1 deletion src/Amalgam/AmalgamMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ PLATFORM_MAIN_CONSOLE
for(auto &[used_node, _] : nr.nodesReferenced)
{
std::cerr << "Item:" << std::endl;
std::cerr << Parser::Unparse(used_node, &entity->evaluableNodeManager);
std::cerr << Parser::Unparse(used_node);
}
}

Expand Down
33 changes: 17 additions & 16 deletions src/Amalgam/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ bool AssetManager::StoreResource(EvaluableNode *code, AssetParameters &asset_par
if(!outf.good())
return false;

std::string code_string = Parser::Unparse(code, enm, asset_params.prettyPrint, true, asset_params.sortKeys);
std::string code_string = Parser::Unparse(code, asset_params.prettyPrint, true, asset_params.sortKeys);
outf.write(code_string.c_str(), code_string.size());
outf.close();

Expand All @@ -297,7 +297,7 @@ bool AssetManager::StoreResource(EvaluableNode *code, AssetParameters &asset_par
}
else if(asset_params.resourceType == FILE_EXTENSION_COMPRESSED_AMALGAM_CODE)
{
std::string code_string = Parser::Unparse(code, enm, asset_params.prettyPrint, true, asset_params.sortKeys);
std::string code_string = Parser::Unparse(code, asset_params.prettyPrint, true, asset_params.sortKeys);

//transform into format needed for compression
CompactHashMap<std::string, size_t> string_map;
Expand All @@ -309,7 +309,10 @@ bool AssetManager::StoreResource(EvaluableNode *code, AssetParameters &asset_par
}
else //binary string
{
std::string s = EvaluableNode::ToStringPreservingOpcodeType(code);
if(code == nullptr || code->GetType() != ENT_STRING)
return false;

const std::string &s = code->GetStringValue();
return StoreFileFromBuffer<std::string>(asset_params.resource, asset_params.resourceType, s);
}

Expand Down Expand Up @@ -337,6 +340,7 @@ Entity *AssetManager::LoadEntityFromResource(AssetParameters &asset_params, bool
}

EvaluableNodeReference code = LoadResource(asset_params, &new_entity->evaluableNodeManager, status);

if(!status.loaded)
{
delete new_entity;
Expand Down Expand Up @@ -373,25 +377,22 @@ Entity *AssetManager::LoadEntityFromResource(AssetParameters &asset_params, bool
if(EvaluableNode::IsAssociativeArray(metadata))
{
EvaluableNode **seed = metadata->GetMappedChildNode(GetStringIdFromBuiltInStringId(ENBISI_rand_seed));
if(seed != nullptr)
if(seed != nullptr && (*seed)->GetType() == ENT_STRING)
{
default_random_seed = EvaluableNode::ToStringPreservingOpcodeType(*seed);
default_random_seed = (*seed)->GetStringValue();
new_entity->SetRandomState(default_random_seed, true);
}

EvaluableNode **version = metadata->GetMappedChildNode(GetStringIdFromBuiltInStringId(ENBISI_version));
if(version != nullptr)
if(version != nullptr && (*version)->GetType() == ENT_STRING)
{
auto [to_str_success, version_str] = EvaluableNode::ToString(*version);
if(to_str_success)
const std::string &version_str = (*version)->GetStringValue();
auto [error_message, success] = AssetManager::ValidateVersionAgainstAmalgam(version_str);
if(!success)
{
auto [error_message, success] = AssetManager::ValidateVersionAgainstAmalgam(version_str);
if(!success)
{
status.SetStatus(false, error_message, version_str);
delete new_entity;
return nullptr;
}
status.SetStatus(false, error_message, version_str);
delete new_entity;
return nullptr;
}
}
}
Expand Down Expand Up @@ -486,7 +487,7 @@ void AssetManager::SetRootPermission(Entity *entity, bool permission)
rootEntities.erase(entity);
}

std::pair<std::string, bool> AssetManager::ValidateVersionAgainstAmalgam(std::string &version)
std::pair<std::string, bool> AssetManager::ValidateVersionAgainstAmalgam(const std::string &version)
{
auto sem_ver = StringManipulation::Split(version, '-'); //split on postfix
auto version_split = StringManipulation::Split(sem_ver[0], '.'); //ignore postfix
Expand Down
13 changes: 6 additions & 7 deletions src/Amalgam/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ class AssetManager
{
EvaluableNode *top_entity_code = EntityManipulation::FlattenOnlyTopEntity(&entity->evaluableNodeManager,
entity, asset_params.includeRandSeeds, true);
std::string code_string = Parser::Unparse(top_entity_code, &entity->evaluableNodeManager,
asset_params.prettyPrint, true, asset_params.sortKeys, true);
std::string code_string = Parser::Unparse(top_entity_code, asset_params.prettyPrint, true, asset_params.sortKeys, true);
entity->evaluableNodeManager.FreeNodeTree(top_entity_code);

//loop over contained entities, freeing resources after each entity
Expand All @@ -147,8 +146,8 @@ class AssetManager
EvaluableNode *create_entity_code = EntityManipulation::FlattenOnlyOneContainedEntity(
&entity->evaluableNodeManager, cur_entity, entity, asset_params.includeRandSeeds, true);

code_string += Parser::Unparse(create_entity_code, &entity->evaluableNodeManager,
asset_params.prettyPrint, true, asset_params.sortKeys, false, 1);
code_string += Parser::Unparse(create_entity_code,
asset_params.prettyPrint, asset_params.sortKeys, false, 1);

entity->evaluableNodeManager.FreeNodeTree(create_entity_code);
}
Expand Down Expand Up @@ -398,7 +397,7 @@ class AssetManager

//stores buffer b (of type BufferType of elements BufferElementType) into the filename, returns true if successful, false if not
template<typename BufferType>
static bool StoreFileFromBuffer(const std::string &filename, std::string &file_type, BufferType &b)
static bool StoreFileFromBuffer(const std::string &filename, std::string &file_type, const BufferType &b)
{
std::ofstream f(filename, std::fstream::binary | std::fstream::out);
if(!f.good())
Expand All @@ -410,14 +409,14 @@ class AssetManager
return false;
}

f.write(reinterpret_cast<char *>(&b[0]), sizeof(char) * b.size());
f.write(reinterpret_cast<const char *>(&b[0]), sizeof(char) * b.size());
return true;
}

//validates given asset version against Amalgam version
//if successful: returns empty string and true
//if failure: returns error message and false
static std::pair<std::string, bool> ValidateVersionAgainstAmalgam(std::string &version);
static std::pair<std::string, bool> ValidateVersionAgainstAmalgam(const std::string &version);

//returns a string representing en's source, empty string if debugSources is false
std::string GetEvaluableNodeSourceFromComments(EvaluableNode *en);
Expand Down
9 changes: 7 additions & 2 deletions src/Amalgam/GeneralizedDistance.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//project headers:
#include "EvaluableNode.h"
#include "EvaluableNodeManagement.h"
#include "EvaluableNodeTreeManipulation.h"
#include "FastMath.h"

Expand Down Expand Up @@ -1086,8 +1087,9 @@ class RepeatedGeneralizedDistanceEvaluator
: distEvaluator(nullptr)
{ }

inline RepeatedGeneralizedDistanceEvaluator(GeneralizedDistanceEvaluator *dist_evaluator)
: distEvaluator(dist_evaluator)
inline RepeatedGeneralizedDistanceEvaluator(GeneralizedDistanceEvaluator *dist_evaluator,
EvaluableNodeManager *enm)
: distEvaluator(dist_evaluator), evaluableNodeManager(enm)
{ }

//for the feature index, computes and stores the distance terms for nominal values
Expand Down Expand Up @@ -1443,4 +1445,7 @@ class RepeatedGeneralizedDistanceEvaluator

//for each feature, precomputed distance terms for each interned value looked up by intern index
std::vector<FeatureData> featureData;

//node allocations in case unparsing is required
EvaluableNodeManager *evaluableNodeManager;
};
21 changes: 20 additions & 1 deletion src/Amalgam/Opcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

StringInternPool string_intern_pool;

static inline void EmplaceStaticString(EvaluableNodeBuiltInStringId bisid, const char *str)
static inline void EmplaceStaticString(EvaluableNodeBuiltInStringId bisid, std::string str)
{
auto sid = string_intern_pool.CreateStringReference(str);
string_intern_pool.staticStringsIndexToStringID[bisid] = sid;
Expand Down Expand Up @@ -306,7 +306,26 @@ void StringInternPool::InitializeStaticStrings()
EmplaceStaticString(ENBISI_neg_infinity, "-.infinity");
EmplaceStaticString(ENBISI_zero, "0");
EmplaceStaticString(ENBISI_one, "1");
EmplaceStaticString(ENBISI_two, "2");
EmplaceStaticString(ENBISI_three, "3");
EmplaceStaticString(ENBISI_four, "4");
EmplaceStaticString(ENBISI_five, "5");
EmplaceStaticString(ENBISI_six, "6");
EmplaceStaticString(ENBISI_seven, "7");
EmplaceStaticString(ENBISI_eight, "8");
EmplaceStaticString(ENBISI_nine, "9");
EmplaceStaticString(ENBISI_neg_one, "-1");
EmplaceStaticString(ENBISI_zero_number_key, std::string("\0" "0", 2));
EmplaceStaticString(ENBISI_one_number_key, std::string("\0" "1", 2));
EmplaceStaticString(ENBISI_two_number_key, std::string("\0" "2", 2));
EmplaceStaticString(ENBISI_three_number_key, std::string("\0" "3", 2));
EmplaceStaticString(ENBISI_four_number_key, std::string("\0" "4", 2));
EmplaceStaticString(ENBISI_five_number_key, std::string("\0" "5", 2));
EmplaceStaticString(ENBISI_six_number_key, std::string("\0" "6", 2));
EmplaceStaticString(ENBISI_seven_number_key, std::string("\0" "7", 2));
EmplaceStaticString(ENBISI_eight_number_key, std::string("\0" "8", 2));
EmplaceStaticString(ENBISI_nine_number_key, std::string("\0" "9", 2));
EmplaceStaticString(ENBISI_neg_one_number_key, std::string("\0" "-1", 3));
EmplaceStaticString(ENBISI_empty_null, "(null)");
EmplaceStaticString(ENBISI_empty_list, "(list)");
EmplaceStaticString(ENBISI_empty_assoc, "(assoc)");
Expand Down
19 changes: 19 additions & 0 deletions src/Amalgam/Opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,26 @@ enum EvaluableNodeBuiltInStringId
ENBISI_neg_infinity,
ENBISI_zero,
ENBISI_one,
ENBISI_two,
ENBISI_three,
ENBISI_four,
ENBISI_five,
ENBISI_six,
ENBISI_seven,
ENBISI_eight,
ENBISI_nine,
ENBISI_neg_one,
ENBISI_zero_number_key,
ENBISI_one_number_key,
ENBISI_two_number_key,
ENBISI_three_number_key,
ENBISI_four_number_key,
ENBISI_five_number_key,
ENBISI_six_number_key,
ENBISI_seven_number_key,
ENBISI_eight_number_key,
ENBISI_nine_number_key,
ENBISI_neg_one_number_key,
ENBISI_empty_null,
ENBISI_empty_list,
ENBISI_empty_assoc,
Expand Down
Loading

0 comments on commit 484e306

Please sign in to comment.