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
_sbl_host.importNamespace(u"ast",
30
_ast_formation_module->instantiate(_ast_formation_module),
31
inout::null_token_location);
34
AstFormationBuilder::AstFormationBuilder(inout::Reporter_abstract & reporter,
35
BaseInterpret_abstract & sbl_host,
36
variable::VariableSet_t & hosts,
37
parser::SyntaxDataCollector_interface & syntax_data_collector)
41
, _syntax_data_collector(syntax_data_collector)
43
_ast_formation_module = std::make_shared<AstFormationModule>(_hosts);
45
_sbl_host.importNamespace(u"ast",
46
_ast_formation_module->instantiate(_ast_formation_module),
47
inout::null_token_location);
50
AstFormationBuilder::~AstFormationBuilder()
54
bool AstFormationBuilder::weave(const ast::Node & code)
56
_sbl_host.inter().addFlowLayer(code);
58
loom::FiberStatus status;
60
while((status=_sbl_host.inter().executeOperation()) == loom::FiberStatus::Flow)
63
return loom::FiberStatus::Complete == status;
66
bool AstFormationBuilder::onStart(const std::map<std::u16string, ast::Node> &handlers)
68
if (InterpretState::Flow != _sbl_host.before_start())
71
auto it = handlers.find(u"on_Start");
73
if (it == handlers.end())
76
return weave(it->second);
79
#ifdef AST_BUILDER_DEBUG
80
bool AstFormationBuilder::onProduction(size_t ,
82
const inout::Token & production,
83
const std::vector<inout::Token> & pattern,
84
const ast::Node & action,
88
bool AstFormationBuilder::onProduction(
89
const inout::Token & production,
90
const std::vector<inout::Token> & pattern,
91
const ast::Node & action)
94
if (action.branches().empty())
97
_ast_formation_module->setCurrentProduction(&production);
98
_ast_formation_module->setCurrentPattern(&pattern);
100
bool ok = weave(action);
102
_ast_formation_module->setCurrentProduction(nullptr);
103
_ast_formation_module->setCurrentPattern(nullptr);
108
void AstFormationBuilder::onTerminal(const inout::Token & token)
110
_syntax_data_collector.collectToken(token);
113
bool AstFormationBuilder::onFinish(bool , const std::map<std::u16string, ast::Node> &handlers)
115
_sbl_host.before_finish(InterpretState::Flow);
117
auto it = handlers.find(u"on_Finish");
119
if (it != handlers.end())
120
if (!weave(it->second))