summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/funcs.cpp
blob: 1e6bd9dbea03a1402b1a3532d05be1a5c6f66cdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "funcs.hpp"

#include "../level-0/errors.hpp"
#include "../level-0/memorystore.hpp"
#include "concepts.hpp"

#include <unordered_map>

namespace intellect {
namespace level1 {

using namespace concepts;

// ensure name link and backing structure are created prior to first use
static auto & namestruct()
{
	static struct name_t
	{
		std::unordered_map<std::string,ref,std::hash<std::string>,std::equal_to<std::string>> conceptsByName;
		ref level1allocationsref, nameref, isref;
		ref level1ref;
		name_t()
		: level1allocationsref(level0::basic_alloc()),
		  nameref(level0::alloc(level1allocationsref)),
		  isref(level0::alloc(level1allocationsref)),
		  level1ref(level0::alloc(level1allocationsref))
		{
			give(level1allocationsref, "level1-allocations");
			give(nameref, "name");
			give(isref, "is");
			give(level1ref, "level1");
		}
		void give(concept* con, std::string const & name)
		{
			if (conceptsByName.count(name)) { throw std::logic_error(name + " already uniquely specified"); }
			level0::ref namestr = level0::alloc(con, name);
			namestr.link(isref, nameref);
			conceptsByName.emplace(namestr.val<std::string>(), con);
			con->set(nameref, namestr);
		}
	} namestruct;
	return namestruct;
}

void givename(concept* con, std::string const & name)
{
	auto & ns = namestruct();
	ns.give(con, name);
}

concept* getnamed(std::string const & name, concept* allocator)
{
	auto & ns = namestruct();
	auto res = ns.conceptsByName.find(name);
	if (res != ns.conceptsByName.end()) {
		return res->second;
	} else {
		if (allocator == nullptr) { allocator = level0::concepts::allocations(); }
		level1::ref con = level0::alloc(allocator);
		givename(con, name);
		return con.ptr();
	}
}

std::string nameforany(concept* c)
{
	using namespace std;
	stringstream ss;
	static int fn_counter = 0;
	if (false);
#define t(T) \
		else if (c->data.type() == typeid(T)) { \
			ss << #T "(" << std::any_cast<T>(c->data) << ")"; \
		}
	t(uint8_t) t(int8_t) t(uint16_t) t(int16_t)
	t(uint32_t) t(int32_t) t(uint64_t) t(int64_t)
	t(bool) t(float) t(double) t(string) t(char const *)
#undef t
#define t(T) \
		else if (c->data.type() == typeid(T)) { \
			ss << #T "(" << (fn_counter++) << ")"; \
		}
	t(function<void(void)>)
	t(function<level0::ref(level0::ref)>)
	t(function<level1::ref(level1::ref)>)
	t(function<level2::ref(level2::ref)>)
	t(function<level3::ref(level3::ref)>)
	t(function<level4::ref(level4::ref)>)
	t(function<level5::ref(level5::ref)>)
	t(function<level6::ref(level6::ref)>)
	t(function<level7::ref(level7::ref)>)
	t(function<level8::ref(level8::ref)>)
	t(function<level9::ref(level9::ref)>)
#undef t
	else { ss << "?"; }
	return ss.str();
}

std::string getname(concept* r)
{
	try {
		return r->vget<std::string>(namestruct().nameref);
	} catch(level0::no_such_link_type&) {
		if (r->data.has_value()) { return nameforany(r); }
		return "UNNAMED";
	}
}

bool isa(concept* member, concept* group)
{
	for (auto & g : member->getAll(is)) {
		if (g == group) return true;
		if (g == member) continue;
		if (isa(g, group)) return true;
	}
	return false;
}

concept* alloc(concept* allocator, std::any val)
{
	ref ret = level0::alloc(allocator, val);
	ref namestr = level0::alloc(ret, nameforany(ret));
	namestr.link(concepts::is, concepts::name);
	ret.link(concepts::name, level0::alloc(ret, nameforany(ret)));
	return ret;
}

concept* hyphenate(concept* a, concept* b)
{
	return getnamed(getname(a) + "-" + getname(b));
}

std::string dump(concept* what, concept* skipmarkertype, concept* skipmarkertarget)
{
	std::stringstream ss;
	if (what->linked(skipmarkertype, skipmarkertarget)) {
		return {};
	}
	for (auto & link : ref(what).links()) {
		if (link.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; }
		if (link.second.isa(concepts::name)) { continue; }
		if (ss.str().size() == 0) {
			ss << ref(what).name() << " " << (void*) what << ":\n";
		}
		ss << "  " << link.first.name() << ": " << link.second.name() << "\n";
	}
	what->link(skipmarkertype, skipmarkertarget);
	for (auto & link : ref(what).links()) {
		if (link.first.linked(allocator, level0-allocations)) { continue; }
		if (link.second.linked(allocator, level1-allocations)) { continue; }
		if (link.first.ptr() == skipmarkertype && link.second.ptr() == skipmarkertarget) {
			continue;
		}
		if (link.second.isa(concepts::name)) { continue; }
		ss << dump(link.second, skipmarkertype, skipmarkertarget);
	}
	return ss.str();
}

}
}