I can also get concept by name
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
from core.ast.nodes import python_to_concept
|
||||
from core.builtin_concepts import ParserResultConcept, ReturnValueConcept, BuiltinConcepts
|
||||
from core.builtin_helpers import get_names
|
||||
from core.concept import Concept
|
||||
from core.concept import Concept, DEFINITION_TYPE_BNF, DEFINITION_TYPE_DEF
|
||||
from core.tokenizer import TokenKind
|
||||
from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
from parsers.BaseParser import NotInitializedNode
|
||||
from parsers.ConceptLexerParser import ParsingExpression, ParsingExpressionVisitor
|
||||
from parsers.DefaultParser import DefConceptNode
|
||||
from parsers.DefaultParser import DefConceptNode, NameNode
|
||||
from parsers.PythonParser import PythonNode
|
||||
import core.utils
|
||||
|
||||
@@ -55,22 +56,33 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
props_found = set()
|
||||
|
||||
concept = Concept(def_concept_node.name)
|
||||
for prop in ("definition", "where", "pre", "post", "body"):
|
||||
# put back the sources
|
||||
part_ret_val = getattr(def_concept_node, prop)
|
||||
if not isinstance(part_ret_val, ReturnValueConcept) or not part_ret_val.status:
|
||||
continue # Nothing to do is not initialized
|
||||
concept.metadata.definition_type = def_concept_node.definition_type
|
||||
name_to_use = self.get_name_to_use(def_concept_node)
|
||||
|
||||
# update the metadata
|
||||
source = self.get_source(part_ret_val)
|
||||
for prop in ("definition", "where", "pre", "post", "body"):
|
||||
|
||||
part_ret_val = getattr(def_concept_node, prop)
|
||||
|
||||
# put back the sources
|
||||
if isinstance(part_ret_val, NotInitializedNode):
|
||||
continue
|
||||
elif isinstance(part_ret_val, NameNode):
|
||||
source = str(part_ret_val)
|
||||
elif isinstance(part_ret_val, ReturnValueConcept) and part_ret_val.status:
|
||||
source = part_ret_val.value.source
|
||||
else:
|
||||
raise Exception("Unexpected")
|
||||
setattr(concept.metadata, prop, source)
|
||||
|
||||
# Do not try to resolve variables from itself
|
||||
if prop == "definition" and concept.metadata.definition_type == DEFINITION_TYPE_DEF:
|
||||
continue
|
||||
|
||||
# try to find what can be a property
|
||||
concept_name = [part.value for part in core.utils.strip_tokens(def_concept_node.name.tokens, True)]
|
||||
for p in self.get_props(sheerka, part_ret_val, concept_name):
|
||||
for p in self.get_props(sheerka, part_ret_val, name_to_use):
|
||||
props_found.add(p)
|
||||
|
||||
# add props order by appearance when possible
|
||||
# add props by order of appearance when possible
|
||||
for token in def_concept_node.name.tokens:
|
||||
if token.value in props_found:
|
||||
concept.def_prop(token.value, None)
|
||||
@@ -80,10 +92,15 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
if p not in concept.props:
|
||||
concept.def_prop(p, None)
|
||||
|
||||
# finish initialisation
|
||||
concept.init_key(def_concept_node.name.tokens)
|
||||
# initialize the key
|
||||
key_source = def_concept_node.definition.tokens if \
|
||||
def_concept_node.definition_type == DEFINITION_TYPE_DEF else \
|
||||
def_concept_node.name.tokens
|
||||
concept.init_key(key_source)
|
||||
|
||||
# update the bnf definition if needed
|
||||
if not isinstance(def_concept_node.definition, NotInitializedNode) and \
|
||||
sheerka.is_success(def_concept_node.definition):
|
||||
def_concept_node.definition_type == DEFINITION_TYPE_BNF:
|
||||
concept.bnf = def_concept_node.definition.value.value
|
||||
|
||||
ret = sheerka.create_new_concept(context, concept)
|
||||
@@ -93,8 +110,9 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
return sheerka.ret(self.name, ret.status, ret.value, parents=[return_value])
|
||||
|
||||
@staticmethod
|
||||
def get_source(ret_value):
|
||||
return ret_value.value.source
|
||||
def get_name_to_use(node):
|
||||
source = node.definition if node.definition_type == DEFINITION_TYPE_DEF else node.name
|
||||
return [part.value for part in core.utils.strip_tokens(source.tokens, True)]
|
||||
|
||||
@staticmethod
|
||||
def get_props(sheerka, ret_value, concept_name):
|
||||
@@ -104,6 +122,15 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
I guess that it can only be complete when will we have access to Sheerka memory
|
||||
"""
|
||||
|
||||
#
|
||||
# Case of NameNode
|
||||
#
|
||||
if isinstance(ret_value, NameNode):
|
||||
names = [str(t.value) for t in ret_value.tokens if t.type in (
|
||||
TokenKind.IDENTIFIER, TokenKind.STRING, TokenKind.KEYWORD)]
|
||||
variables = filter(lambda x: x in concept_name, names)
|
||||
return list(variables)
|
||||
|
||||
#
|
||||
# Case of python code
|
||||
#
|
||||
@@ -111,8 +138,8 @@ class AddConceptEvaluator(OneReturnValueEvaluator):
|
||||
if len(concept_name) > 1:
|
||||
python_node = ret_value.value.value
|
||||
as_concept_node = python_to_concept(python_node.ast_)
|
||||
variables = get_names(sheerka, as_concept_node)
|
||||
variables = filter(lambda x: x in concept_name, variables)
|
||||
names = get_names(sheerka, as_concept_node)
|
||||
variables = filter(lambda x: x in concept_name, names)
|
||||
return list(variables)
|
||||
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user