summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/funcs.cpp
blob: 07f0eba7ee8de555940c0950f641787ccb511da4 (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
#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));
}

}
}