Skip to content

Commit

Permalink
20554: Fixes issues with difference_entities, as well as some inconsi…
Browse files Browse the repository at this point in the history
…stent edge case handling with null values across opcodes (#152)
  • Loading branch information
howsohazard authored Jun 14, 2024
1 parent f9ca283 commit 4c7d8e3
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 218 deletions.
19 changes: 10 additions & 9 deletions src/Amalgam/amlg_code/full_test.amlg
Original file line number Diff line number Diff line change
Expand Up @@ -1939,10 +1939,11 @@

(let (assoc new_entity
(call (difference_entities "DiffEntity1" "DiffEntity2") (assoc _ "DiffEntity1")))
(print new_entity "\n")
(print (retrieve_entity_root new_entity))
(print (retrieve_entity_root (list new_entity "DiffEntityChild1")))
(print (contained_entities new_entity))
(print "new_entity: " new_entity "\n")
(print "new_entity root: " (retrieve_entity_root new_entity))
(print "DiffEntityChild1 root:\n" (retrieve_entity_root (list new_entity "DiffEntityChild1")))
(print "contained_entities new_entity: " (contained_entities new_entity))
(print "difference between DiffEntity2 and new_entity:\n" (difference_entities "DiffEntity2" new_entity))
)

(create_entities "DiffContainer" null)
Expand All @@ -1968,11 +1969,11 @@
(let (assoc new_entity
(call (difference_entities (list "DiffContainer" "DiffEntity1") (list "DiffContainer" "DiffEntity2") )
(assoc _ (list "DiffContainer" "DiffEntity1") new_entity "DiffContainerReconstructed")))
(print new_entity "\n")
(print (retrieve_entity_root new_entity))
(print (retrieve_entity_root (list new_entity "DiffEntityChild1")))
(print (contained_entities new_entity))
(print (difference_entities (list "DiffContainer" "DiffEntity2") new_entity ))
(print "new_entity: " new_entity "\n")
(print "new_entity root: " (retrieve_entity_root new_entity))
(print "DiffEntityChild1 root:\n" (retrieve_entity_root (list new_entity "DiffEntityChild1")))
(print "contained_entities new_entity: " (contained_entities new_entity))
(print "difference between DiffContainer and DiffEntity2:\n" (difference_entities (list "DiffContainer" "DiffEntity2") new_entity ))
)

(print "--mix_entities--\n")
Expand Down
93 changes: 43 additions & 50 deletions src/Amalgam/amlg_code/test.amlg
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
(seq

(create_entities "descriptive_entity" (lambda
;this is a fully described entity
(null
;some public variable
##publicvar 1
;some private variable
##!privatevar 2
;a variable accessible to contained entities
##^containervar 3
;the function foo
##foo (declare
;a number representing the sum
(assoc
;the value of x
x
;the default value of x
1
;the value of y
y 2
)
( + x y)

)

;returns the api details
##get_api (seq

(assoc
"description"
(get_entity_comments)

"labels"
(map (lambda
(assoc
"description"
(current_value 1)
"parameters"
(get_entity_comments (null) (current_index 1) (true))
(if 0 (seq
(print
(declare
{_ (null)}
(replace
_
[]
(lambda
{o 6}
)
)
(get_entity_comments (null) (null) (true))
)


)

)
"\n")
(conclude)
)


))

(print (get_entity_comments "descriptive_entity" "foo" (true)))

)

(create_entities "DiffEntity1" (lambda (assoc "c" 3 "b" 4)) )
(create_entities (list "DiffEntity1" "DiffEntityChild1") (lambda (assoc "x" 3 "y" 4 "z" 6)) )
(create_entities (list "DiffEntity1" "DiffEntityChild1" "DiffEntityChild2") (lambda (assoc "p" 3 "q" 4 "u" 5 "v" 6 "w" 7)) )
(create_entities (list "DiffEntity1" "DiffEntityChild1" "DiffEntityChild2" "DiffEntityChild3") (lambda (assoc "e" 3 "p" 4 "a" 5 "o" 6 "w" 7)) )
(create_entities (list "DiffEntity1" "OnlyIn1") (lambda (assoc "m" 4)) )
(create_entities (list "DiffEntity1") (lambda (assoc "E" 3 "F" 4)) )
(create_entities (list "DiffEntity1") (lambda (assoc "e" 3 "f" 4 "g" 5 "h" 6)) )

(create_entities "DiffEntity2" (lambda (assoc "c" 3 "b" 4)) )
(create_entities (list "DiffEntity2" "DiffEntityChild1") (lambda (assoc "x" 3 "y" 4 "z" 5)) )
(create_entities (list "DiffEntity2" "DiffEntityChild1" "DiffEntityChild2") (lambda (assoc "p" 3 "q" 4 "u" 5 "v" 6 "w" 7)) )
(create_entities (list "DiffEntity2" "DiffEntityChild1" "DiffEntityChild2" "DiffEntityChild3") (lambda (assoc "e" 3 "p" 4 "a" 5 "o" 6 "w" 7)) )
(create_entities (list "DiffEntity2" "OnlyIn2") (lambda (assoc "o" 6)) )
(create_entities (list "DiffEntity2") (lambda (assoc "E" 3 "F" 4 "G" 5 "H" 6)) )
(create_entities (list "DiffEntity2") (lambda (assoc "e" 3 "f" 4)) )

(print (contained_entities "DiffEntity2"))

(print (difference_entities "DiffEntity1" "DiffEntity2"))

(let (assoc new_entity
(call (difference_entities "DiffEntity1" "DiffEntity2") (assoc _ "DiffEntity1")))
(print "new_entity: " new_entity "\n")
(print "new_entity root: " (retrieve_entity_root new_entity))
(print "DiffEntityChild1 root:\n" (retrieve_entity_root (list new_entity "DiffEntityChild1")))
(print "contained_entities new_entity: " (contained_entities new_entity))
(print "difference between DiffEntity2 and new_entity:\n" (difference_entities "DiffEntity2" new_entity))
;(print "DiffEntity2:\n" (flatten_entity "DiffEntity2"))
;(print "new_entity:\n" (flatten_entity new_entity))
)
)
4 changes: 2 additions & 2 deletions src/Amalgam/entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ void Entity::RemoveContainedEntity(StringInternPool::StringID id, std::vector<En

Entity *Entity::GetContainedEntity(StringInternPool::StringID id)
{
if(!hasContainedEntities)
if(!hasContainedEntities || id == string_intern_pool.NOT_A_STRING_ID)
return nullptr;

auto &id_to_index_lookup = entityRelationships.relationships->containedEntityStringIdToIndex;
Expand All @@ -777,7 +777,7 @@ Entity *Entity::GetContainedEntity(StringInternPool::StringID id)

size_t Entity::GetContainedEntityIndex(StringInternPool::StringID id)
{
if(!hasContainedEntities)
if(!hasContainedEntities || id == string_intern_pool.NOT_A_STRING_ID)
return std::numeric_limits<size_t>::max();

auto &id_to_index_lookup = entityRelationships.relationships->containedEntityStringIdToIndex;
Expand Down
4 changes: 2 additions & 2 deletions src/Amalgam/entity/EntityManipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ EvaluableNodeReference EntityManipulation::DifferenceEntities(Interpreter *inter
// )
EvaluableNode *src_id_list = GetTraversalIDPathFromAToB(enm, entity2, entity_to_create);
EvaluableNode *src_append = enm->AllocNode(ENT_APPEND);
src_append->AppendOrderedChildNode(enm->AllocNode(ENT_SYMBOL, ENBISI_new_entity));
src_append->AppendOrderedChildNode(enm->AllocNode(ENT_SYMBOL, ENBISI__));
src_append->AppendOrderedChildNode(src_id_list);

EvaluableNode *dest_id_list = enm->DeepAllocCopy(src_id_list);
EvaluableNode *dest_append = enm->AllocNode(ENT_APPEND);
dest_append->AppendOrderedChildNode(enm->AllocNode(ENT_SYMBOL, ENBISI__));
dest_append->AppendOrderedChildNode(enm->AllocNode(ENT_SYMBOL, ENBISI_new_entity));
dest_append->AppendOrderedChildNode(dest_id_list);

EvaluableNode *create_entity = enm->AllocNode(ENT_CREATE_ENTITIES);
Expand Down
12 changes: 2 additions & 10 deletions src/Amalgam/evaluablenode/EvaluableNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,12 @@ std::pair<bool, std::string> EvaluableNode::ToString(EvaluableNode *e)

StringInternPool::StringID EvaluableNode::ToStringIDIfExists(EvaluableNode *e)
{
if(e == nullptr)
if(EvaluableNode::IsEmptyNode(e))
return StringInternPool::NOT_A_STRING_ID;

if((e->GetType() == ENT_STRING || e->GetType() == ENT_SYMBOL))
return e->GetStringIDReference();

//this will catch any other form of null or NaN not yet caught
if(IsNaN(e))
return StringInternPool::NOT_A_STRING_ID;

//see if the string exists even if it is not stored as a StringID
const std::string str_value = ToStringPreservingOpcodeType(e);
//will return empty string if not found
Expand All @@ -255,15 +251,11 @@ StringInternPool::StringID EvaluableNode::ToStringIDIfExists(EvaluableNode *e)

StringInternPool::StringID EvaluableNode::ToStringIDWithReference(EvaluableNode *e)
{
if(e == nullptr)
if(EvaluableNode::IsEmptyNode(e))
return StringInternPool::NOT_A_STRING_ID;

if(e->GetType() == ENT_STRING || e->GetType() == ENT_SYMBOL)
return string_intern_pool.CreateStringReference(e->GetStringIDReference());

//this will catch any other form of null or NaN not yet caught
if(IsNaN(e))
return StringInternPool::NOT_A_STRING_ID;

std::string stringified = ToStringPreservingOpcodeType(e);
return string_intern_pool.CreateStringReference(stringified);
Expand Down
2 changes: 1 addition & 1 deletion src/Amalgam/evaluablenode/EvaluableNodeTreeDifference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ EvaluableNode *EvaluableNodeTreeDifference::DifferenceTrees(EvaluableNodeManager
//if node to replace is nullptr, then replace the parent object
if(node_to_replace == nullptr)
{
df_replace->AppendOrderedChildNode(tree2);
replacement_function->AppendOrderedChildNode(tree2);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Amalgam/evaluablenode/EvaluableNodeTreeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ std::vector<EvaluableNode *> CustomEvaluableNodeOrderedChildNodesSort(std::vecto
void TraverseToEntityViaEvaluableNodeIDPath(Entity *container, EvaluableNode *id_path,
Entity *&relative_entity_container, StringInternRef &id, Entity *&relative_entity)
{
//TODO 10975: remove this function, deprecated by TraverseToExistingEntityReferenceAndContainerViaEvaluableNodeIDPath
//TODO 10975: remove this function, deprecated by TraverseToEntityReferenceAndContainerViaEvaluableNodeIDPath
relative_entity_container = nullptr;
id = StringInternPool::NOT_A_STRING_ID;

Expand Down
3 changes: 2 additions & 1 deletion src/Amalgam/interpreter/InterpreterOpcodesBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,9 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_SET_and_REPLACE(EvaluableN
return EvaluableNodeReference::Null();

auto result = InterpretNode(ocn[0]);

if(result == nullptr)
return EvaluableNodeReference::Null();
result.SetReference(evaluableNodeManager->AllocNode(ENT_NULL), true);

if(!result.unique)
result = evaluableNodeManager->DeepAllocCopy(result);
Expand Down
2 changes: 1 addition & 1 deletion src/Amalgam/interpreter/InterpreterOpcodesCodeMixing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_MUTATE(EvaluableNode *en,

auto to_mutate = InterpretNodeForImmediateUse(ocn[0]);
if(to_mutate == nullptr)
return EvaluableNodeReference::Null();
to_mutate.SetReference(evaluableNodeManager->AllocNode(ENT_NULL));
auto node_stack = CreateInterpreterNodeStackStateSaver(to_mutate);

double mutation_rate = 0.00001;
Expand Down
6 changes: 2 additions & 4 deletions src/Amalgam/interpreter/InterpreterOpcodesDataTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1088,10 +1088,8 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_GET_CONCURRENCY(EvaluableN
if(ocn.size() == 0)
return EvaluableNodeReference::Null();
auto n = InterpretNodeForImmediateUse(ocn[0]);
if(n == nullptr)
return EvaluableNodeReference::Null();

return AllocReturn(n->GetConcurrency(), immediate_result);

return AllocReturn(n != nullptr && n->GetConcurrency(), immediate_result);
}

EvaluableNodeReference Interpreter::InterpretNode_ENT_SET_CONCURRENCY(EvaluableNode *en, bool immediate_result)
Expand Down
21 changes: 8 additions & 13 deletions src/Amalgam/interpreter/InterpreterOpcodesTransformations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_REWRITE(EvaluableNode *en,
//get tree and make a copy so it can be modified in-place
auto to_modify = InterpretNode(ocn[1]);
if(to_modify == nullptr)
return EvaluableNodeReference::Null();
to_modify.SetReference(evaluableNodeManager->AllocNode(ENT_NULL), true);

if(!to_modify.unique)
to_modify = evaluableNodeManager->DeepAllocCopy(to_modify);
Expand Down Expand Up @@ -362,10 +362,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_FILTER(EvaluableNode *en,
//get list
auto list = InterpretNode(ocn[0]);
if(list == nullptr)
{
evaluableNodeManager->FreeNodeTreeIfPossible(list);
return EvaluableNodeReference::Null();
}

EvaluableNodeReference result_list(list, list.unique);

Expand Down Expand Up @@ -720,7 +717,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_REDUCE(EvaluableNode *en,
return EvaluableNodeReference::Null();

auto function = InterpretNodeForImmediateUse(ocn[0]);
if(function == nullptr)
if(EvaluableNode::IsEmptyNode(function))
return EvaluableNodeReference::Null();

auto node_stack = CreateInterpreterNodeStackStateSaver(function);
Expand Down Expand Up @@ -786,7 +783,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_APPLY(EvaluableNode *en, b
//get the target
auto source = InterpretNode(ocn[1]);
if(source == nullptr)
return EvaluableNodeReference::Null();
source.SetReference(evaluableNodeManager->AllocNode(ENT_NULL));

evaluableNodeManager->EnsureNodeIsModifiable(source);

Expand Down Expand Up @@ -878,7 +875,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_SORT(EvaluableNode *en, bo
{
//get list
auto list = InterpretNode(ocn[list_index]);
if(list == nullptr)
if(EvaluableNode::IsEmptyNode(list))
return EvaluableNodeReference::Null();

//make sure it is an editable copy
Expand Down Expand Up @@ -926,7 +923,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_SORT(EvaluableNode *en, bo

//get list
auto list = InterpretNode(ocn[list_index]);
if(list == nullptr)
if(EvaluableNode::IsEmptyNode(list))
return EvaluableNodeReference::Null();

//make sure it is an editable copy
Expand Down Expand Up @@ -1584,7 +1581,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ZIP(EvaluableNode *en, boo

//attempt to get indices, the keys of the assoc
auto index_list = InterpretNodeForImmediateUse(ocn[index_list_index]);
if(index_list == nullptr)
if(EvaluableNode::IsEmptyNode(index_list))
{
EvaluableNodeReference result(evaluableNodeManager->AllocNode(ENT_ASSOC), true);
return result;
Expand Down Expand Up @@ -1617,8 +1614,6 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_ZIP(EvaluableNode *en, boo
{
//convert index to string
EvaluableNode *index = index_list_ocn[i];
if(index == nullptr)
continue;

//obtain the index, reusing the sid reference if possible
StringInternPool::StringID index_sid = string_intern_pool.EMPTY_STRING_ID;
Expand Down Expand Up @@ -1691,7 +1686,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_UNZIP(EvaluableNode *en, b
return EvaluableNodeReference::Null();

auto zipped = InterpretNode(ocn[0]);
if(zipped == nullptr)
if(EvaluableNode::IsEmptyNode(zipped))
return EvaluableNodeReference(evaluableNodeManager->AllocNode(ENT_LIST), true);

auto node_stack = CreateInterpreterNodeStackStateSaver(zipped);
Expand All @@ -1700,7 +1695,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_UNZIP(EvaluableNode *en, b

EvaluableNodeReference result(evaluableNodeManager->AllocNode(ENT_LIST), true);

if(index_list == nullptr)
if(EvaluableNode::IsEmptyNode(index_list))
return result;

auto &index_list_ocn = index_list->GetOrderedChildNodes();
Expand Down
Loading

0 comments on commit 4c7d8e3

Please sign in to comment.