summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/sugar.cpp
blob: 21f400138816f51b8ba4caf5830b9e3a74fe4bfc (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
#include "sugar.hpp"

#include "concepts.hpp"

#include <stdexcept>

using namespace intellect::level1;
using namespace concepts;

namespace intellect {
namespace level1 {

ref a(ref group)
{
	static unsigned long long gid = 0;
	ref ret(group.name() + "-" + std::to_string(gid++));
	ret.link(is, group);
	ret.link(is, anonymous);
	return ret;
}
ref a(ref group, ref name)
{
	if (!name.isa(group)) {
		name.link(is, group);
	}
	return name;
}
ref an(ref group)
{
	return a(group);
}
ref an(ref group, ref name)
{
	return a(group, name);
}

bool isanonymous(ref topic)
{
	return topic.isa(concepts::anonymous);
}

ref movetoname(ref anonymous, ref name)
{
	if (!isanonymous(anonymous)) { throw std::invalid_argument("not anonymous"); }
	if (isanonymous(name)) { throw std::invalid_argument("not named"); }
	
	// this only provides for writing to empty concepts, because merging concepts is
	// best done with a knowledge of which links can be multiply attached, and that
	// information is not available at this level.
	bool nonempty = false;
	for (auto & l : name.links()) {
		if (l.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; }
		if (l.first == concepts::name) { continue; }
		nonempty = true;
	}
	if (nonempty) {
		for (auto & link : anonymous.links()) {
			if (link.first == concepts::is && link.second == concepts::anonymous) { continue; }
			if (link.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; }
			if (link.first == concepts::name) { continue; }
			if (!name.linked(link.first, link.second)) {
				throw std::logic_error(name.name() + " already defined otherwise from " + anonymous.getAll(concepts::is).begin()->name());
			}
		}
	}
	anonymous.unlink(concepts::is, concepts::anonymous);
	auto nam = anonymous.get(concepts::name);
	anonymous.unlink(concepts::name, nam);
	if (!nonempty) {
		for (auto & l : anonymous.links()) {
			if (l.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; }
			name.link(l.first, l.second);
		}
	}
	anonymous.link(concepts::name, nam);
	dealloc(anonymous, level0::concepts::allocations());
	//dealloc(nam, level0::concepts::allocations());
	return name;
}

}
}