Enhanced PythonEvaluator to accept concepts
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept
|
||||
from core.concept import ConceptParts
|
||||
import core.builtin_helpers
|
||||
from parsers.BaseParser import BaseParser, Node, NopNode, ErrorNode, NotInitializedNode
|
||||
from core.tokenizer import Tokenizer, TokenKind, Token, Keywords
|
||||
from dataclasses import dataclass, field
|
||||
@@ -409,8 +410,8 @@ class DefaultParser(BaseParser):
|
||||
continue
|
||||
|
||||
# ask the other parsers if they recognize the tokens
|
||||
new_context = self.context.push(self)
|
||||
parsing_result = self.sheerka.expect_one(new_context, self.sheerka.parse(new_context, tokens))
|
||||
new_context = self.context.push(self.name)
|
||||
parsing_result = core.builtin_helpers.expect_one(new_context, self.sheerka.parse(new_context, tokens))
|
||||
if not parsing_result.status:
|
||||
self.add_error(parsing_result.value)
|
||||
continue
|
||||
|
||||
@@ -103,28 +103,6 @@ class PythonParser(BaseParser):
|
||||
except Exception as error:
|
||||
return False, None, error
|
||||
|
||||
def expr_to_expression(self, expr):
|
||||
expr.lineno = 0
|
||||
expr.col_offset = 0
|
||||
result = ast.Expression(expr.value, lineno=0, col_offset=0)
|
||||
|
||||
return result
|
||||
|
||||
def exec_with_return(self, code):
|
||||
code_ast = ast.parse(code)
|
||||
|
||||
init_ast = copy.deepcopy(code_ast)
|
||||
init_ast.body = code_ast.body[:-1]
|
||||
|
||||
last_ast = copy.deepcopy(code_ast)
|
||||
last_ast.body = code_ast.body[-1:]
|
||||
|
||||
exec(compile(init_ast, "<ast>", "exec"), globals())
|
||||
if type(last_ast.body[0]) == ast.Expr:
|
||||
return eval(compile(self.expr_to_expression(last_ast.body[0]), "<ast>", "eval"), globals())
|
||||
else:
|
||||
exec(compile(last_ast, "<ast>", "exec"), globals())
|
||||
|
||||
|
||||
class PythonGetNamesVisitor(ast.NodeVisitor):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user