summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/funcs.cpp
blob: 3d0d88d02f8e04a103903a861215ecb9954d2d5f (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
#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())
		{
			auto namestr = valloc<std::string>("name");
			nameref.set(nameref, namestr);
			conceptsByName.emplace(namestr->data, 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::value<std::string>* namestr = level0::valloc(name);
		ns.conceptsByName.emplace(namestr->data, con);
		con.set(ns.nameref, namestr);
		return con.ptr();
	}
}

value<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;
}

template <typename T>
value<T>* valloc(T const & val)
{
	auto ret = level0::valloc<T>(val);
	std::stringstream ss;
	// << val is making recursion
	ss << typeid(T).name() << "(" << val << ")";
	ret->link(concepts::name, level0::valloc(ss.str()));
	return ret;
}

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

template value<std::string>* valloc<std::string>(std::string const & val);
template value<int>* valloc<int>(int const & val);

}
}