summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/funcs.cpp
blob: 72d3c27782471250e0ec0a3a536496fa7a1fbcd8 (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
#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 nameref;
		name_t()
		: nameref(level0::alloc())
		{
			level0::ref namestr = alloc((std::string)("name"));
			nameref.set(nameref, namestr);
			conceptsByName.emplace(namestr.val<std::string>(), nameref);
		}
	} namestruct;
	return namestruct;
}

concept* getnamed(std::string const & name)
{
	auto & ns = namestruct();
	auto res = ns.conceptsByName.find(name);
	if (res != ns.conceptsByName.end()) {
		return res->second;
	} else {
		level1::ref con = level0::alloc();
		level0::ref namestr = level0::alloc(name);
		ns.conceptsByName.emplace(namestr.val<std::string>(), con);
		con.set(ns.nameref, namestr);
		return con.ptr();
	}
}

std::string& getname(concept* r)
{
	try {
		return r->vget<std::string>(namestruct().nameref);
	} catch(level0::no_such_link_type&) {
		return getname(ref("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(std::any val)
{

	ref ret = level0::alloc(val);
	std::stringstream ss;
	ss << val.type().name() << "(";
	if (false);
#define t(T) \
		else if (val.type() == typeid(T)) { \
			ss << ret.val<T>(); \
		}
	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(std::string) t(char const *)
#undef t
	else { ss << "?"; }
	ss << ")";
	ret.link(concepts::name, level0::alloc(ss.str()));
	return ret;
}

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

std::string dump(concept* what, concept* skipmarkertype, concept* skipmarkertarget)
{
	if (what->linked(skipmarkertype, skipmarkertarget)) {
		return {};
	}
	std::string ret;
	for (auto & link : ref(what).links()) {
		if (link.first == concepts::name) { continue; }
		if (ret.size() == 0) {
			ret = ref(what).name() + ":\n";
		}
		ret += "  " + link.first.name() + ": " + link.second.name() + "\n";
	}
	what->link(skipmarkertype, skipmarkertarget);
	for (auto & link : ref(what).links()) {
		if (link.first.ptr() == skipmarkertype && link.second.ptr() == skipmarkertarget) {
			continue;
		}
		ret += dump(link.second, skipmarkertype, skipmarkertarget);
	}
	return ret;
}

}
}