Fixed some bugs

This commit is contained in:
2020-08-27 18:54:28 +02:00
parent 351c16f946
commit 37cd3ed757
27 changed files with 685 additions and 189 deletions
+50 -11
View File
@@ -1,13 +1,13 @@
from dataclasses import dataclass
import core.utils
from core.builtin_concepts import BuiltinConcepts
from core.builtin_helpers import only_successful, parse_unrecognized, get_lexer_nodes
from core.concept import Concept
from parsers.BaseNodeParser import ConceptNode, UnrecognizedTokensNode, SourceCodeNode, SourceCodeWithConceptNode
from parsers.BaseParser import BaseParser, ErrorNode
from core.builtin_helpers import only_successful, parse_unrecognized, get_lexer_nodes
import core.utils
PARSERS = ["EmptyString", "AtomNode", "BnfNode", "SyaNode", "Python"]
PARSERS = ["EmptyString", "ShortTermMemory", "AtomNode", "BnfNode", "SyaNode", "Python"]
@dataclass()
@@ -64,7 +64,18 @@ class UnrecognizedNodeParser(BaseParser):
elif isinstance(node, SourceCodeNode):
sequences_found = core.utils.product(sequences_found, [node])
has_unrecognized = True # never trust source code not. I may be an invalid source code
has_unrecognized = True # to let PythonWithConceptParser validate the code
elif isinstance(node, SourceCodeWithConceptNode):
for i, n in [(i, n) for i, n in enumerate(node.nodes) if isinstance(n, ConceptNode)]:
res = self.validate_concept_node(context, n)
if not res.status:
self.add_error(res.body)
break
else:
node.nodes[i] = res.body
sequences_found = core.utils.product(sequences_found, [node])
has_unrecognized = True # to let PythonWithConceptParser validate the code
else: # cannot happen as of today :-)
raise NotImplementedError(f"Node is {type(node)}, which is not supported yet")
@@ -104,19 +115,47 @@ class UnrecognizedNodeParser(BaseParser):
:param concept:
:return:
"""
for name, value in concept.compiled.items():
if isinstance(value, Concept):
_validate_concept(value)
for k, v in concept.compiled.items():
if isinstance(v, Concept):
_validate_concept(v)
elif isinstance(value, UnrecognizedTokensNode):
res = parse_unrecognized(context, value.source, PARSERS)
elif isinstance(v, UnrecognizedTokensNode):
res = parse_unrecognized(context, v.source, PARSERS)
res = only_successful(context, res) # only key successful parsers
if res.status:
concept.compiled[name] = res.body.body
concept.compiled[k] = res.body.body
else:
errors.append(sheerka.new(BuiltinConcepts.ERROR, body=f"Cannot parse '{value.source}'"))
errors.append(sheerka.new(BuiltinConcepts.ERROR, body=f"Cannot parse '{v.source}'"))
def _get_source(compiled, var_name):
if var_name not in compiled:
return None
if not isinstance(compiled[var_name], list):
return None
if not len(compiled[var_name]) == 1:
return None
if not sheerka.isinstance(compiled[var_name][0], BuiltinConcepts.RETURN_VALUE):
return None
if not sheerka.isinstance(compiled[var_name][0].body, BuiltinConcepts.PARSER_RESULT):
return None
if compiled[var_name][0].body.name == "parsers.ShortTermMemory":
return None
return compiled[var_name][0].body.source
_validate_concept(concept_node.concept)
# Special case where the values of the variables are the names of the variable
# example : Concept("a plus b").def_var("a").def_var("b")
# and the user has entered 'a plus b'
# Chances are that we are talking about the concept itself, and not an instantiation (like '10 plus 2')
# This means that 'a' and 'b' don't have any real value
for name, value in concept_node.concept.metadata.variables:
if not _get_source(concept_node.concept.compiled, name) == name:
break
else:
concept_node.concept.metadata.is_evaluated = True
if len(errors) > 0:
return context.sheerka.ret(self.name, False, errors)
else: