2020-05-02 23:54:00 +00:00
|
|
|
/* Copyright 2020, Michele Santullo
|
|
|
|
* This file is part of wrenpp.
|
|
|
|
*
|
|
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "wrenpp/def_configuration.hpp"
|
|
|
|
#include "wrenpp/vm_fun.hpp"
|
|
|
|
#include <string_view>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
constexpr const char g_calendar_src[] =
|
|
|
|
"foreign class Calendar {" R"script(
|
|
|
|
construct new() {}
|
|
|
|
foreign static today()
|
|
|
|
foreign add_appointment(desc)
|
2020-05-03 10:23:06 +00:00
|
|
|
foreign appointment_count()
|
2020-05-02 23:54:00 +00:00
|
|
|
}
|
|
|
|
)script";
|
|
|
|
|
|
|
|
constexpr const char g_test_script[] =
|
|
|
|
R"script(import "calendar" for Calendar
|
|
|
|
var cale = Calendar.new()
|
|
|
|
System.print("Today is %(Calendar.today())")
|
|
|
|
cale.add_appointment("go get a haircut")
|
2020-05-03 10:23:06 +00:00
|
|
|
System.print("You have %(cale.appointment_count()) appointment(s)")
|
2020-05-02 23:54:00 +00:00
|
|
|
)script";
|
|
|
|
|
|
|
|
auto today_unique_str() {
|
|
|
|
std::time_t now = std::time(nullptr);
|
|
|
|
constexpr const std::size_t buff_sz = 1024;
|
|
|
|
auto mem(std::make_unique<char[]>(buff_sz));
|
|
|
|
const std::size_t sz = std::strftime(mem.get(), buff_sz, "%A %-e %B %Y", std::localtime(&now));
|
|
|
|
if (not sz)
|
|
|
|
std::strcpy(mem.get(), "n/a");
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Calendar {
|
|
|
|
public:
|
|
|
|
Calendar() {
|
|
|
|
//std::cout << "Calendar initialized\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
~Calendar() {
|
|
|
|
try {
|
|
|
|
//std::cout << "Calendar destroyed\n";
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void today (wren::VM& vm) {
|
|
|
|
vm.set_slot_string(0, today_unique_str().get());
|
|
|
|
}
|
|
|
|
|
2020-05-03 10:23:06 +00:00
|
|
|
void add_appointment (std::string&& appointment) {
|
|
|
|
m_appointments.push_back(std::move(appointment));
|
2020-05-02 23:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_appointments() const {
|
|
|
|
std::cout << "Appointments for " << today_unique_str().get() << ":\n";
|
|
|
|
if (m_appointments.empty()) {
|
|
|
|
std::cout << "\t[none]\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (const auto& app : m_appointments) {
|
|
|
|
std::cout << '\t' << app << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 10:23:06 +00:00
|
|
|
std::size_t appointment_count() const {
|
|
|
|
return m_appointments.size();
|
|
|
|
}
|
|
|
|
|
2020-05-02 23:54:00 +00:00
|
|
|
private:
|
|
|
|
std::vector<std::string> m_appointments;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyConf : public wren::DefConfiguration {
|
|
|
|
public:
|
|
|
|
char* load_module_fn(wren::VM* vm, const char* module_name) {
|
|
|
|
if (std::string_view{module_name} == "calendar") {
|
|
|
|
constexpr const std::size_t buff_sz = sizeof(g_calendar_src);
|
|
|
|
char* const buff = static_cast<char*>(MyConf::reallocate_fn(nullptr, buff_sz));
|
|
|
|
std::copy(g_calendar_src, g_calendar_src + buff_sz / sizeof(g_calendar_src[0]), buff);
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
wren::foreign_method_t foreign_method_fn(wren::VM* vm, const char* m, const char* c, bool is_static, const char* s) {
|
|
|
|
std::string_view module(m);
|
|
|
|
std::string_view class_name(c);
|
|
|
|
std::string_view signature(s);
|
|
|
|
|
|
|
|
if (module == "calendar" and class_name == "Calendar") {
|
|
|
|
if (is_static and signature == "today()")
|
|
|
|
return &Calendar::today;
|
|
|
|
else if (not is_static and signature == "add_appointment(_)")
|
|
|
|
return wren::make_method_bindable<&Calendar::add_appointment>();
|
2020-05-03 10:23:06 +00:00
|
|
|
else if (not is_static and signature == "appointment_count()")
|
|
|
|
return wren::make_method_bindable<&Calendar::appointment_count>();
|
2020-05-02 23:54:00 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
wren::foreign_class_t foreign_class_fn(wren::VM* vm, const char* m, const char* c) {
|
|
|
|
std::string_view module(m);
|
|
|
|
std::string_view class_name(c);
|
|
|
|
|
|
|
|
if (module == "calendar" and class_name == "Calendar") {
|
|
|
|
return wren::make_foreign_class<Calendar>();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {nullptr, nullptr};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
int main() {
|
2020-05-03 09:32:43 +00:00
|
|
|
typedef wren::ModuleAndName MN;
|
|
|
|
|
2020-05-02 23:54:00 +00:00
|
|
|
MyConf conf;
|
|
|
|
wren::VM vm(&conf, nullptr);
|
|
|
|
vm.interpret("main", g_test_script);
|
|
|
|
|
2020-05-03 09:32:43 +00:00
|
|
|
Calendar* const cale = std::get<0>(wren::variables<Calendar>(vm, MN{"main", "cale"}));
|
2020-05-02 23:54:00 +00:00
|
|
|
cale->print_appointments();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|