4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/interpret/builtins/AstFormationBuilder.h"
10
#include "simodo/bormental/DrBormental.h"
12
namespace simodo::interpret::builtins
16
parser::SyntaxDataCollector_null null_data_collector;
19
AstFormationBuilder::AstFormationBuilder(inout::Reporter_abstract & reporter,
20
BaseInterpret_abstract & sbl_host,
21
variable::VariableSet_t & hosts)
25
, _syntax_data_collector(null_data_collector)
27
_ast_formation_module = std::make_shared<AstFormationModule>(_hosts);
29
/// \todo Пересмотреть странную передачу самого себя в свой же метод.
30
/// PVS Studio: warn V678 An object is used as an argument to its own method.
31
/// Consider checking the first actual argument of the 'instantiate' function.
32
_sbl_host.importNamespace(u"ast",
33
_ast_formation_module->instantiate(_ast_formation_module),
34
inout::null_token_location);
37
AstFormationBuilder::AstFormationBuilder(inout::Reporter_abstract & reporter,
38
BaseInterpret_abstract & sbl_host,
39
variable::VariableSet_t & hosts,
40
parser::SyntaxDataCollector_interface & syntax_data_collector)
44
, _syntax_data_collector(syntax_data_collector)
46
_ast_formation_module = std::make_shared<AstFormationModule>(_hosts);
48
/// \todo Пересмотреть странную передачу самого себя в свой же метод.
49
/// PVS Studio: warn V678 An object is used as an argument to its own method.
50
/// Consider checking the first actual argument of the 'instantiate' function.
51
_sbl_host.importNamespace(u"ast",
52
_ast_formation_module->instantiate(_ast_formation_module),
53
inout::null_token_location);
56
AstFormationBuilder::~AstFormationBuilder()
60
bool AstFormationBuilder::weave(const ast::Node & code)
62
_sbl_host.inter().addFlowLayer(code);
64
loom::FiberStatus status;
66
while((status=_sbl_host.inter().executeOperation()) == loom::FiberStatus::Flow)
69
return loom::FiberStatus::Complete == status;
72
bool AstFormationBuilder::onStart(const std::map<std::u16string, ast::Node> &handlers)
74
if (InterpretState::Flow != _sbl_host.before_start())
77
auto it = handlers.find(u"onStart");
79
if (it == handlers.end())
82
return weave(it->second);
85
#ifdef AST_BUILDER_DEBUG
86
bool AstFormationBuilder::onProduction(size_t ,
88
const inout::Token & production,
89
const std::vector<inout::Token> & pattern,
90
const ast::Node & action,
94
bool AstFormationBuilder::onProduction(
95
const inout::Token & production,
96
const std::vector<inout::Token> & pattern,
97
const ast::Node & action)
100
if (action.branches().empty())
103
_ast_formation_module->setCurrentProduction(&production);
104
_ast_formation_module->setCurrentPattern(&pattern);
106
bool ok = weave(action);
108
_ast_formation_module->setCurrentProduction(nullptr);
109
_ast_formation_module->setCurrentPattern(nullptr);
114
void AstFormationBuilder::onTerminal(const inout::Token & token)
116
_syntax_data_collector.collectToken(token);
119
bool AstFormationBuilder::onFinish(bool , const std::map<std::u16string, ast::Node> &handlers)
121
_sbl_host.before_finish(InterpretState::Flow);
123
auto it = handlers.find(u"onFinish");
125
if (it != handlers.end())
126
if (!weave(it->second))