From df58c6cd5c26120a985cca74243fc7978457ff2e Mon Sep 17 00:00:00 2001 From: olpc user Date: Thu, 12 Dec 2019 07:38:19 -0800 Subject: tried to use allocations with level1 ... messy, works, for some reason name of function is output in dump, shows internal problem --- starts/meaning-vm/level-1/baseref.hpp | 7 +-- starts/meaning-vm/level-1/concepts.cpp | 26 ++++++-- starts/meaning-vm/level-1/concepts.hpp | 4 +- starts/meaning-vm/level-1/funcs.cpp | 106 ++++++++++++++++++++++----------- starts/meaning-vm/level-1/funcs.hpp | 5 +- starts/meaning-vm/level-1/sugar.cpp | 6 +- starts/meaning-vm/level1.cpp | 6 +- 7 files changed, 109 insertions(+), 51 deletions(-) (limited to 'starts') diff --git a/starts/meaning-vm/level-1/baseref.hpp b/starts/meaning-vm/level-1/baseref.hpp index 5055d44..59550dd 100644 --- a/starts/meaning-vm/level-1/baseref.hpp +++ b/starts/meaning-vm/level-1/baseref.hpp @@ -15,16 +15,15 @@ struct baseref : public level0::baseref { baseref(concept * p) : level0::baseref(p) { } baseref(level0::ref const & other) : baseref(other.ptr()) { } - baseref(std::string const & name) : baseref(getnamed(name)) { } - baseref(const char *name) : baseref(std::string(name)) { } + baseref(std::string const & name, concept* allocator = nullptr) : baseref(getnamed(name, allocator)) { } + baseref(const char *name, concept* allocator = nullptr) : baseref(std::string(name), allocator) { } baseref(bool b) : baseref(b ? "true" : "false") { } baseref() : baseref("nothing") { } bool isa(ref group) const { return level1::isa(self, group); } bool isan(ref group) const { return isa(group); } - std::string const & name() const { return getname(self); } - operator std::string const &() const { return getname(self); } + std::string name() const { return getname(self); } explicit operator char const *() const { return getname(self)->data.c_str(); } ref operator-(ref other) const { return hyphenate(self, other); } diff --git a/starts/meaning-vm/level-1/concepts.cpp b/starts/meaning-vm/level-1/concepts.cpp index 890d8b2..be643a9 100644 --- a/starts/meaning-vm/level-1/concepts.cpp +++ b/starts/meaning-vm/level-1/concepts.cpp @@ -1,15 +1,29 @@ #include "concepts.hpp" +#include "funcs.hpp" +#include "../level-0/memorystore.hpp" + namespace intellect { namespace level1 { namespace concepts { -ref nothing("nothing"); -ref is("is"); -ref anonymous("anonymous"); -ref link("link"); -ref name("name"); -ref allocation("allocation"), part("part"), group("group"); +static struct init { init() { + givename(intellect::level0::concepts::allocator(), "allocator"); + givename(intellect::level0::concepts::allocates(), "allocates"); + givename(intellect::level0::concepts::allocations(), "allocations"); + givename(intellect::level0::concepts::level0allocations(), "level0-allocations"); +} } __init; + +ref level1("level1"), allocations("allocations"); +ref is("is", level1-allocations); +ref name("name", level1-allocations); +ref nothing("nothing", level1-allocations); +ref anonymous("anonymous", level1-allocations); +ref link("link", level1-allocations); +ref level0("level0", level1-allocations) , level2("level2", level1-allocations) , level3("level3", level1-allocations) , level4("level4", level1-allocations) , level5("level5", level1-allocations) , level6("level6", level1-allocations) , level7("level7", level1-allocations) , level8("level8", level1-allocations) , level9("level9", level1-allocations); +ref allocator("allocator"), allocates("allocates"); + +ref _false("false", level1-allocations), _true("true", level1-allocations); } } diff --git a/starts/meaning-vm/level-1/concepts.hpp b/starts/meaning-vm/level-1/concepts.hpp index b75133e..cd92f26 100644 --- a/starts/meaning-vm/level-1/concepts.hpp +++ b/starts/meaning-vm/level-1/concepts.hpp @@ -12,7 +12,9 @@ extern ref name; // used as the link to value naming each concept extern ref is; // a link to define group relationships, links to the more general class extern ref anonymous; // a group given concepts with generated names extern ref link; // TODO: for concepts that are links, link them with is=link -extern ref allocation, part, group; // links structuring allocation groups +extern ref level0, level1, level2, level3, level4, level5, level6, level7, level8, level9, level10; +extern ref allocator, allocates, allocations; + //extern ref true, false; <-- casting provides as if these were declared } diff --git a/starts/meaning-vm/level-1/funcs.cpp b/starts/meaning-vm/level-1/funcs.cpp index cfb44b7..1e6bd9d 100644 --- a/starts/meaning-vm/level-1/funcs.cpp +++ b/starts/meaning-vm/level-1/funcs.cpp @@ -17,13 +17,26 @@ static auto & namestruct() static struct name_t { std::unordered_map,std::equal_to> conceptsByName; - ref nameref; + ref level1allocationsref, nameref, isref; + ref level1ref; name_t() - : nameref(level0::alloc(level0::concepts::allocations())) + : level1allocationsref(level0::basic_alloc()), + nameref(level0::alloc(level1allocationsref)), + isref(level0::alloc(level1allocationsref)), + level1ref(level0::alloc(level1allocationsref)) { - level0::ref namestr = level0::alloc(nameref.ptr(), (std::string)("name")); - nameref.set(nameref, namestr); - conceptsByName.emplace(namestr.val(), nameref); + 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(), con); + con->set(nameref, namestr); } } namestruct; return namestruct; @@ -32,30 +45,64 @@ static auto & namestruct() void givename(concept* con, std::string const & name) { auto & ns = namestruct(); - level0::ref namestr = level0::alloc(con, name); - ns.conceptsByName.emplace(namestr.val(), con); - con->set(ns.nameref, namestr); + ns.give(con, name); } -concept* getnamed(std::string const & 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 { - level1::ref con = level0::alloc(level0::concepts::allocations()); + if (allocator == nullptr) { allocator = level0::concepts::allocations(); } + level1::ref con = level0::alloc(allocator); givename(con, name); return con.ptr(); } } -std::string& getname(concept* r) +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(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) + t(function) + t(function) + t(function) + t(function) + t(function) + t(function) + t(function) + t(function) + t(function) + t(function) +#undef t + else { ss << "?"; } + return ss.str(); +} + +std::string getname(concept* r) { try { return r->vget(namestruct().nameref); } catch(level0::no_such_link_type&) { - return getname(ref("UNNAMED")); + if (r->data.has_value()) { return nameforany(r); } + return "UNNAMED"; } } @@ -72,20 +119,9 @@ bool isa(concept* member, concept* group) concept* alloc(concept* allocator, std::any val) { ref ret = level0::alloc(allocator, val); - std::stringstream ss; - ss << val.type().name() << "("; - if (false); -#define t(T) \ - else if (val.type() == typeid(T)) { \ - ss << ret.val(); \ - } - 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(ret, ss.str())); + ref namestr = level0::alloc(ret, nameforany(ret)); + namestr.link(concepts::is, concepts::name); + ret.link(concepts::name, level0::alloc(ret, nameforany(ret))); return ret; } @@ -96,27 +132,29 @@ concept* hyphenate(concept* a, concept* b) std::string dump(concept* what, concept* skipmarkertype, concept* skipmarkertarget) { + std::stringstream ss; if (what->linked(skipmarkertype, skipmarkertarget)) { return {}; } - std::string ret; for (auto & link : ref(what).links()) { if (link.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; } - if (link.first == concepts::name) { continue; } - if (ret.size() == 0) { - ret = ref(what).name() + ":\n"; + if (link.second.isa(concepts::name)) { continue; } + if (ss.str().size() == 0) { + ss << ref(what).name() << " " << (void*) what << ":\n"; } - ret += " " + link.first.name() + ": " + link.second.name() + "\n"; + ss << " " << link.first.name() << ": " << link.second.name() << "\n"; } what->link(skipmarkertype, skipmarkertarget); for (auto & link : ref(what).links()) { - if (link.first.linked(level0::concepts::allocator(), level0::concepts::level0allocations())) { continue; } + 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; } - ret += dump(link.second, skipmarkertype, skipmarkertarget); + if (link.second.isa(concepts::name)) { continue; } + ss << dump(link.second, skipmarkertype, skipmarkertarget); } - return ret; + return ss.str(); } } diff --git a/starts/meaning-vm/level-1/funcs.hpp b/starts/meaning-vm/level-1/funcs.hpp index 9361917..d70a868 100644 --- a/starts/meaning-vm/level-1/funcs.hpp +++ b/starts/meaning-vm/level-1/funcs.hpp @@ -11,8 +11,9 @@ namespace intellect { namespace level1 { -concept* getnamed(std::string const & name); -std::string& getname(concept* r); +concept* getnamed(std::string const & name, concept* allocator = nullptr); +std::string getname(concept* r); +void givename(concept* con, std::string const & name); bool isa(concept* member, concept* group); diff --git a/starts/meaning-vm/level-1/sugar.cpp b/starts/meaning-vm/level-1/sugar.cpp index 21f4001..accad6c 100644 --- a/starts/meaning-vm/level-1/sugar.cpp +++ b/starts/meaning-vm/level-1/sugar.cpp @@ -50,16 +50,16 @@ ref movetoname(ref anonymous, ref name) 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; } + if (l.second.isa(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 (link.second.isa(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()); + throw std::logic_error(name.name() + " already defined otherwise from " + anonymous.getAll(concepts::is).begin()->name());// + ": \n" + dump(name, ref("dump"), ref(true)) + dump(anonymous, ref("dump"), ref(true))); } } } diff --git a/starts/meaning-vm/level1.cpp b/starts/meaning-vm/level1.cpp index 50c40a3..628fc1e 100644 --- a/starts/meaning-vm/level1.cpp +++ b/starts/meaning-vm/level1.cpp @@ -9,6 +9,8 @@ int main() { std::cout << intellect::level0::allocated() << " allocated" << std::endl; + ///* + decls(make, linked, habit); std::cout << intellect::level0::allocated() << " allocated" << std::endl; decls(needs, assumes, makes); @@ -68,10 +70,12 @@ int main() while (true) { try { intellect::level0::dealloc(intellect::level0::concepts::allocations(), intellect::level0::concepts::level0allocations()); + break; } catch (intellect::level0::still_referenced_by &e) { if (e.topic->linked(intellect::level0::concepts::allocates(), e.referrer)) { - intellect::level0::dealloc(e.referrer, e.topic); + intellect::level0::realloc(e.referrer, intellect::level0::concepts::allocations()); } else { + std::cout << "In deallocation, " << ref(e.topic).name() << " still referenced by " << ref(e.referrer).name() << std::endl; throw; } } -- cgit v1.2.3