4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/interpret/builtins/modules/AstFormationModule.h"
10
#include "simodo/variable/VariableSetWrapper.h"
11
#include "simodo/parser/fuze/BaseOperationCode.h"
13
#include "simodo/bormental/DrBormental.h"
15
// #include "simodo/interpret/script/ScriptOperationCode.h"
21
namespace simodo::interpret::builtins
26
AReporter * logger_ = nullptr;
29
variable::Value addNode(variable::Module_interface * host, const variable::VariableSetWrapper & args)
32
if (logger_ != nullptr)
33
logger_->reportInformation(u"/tAststb::addNode");
35
assert(host != nullptr);
36
AstFormationModule & stb = *(static_cast<AstFormationModule *>(host));
38
const variable::Variable & host_value = args[0].origin();
39
const variable::Variable & op_value = args[1].origin();
40
const variable::Variable & index_value = args[2].origin();
42
if (index_value.type() == variable::ValueType::String)
44
std::u16string value { get<std::u16string>(index_value.variant()) };
45
inout::Token token { inout::LexemeType::Id, value, args[2].origin().location() };
47
stb().addNode(get<std::u16string>(host_value.variant()),
48
static_cast<ast::OperationCode>(get<int64_t>(op_value.variant())),
53
else if (index_value.type() == variable::ValueType::Int)
55
int64_t index = get<int64_t>(index_value.variant());
57
assert((&stb.current_pattern()) != nullptr);
58
assert((&stb.current_production()) != nullptr);
60
if (index < 0 || index >= static_cast<int64_t>(stb.current_pattern().size()))
61
throw bormental::DrBormental("addNode", "Выход индекса за пределы массива");
63
stb().addNode(get<std::u16string>(host_value.variant()),
64
static_cast<ast::OperationCode>(get<int64_t>(op_value.variant())),
65
stb.current_pattern()[static_cast<size_t>(index)],
66
stb.current_production()
70
throw bormental::DrBormental("addNode", "Недопустимый тип индекса");
75
variable::Value addNode_StepInto(variable::Module_interface * host, const variable::VariableSetWrapper & args)
78
if (logger_ != nullptr)
79
logger_->reportInformation(u"/tAststb::addNode_StepInto");
81
assert(host != nullptr);
82
AstFormationModule & stb = *(static_cast<AstFormationModule *>(host));
84
const variable::Variable & host_value = args[0].origin();
85
const variable::Variable & op_value = args[1].origin();
86
const variable::Variable & index_value = args[2].origin();
88
if (index_value.type() == variable::ValueType::String)
90
std::u16string value { get<std::u16string>(index_value.variant()) };
91
inout::Token token { inout::LexemeType::Id, value, args[2].origin().location() };
93
stb().addNode_StepInto(get<std::u16string>(host_value.variant()),
94
static_cast<ast::OperationCode>(get<int64_t>(op_value.variant())),
99
else if (index_value.type() == variable::ValueType::Int)
101
int64_t index = get<int64_t>(index_value.variant());
103
assert((&stb.current_pattern()) != nullptr);
104
assert((&stb.current_production()) != nullptr);
106
if (index < 0 || index >= static_cast<int64_t>(stb.current_pattern().size()))
107
throw bormental::DrBormental("addNode_StepInto", "Выход индекса за пределы массива");
109
stb().addNode_StepInto(get<std::u16string>(host_value.variant()),
110
static_cast<ast::OperationCode>(get<int64_t>(op_value.variant())),
111
stb.current_pattern()[static_cast<size_t>(index)],
112
stb.current_production()
116
throw bormental::DrBormental("addNode_StepInto", "Недопустимый тип индекса");
121
variable::Value goParent(variable::Module_interface * host, const variable::VariableSetWrapper & )
124
if (logger_ != nullptr)
125
logger_->reportInformation(u"/tAststb::goParent");
127
assert(host != nullptr);
128
AstFormationModule & stb = *(static_cast<AstFormationModule *>(host));
130
bool ok = stb().goParent();
133
throw bormental::DrBormental("goParent", "Недопустимое использование goParent");
138
// Variable setStreamNo(variable::Module_interface * host, const variable::VariableSetWrapper & args)
141
// if (logger_ != nullptr)
142
// logger_->reportInformation(u"/tAststb::setStreamNo");
144
// assert(host != nullptr);
145
// AstFormationModule & stb = *(static_cast<AstFormationModule *>(host));
147
// int64_t no = get<int64_t>(args[0].origin().variant());
149
// if (no < 0 || no > UINT16_MAX)
150
// throw Exception("setStreamNo", "Выход номера потока за лимиты");
152
// stb.setCurrentStreamNo(no);
154
// return error_variable();
157
// GrammarManagement_null gm_dummy;
159
static variable::VariableSet_t hosts;
162
using namespace simodo::variable;
164
AstFormationModule::AstFormationModule()
169
AstFormationModule::AstFormationModule(variable::VariableSet_t & hosts)
174
variable::Object AstFormationModule::instantiate(std::shared_ptr<variable::Module_interface> module_host)
176
variable::VariableSet_t ret {{
177
{u"addNode", {ValueType::Function, variable::Object {{
178
{u"@", ExternalFunction {{module_host}, addNode}},
179
{u"", ValueType::Null},
180
{u"host", ValueType::String},
181
{u"op", ValueType::Int},
182
// Неопределённый тип для индекса предполагает тип параметра 'any'
183
{u"index", ValueType::Null},
185
{u"addNode_StepInto", {ValueType::Function, variable::Object {{
186
{u"@", ExternalFunction {{module_host}, addNode_StepInto}},
187
{u"", ValueType::Null},
188
{u"host", ValueType::String},
189
{u"op", ValueType::Int},
190
// Неопределённый тип для индекса предполагает тип параметра 'any'
191
{u"index", ValueType::Null},
193
{u"goParent", {ValueType::Function, variable::Object {{
194
{u"@", ExternalFunction {{module_host}, goParent}},
195
{u"", ValueType::Null},
196
// {u"host", ValueType::String},
198
// {u"setStream", ValueType::Function, variable::Object {
199
// {u"@", ValueType::ExtFunction, ExternalFunction {{}, setStreamNo}},
200
// {u"", ValueType::Error, {}},
201
// {u"no", ValueType::Int, {}},
203
// {u"Stream", ValueType::Enum, variable::Object {
204
// {u"Default", ValueType::Int, static_cast<int64_t>(DEFAULT_STREAM_NO)},
205
// {u"Equation", ValueType::Int, static_cast<int64_t>(EQUATION_STREAM_NO)},
206
// {u"Diff", ValueType::Int, static_cast<int64_t>(DIFF_STREAM_NO)},
208
{u"op", variable::Object {{
209
{u"None", static_cast<int64_t>(parser::BaseOperationCode::None)},
210
{u"PushConstant", static_cast<int64_t>(parser::BaseOperationCode::PushConstant)},
211
{u"PushVariable", static_cast<int64_t>(parser::BaseOperationCode::PushVariable)},
212
{u"ObjectElement", static_cast<int64_t>(parser::BaseOperationCode::ObjectElement)},
213
{u"FunctionCall", static_cast<int64_t>(parser::BaseOperationCode::FunctionCall)},
214
{u"ProcedureCheck", static_cast<int64_t>(parser::BaseOperationCode::ProcedureCheck)},
215
{u"Print", static_cast<int64_t>(parser::BaseOperationCode::Print)},
216
{u"Block", static_cast<int64_t>(parser::BaseOperationCode::Block)},
217
{u"Pop", static_cast<int64_t>(parser::BaseOperationCode::Pop)},
220
{u"host", variable::Object {{
226
ret.insert(ret.end(), _hosts.begin(), _hosts.end());
231
// variable::ModuleFactory_interface* AstFormationModule::factory()
233
// // std::cout << "factory" << std::endl;