Fixed #41 : Implement concept 'and'
This commit is contained in:
@@ -18,6 +18,22 @@ EVAL_STEPS = PARSE_STEPS + [BuiltinConcepts.BEFORE_EVALUATION, BuiltinConcepts.E
|
||||
PARSERS = ["EmptyString", "ShortTermMemory", "Sequence", "Bnf", "Sya", "Python"]
|
||||
|
||||
|
||||
def remove_python_nodes(context, return_values):
|
||||
"""
|
||||
Try to reduce the number of return_values by removing return values with python node
|
||||
:param context:
|
||||
:param return_values:
|
||||
:return:
|
||||
"""
|
||||
res = []
|
||||
for ret_val in return_values:
|
||||
value = context.sheerka.objvalue(ret_val)
|
||||
if not hasattr(value, "get_python_node"):
|
||||
res.append(ret_val)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def is_same_success(context, return_values):
|
||||
"""
|
||||
Returns True if all returns values are successful and have the same value
|
||||
@@ -89,12 +105,25 @@ def expect_one(context, return_values):
|
||||
|
||||
# too many winners, which one to choose ?
|
||||
if number_of_successful > 1:
|
||||
# first, try to remove python node results.
|
||||
# In case of conflict, the concept take precedence over the natural Python result
|
||||
# as it is considered as an override (overload ?)
|
||||
successful_results = remove_python_nodes(context, successful_results)
|
||||
|
||||
if len(successful_results) == 1:
|
||||
return sheerka.ret(
|
||||
context.who,
|
||||
True,
|
||||
successful_results[0].body,
|
||||
parents=return_values)
|
||||
|
||||
if is_same_success(context, successful_results):
|
||||
return sheerka.ret(
|
||||
context.who,
|
||||
True,
|
||||
successful_results[0].value,
|
||||
parents=return_values)
|
||||
|
||||
else:
|
||||
if context.logger and context.logger.isEnabledFor(logging.DEBUG):
|
||||
context.log(f"Too many successful results found by expect_one()", context.who)
|
||||
|
||||
Reference in New Issue
Block a user