4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/variable/Module_interface.h"
10
#include "simodo/variable/VariableSetWrapper.h"
11
#include "simodo/interpret/Interpret_interface.h"
18
// MinGW related workaround
19
#define BOOST_DLL_FORCE_ALIAS_INSTANTIATION
22
#include <boost/dll/alias.hpp>
24
using namespace simodo;
25
using namespace simodo::variable;
29
Value createInt(Module_interface * host, const VariableSetWrapper & args);
30
Value createIntOne(Module_interface * host, const VariableSetWrapper & args);
31
Value createAny(Module_interface * host, const VariableSetWrapper & args);
34
class Array_module : public Module_interface
37
virtual version_t version() const override { return lib_version(); }
39
virtual Value instantiate(std::shared_ptr<Module_interface> module_object) override
42
{u"int", {ValueType::Function, Object {{
43
{u"@", ExternalFunction {module_object, ::createInt}},
44
{{}, ValueType::Array},
45
{u"size", ValueType::Int},
47
{u"int1", {ValueType::Function, Object {{
48
{u"@", ExternalFunction {module_object, ::createIntOne}},
49
{{}, ValueType::Array},
51
{u"any", {ValueType::Function, Object {{
52
{u"@", ExternalFunction {module_object, ::createAny}},
53
{{}, ValueType::Array},
54
{u"size", ValueType::Int},
55
{u"type", ValueType::Null},
61
static std::shared_ptr<Module_interface> create(interpret::Interpret_interface * ) {
62
return std::make_shared<Array_module>();
67
Array_module::create, // <-- this function is exported with...
68
create_simodo_module // <-- ...this alias name
73
Value createInt(Module_interface * host, const VariableSetWrapper & args)
78
assert(args.size() == 1);
80
int64_t size = args[0].origin().value().getInt();
82
if (size <= 0 || size > 32654)
83
throw std::invalid_argument("Invalid argument");
85
std::vector<Value> array(size, 0);
90
Value createIntOne(Module_interface * host, const VariableSetWrapper & args)
95
assert(args.size() == 0);
97
std::vector<Value> array {{0}};
102
Value createAny(Module_interface * host, const VariableSetWrapper & args)
107
assert(args.size() == 2);
109
int64_t size = args[0].origin().value().getInt();
110
const Value & type = args[1].origin().value();
112
if (size <= 0 || size > 32654)
113
throw std::invalid_argument("Invalid argument");
115
std::vector<Value> array;
119
for(int64_t i=0; i < size; ++i)
120
array.push_back(type.copy());
122
// std::cerr << "*** module.init out" << std::endl;