Implemented FunctionParser

This commit is contained in:
2020-09-17 14:11:09 +02:00
parent 8a866880bc
commit 177a6b1d5f
40 changed files with 1752 additions and 561 deletions
+4 -51
View File
@@ -2,7 +2,7 @@ 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.builtin_helpers import only_successful, parse_unrecognized, get_lexer_nodes, update_compiled
from core.concept import Concept
from parsers.BaseNodeParser import ConceptNode, UnrecognizedTokensNode, SourceCodeNode, SourceCodeWithConceptNode
from parsers.BaseParser import BaseParser, ErrorNode
@@ -38,6 +38,7 @@ class UnrecognizedNodeParser(BaseParser):
sequences_found = [[]]
has_unrecognized = False
self.error_sink.clear()
for node in nodes:
if isinstance(node, ConceptNode):
@@ -93,7 +94,7 @@ class UnrecognizedNodeParser(BaseParser):
sheerka.new(
BuiltinConcepts.PARSER_RESULT,
parser=self,
source=parser_input,
source=parser_input.source,
body=choice,
try_parsed=choice)))
@@ -105,56 +106,8 @@ class UnrecognizedNodeParser(BaseParser):
return ret
def validate_concept_node(self, context, concept_node):
sheerka = context.sheerka
errors = []
def _validate_concept(concept):
"""
Recursively browse the compiled properties in order to find unrecognized
:param concept:
:return:
"""
for k, v in concept.compiled.items():
if isinstance(v, Concept):
_validate_concept(v)
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[k] = res.body.body
else:
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
update_compiled(context, concept_node.concept, errors)
if len(errors) > 0:
return context.sheerka.ret(self.name, False, errors)