diff options
author | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-25 08:42:45 -0800 |
---|---|---|
committer | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-25 08:42:45 -0800 |
commit | 684ac69af192670bb6547ec01df19a3159e7d8e6 (patch) | |
tree | e6c6762e9fc40cf0ad557e160e9ab6ba7bb99eea /starts | |
parent | 520119a6f6ed418b8ae45bfde8239bbb532562cd (diff) | |
download | standingwithresilience-684ac69af192670bb6547ec01df19a3159e7d8e6.tar.gz standingwithresilience-684ac69af192670bb6547ec01df19a3159e7d8e6.zip |
separate functionality out a bit
Diffstat (limited to 'starts')
22 files changed, 216 insertions, 192 deletions
diff --git a/starts/meaning-vm/DESIGN.txt b/starts/meaning-vm/DESIGN.txt index 340f0d2..036e6e4 100644 --- a/starts/meaning-vm/DESIGN.txt +++ b/starts/meaning-vm/DESIGN.txt @@ -10,7 +10,14 @@ Concepts may not be deallocated unless nothing links to them. A special kind of concept is the value, which holds arbitrary data alongside itself. # LEVEL 1 -Level 1 provides for every concept having a name. There is additionally syntax sugar -for instantiating and using named or hyphen-separated references. -Level-1 names are for development ease, and are considered unique. Hence this also -begins definition of simple shared meaning associated with a name. +Level 1 provides for every concept having a name using the "name" link. +There is additionally syntax sugar for instantiating and using named or hyphen-separated +references. Level-1 names are for development ease, and are considered unique. +When multiple references are made with the same name, they always refer to the same concept. +Level-1 introduce the concept of groups, using the "is" link, and uses it to categorize +concepts that have an autogenerated name in the "anonymous" group. + +# LEVEL 2 +Level 2 will introdue syntax sugar for quickly instantiating conceptual relationships +using operators. This is handled by creating syntax expression concepts. + diff --git a/starts/meaning-vm/level-0/baseref.hpp b/starts/meaning-vm/level-0/baseref.hpp index c888021..bdfd065 100644 --- a/starts/meaning-vm/level-0/baseref.hpp +++ b/starts/meaning-vm/level-0/baseref.hpp @@ -9,8 +9,9 @@ namespace intellect { namespace level0 { -template <typename ref, template<typename> typename vref, typename concept> -class baseref { +template <typename ref> +class baseref +{ struct array; struct links_t; public: baseref(concept *p) @@ -35,15 +36,22 @@ public: links_t links() const; template <typename T> - vref<T> vget(ref const & type) const { return p->template vget<T>(type.p); } + T& vget(ref const & type) const { return p->vget<T>(type.p)->data; } template <typename T> - vref<T> val() { return p->template val<T>(); } + T& val() { return p->val<T>()->data; } operator concept*() const { return p; } concept*& ptr() { return p; } concept* const & ptr() const { return p; } + level0::ref & r0() { return *reinterpret_cast<level0::ref*>(this); } + level1::ref & r1() { return *reinterpret_cast<level1::ref*>(this); } + level2::ref & r2() { return *reinterpret_cast<level2::ref*>(this); } + level3::ref & r3() { return *reinterpret_cast<level3::ref*>(this); } + level4::ref & r4() { return *reinterpret_cast<level4::ref*>(this); } + level5::ref & r5() { return *reinterpret_cast<level5::ref*>(this); } + bool operator==(ref const & other) const { return self.p == other.p; } bool operator!=(ref const & other) const { return self.p == other.p; } bool operator<(ref const & other) const { return self.p < other.p; } @@ -90,13 +98,13 @@ private: }; }; -template <typename ref, template<typename> typename vref, typename concept> -typename baseref<ref,vref,concept>::array baseref<ref,vref,concept>::getAll(ref const & type) const +template <typename ref> +typename baseref<ref>::array baseref<ref>::getAll(ref const & type) const { return {p->getAll(type.p)}; } -template <typename ref, template<typename> typename vref, typename concept> -typename baseref<ref,vref,concept>::links_t baseref<ref,vref,concept>::links() const +template <typename ref> +typename baseref<ref>::links_t baseref<ref>::links() const { return {p->links}; } diff --git a/starts/meaning-vm/level-0/common.hpp b/starts/meaning-vm/level-0/common.hpp index 4c36a04..74031cc 100644 --- a/starts/meaning-vm/level-0/common.hpp +++ b/starts/meaning-vm/level-0/common.hpp @@ -11,4 +11,9 @@ template <typename T> struct value; template <typename T> struct vref; } +namespace level1 { struct ref; template <typename> struct vref; } +namespace level2 { struct ref; template <typename> struct vref; } +namespace level3 { struct ref; template <typename> struct vref; } +namespace level4 { struct ref; template <typename> struct vref; } +namespace level5 { struct ref; template <typename> struct vref; } } diff --git a/starts/meaning-vm/level-0/level-0.hpp b/starts/meaning-vm/level-0/level-0.hpp index b44b6e0..7062be0 100644 --- a/starts/meaning-vm/level-0/level-0.hpp +++ b/starts/meaning-vm/level-0/level-0.hpp @@ -6,4 +6,3 @@ #include "memorystore.hpp" #include "ref.hpp" #include "value.hpp" -#include "vref.hpp" diff --git a/starts/meaning-vm/level-0/memorystore.cpp b/starts/meaning-vm/level-0/memorystore.cpp index 7018418..d3e4f95 100644 --- a/starts/meaning-vm/level-0/memorystore.cpp +++ b/starts/meaning-vm/level-0/memorystore.cpp @@ -9,11 +9,11 @@ namespace level0 { static auto & concepts() { - static std::unordered_set<ref, std::hash<concept*>> concepts; + static std::unordered_set<concept*, std::hash<concept*>> concepts; return concepts; } -ref alloc(concept * moved) { +concept* alloc(concept * moved) { ref r = moved ? moved : new concept(); concepts().insert(r); return r; @@ -36,7 +36,7 @@ static concept* referenced(ref r) { return 0; } -void dealloc(ref r) { +void dealloc(concept * r) { concept * referenced = intellect::level0::referenced(r); if (referenced) { throw still_referenced_by(r, referenced); @@ -46,7 +46,7 @@ void dealloc(ref r) { it != concepts().end(); ++ it) { - if (ref(*it) == r) { + if (*it == r) { concepts().erase(it); delete (concept*)r; return; diff --git a/starts/meaning-vm/level-0/memorystore.hpp b/starts/meaning-vm/level-0/memorystore.hpp index a86ccea..cdd7462 100644 --- a/starts/meaning-vm/level-0/memorystore.hpp +++ b/starts/meaning-vm/level-0/memorystore.hpp @@ -2,12 +2,15 @@ #include "common.hpp" #include "ref.hpp" +#include "value.hpp" namespace intellect { namespace level0 { -ref alloc(concept * moved = 0); -void dealloc(ref); +concept * alloc(concept * moved = 0); +template <typename T> +value<T> * valloc(T const & v) { return static_cast<value<T>*>(alloc(new value<T>(v))); } +void dealloc(concept*); std::size_t allocated(); } diff --git a/starts/meaning-vm/level-0/ref.hpp b/starts/meaning-vm/level-0/ref.hpp index 87a37c6..ff55355 100644 --- a/starts/meaning-vm/level-0/ref.hpp +++ b/starts/meaning-vm/level-0/ref.hpp @@ -8,13 +8,9 @@ namespace intellect { namespace level0 { -struct ref : public baseref<ref, vref, concept> +struct ref : public baseref<ref> { ref(concept *p) : baseref(p) { } - ref & operator=(ref const & other) { self.p = other.p; return self; } - - ref & l0() { return self; } - ref const & l0() const { return self; } std::string dump(ref skipmarkertype, ref skipmarkertarget); }; diff --git a/starts/meaning-vm/level-0/vref.hpp b/starts/meaning-vm/level-0/vref.hpp deleted file mode 100644 index 1eb701d..0000000 --- a/starts/meaning-vm/level-0/vref.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include "common.hpp" -#include "memorystore.hpp" -#include "ref.hpp" -#include "value.hpp" - -namespace intellect { -namespace level0 { - -template <typename T> -struct vref -{ - vref(value<T> *p) : ptr(p) { } - value<T>* operator->() { return ptr; } - operator T const &() const { return *ptr; } - - vref(T const & val) : vref(alloc(new value<T>(val))) { } - - vref(ref const & other) : ptr(static_cast<value<T>*>((concept*)other)) { } - operator ref() { return ptr; } - T const & val() { return *ptr; } - - // for use by containers - bool operator<(vref<T> const & other) const { return self.ptr < other.ptr; } - -protected: - value<T> * const ptr; -}; - - -} -} diff --git a/starts/meaning-vm/level-1/baseref.hpp b/starts/meaning-vm/level-1/baseref.hpp new file mode 100644 index 0000000..2f072b6 --- /dev/null +++ b/starts/meaning-vm/level-1/baseref.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include "common.hpp" +#include "funcs.hpp" + +namespace intellect { +namespace level1 { + +template <typename ref> +struct baseref : public level0::baseref<ref> +{ + baseref(concept * p) : level0::baseref<ref>(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(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)->data; } + operator std::string const &() const { return getname(self)->data; } + operator char const *() const { return getname(self)->data.c_str(); } + + std::string dump(ref skipmarkertype, ref skipmarkertarget); +}; + +} +} diff --git a/starts/meaning-vm/level-1/common.hpp b/starts/meaning-vm/level-1/common.hpp index 2977077..2c9297b 100644 --- a/starts/meaning-vm/level-1/common.hpp +++ b/starts/meaning-vm/level-1/common.hpp @@ -1,10 +1,15 @@ #pragma once +#include "../level-0/common.hpp" + namespace intellect { namespace level1 { +using level0::concept; +using level0::value; + +template <typename T> struct baseref; struct ref; -template <typename T> struct vref; } } diff --git a/starts/meaning-vm/level-1/concepts.cpp b/starts/meaning-vm/level-1/concepts.cpp index 4c098de..5a3dfaf 100644 --- a/starts/meaning-vm/level-1/concepts.cpp +++ b/starts/meaning-vm/level-1/concepts.cpp @@ -7,6 +7,7 @@ namespace concepts { ref is("is"); ref anonymous("anonymous"); ref link("link"); +ref name("name"); } } diff --git a/starts/meaning-vm/level-1/funcs.cpp b/starts/meaning-vm/level-1/funcs.cpp new file mode 100644 index 0000000..c9cca37 --- /dev/null +++ b/starts/meaning-vm/level-1/funcs.cpp @@ -0,0 +1,81 @@ +#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.r0(), 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; +} + +template value<std::string>* valloc<std::string>(std::string const & val); +template value<int>* valloc<int>(int const & val); + +} +} diff --git a/starts/meaning-vm/level-1/funcs.hpp b/starts/meaning-vm/level-1/funcs.hpp new file mode 100644 index 0000000..55a28f2 --- /dev/null +++ b/starts/meaning-vm/level-1/funcs.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "common.hpp" + +#include "../level-0/memorystore.hpp" + +#include <string> +#include <sstream> +#include <typeinfo> + +namespace intellect { +namespace level1 { + +concept* getnamed(std::string const & name); +value<std::string>* getname(concept* r); + +bool isa(concept* member, concept* group); + +template <typename T> +value<T>* valloc(T const & val); + +} +} diff --git a/starts/meaning-vm/level-1/level-1.hpp b/starts/meaning-vm/level-1/level-1.hpp index d13ba9f..e34d86a 100644 --- a/starts/meaning-vm/level-1/level-1.hpp +++ b/starts/meaning-vm/level-1/level-1.hpp @@ -4,4 +4,3 @@ #include "concepts.hpp" #include "ref.hpp" #include "sugar.hpp" -#include "vref.hpp" diff --git a/starts/meaning-vm/level-1/ref.cpp b/starts/meaning-vm/level-1/ref.cpp index b593f81..8d74b50 100644 --- a/starts/meaning-vm/level-1/ref.cpp +++ b/starts/meaning-vm/level-1/ref.cpp @@ -1,72 +1,12 @@ #include "ref.hpp" -#include "../level-0/errors.hpp" -#include "../level-0/memorystore.hpp" -#include "../level-0/vref.hpp" + #include "concepts.hpp" -#include "vref.hpp" -#include <unordered_map> using namespace intellect; using 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<level0::vref<std::string>,level0::ref,std::hash<std::string>,std::equal_to<std::string>> conceptsByName; - ref nameref; - name_t() - : nameref(level0::alloc()) - { - level0::vref namestr(std::string("name")); - nameref.set(nameref, (level0::ref)namestr); - conceptsByName.emplace(namestr, nameref.l0()); - } - } namestruct; - return namestruct; -} - -level0::ref getnamed(std::string const & name) -{ - auto & ns = namestruct(); - auto res = ns.conceptsByName.find(name); - if (res != ns.conceptsByName.end()) { - return res->second; - } else { - level0::ref con = level0::alloc(); - level0::vref namestr(name); - ns.conceptsByName.emplace(namestr, con); - con.set(ns.nameref.l0(), namestr); - return con; - } -} - -ref::ref(std::string const & name) -: baseref(getnamed(name).ptr()) -{ } - -vref<std::string> ref::name() const -{ - try { - return vget<std::string>(namestruct().nameref); - } catch(level0::no_such_link_type&) { - return ref("UNNAMED").name(); - } -} - -bool ref::isa(ref group) const -{ - for (auto & g : getAll(is)) { - if (g == group) return true; - if (g == self) continue; - if (g.isa(group)) return true; - } - return false; -} - std::string ref::dump(ref skipmarkertype, ref skipmarkertarget) { if (linked(skipmarkertype, skipmarkertarget)) { @@ -74,11 +14,11 @@ std::string ref::dump(ref skipmarkertype, ref skipmarkertarget) } std::string ret; for (auto & link : links()) { - if (link.first == namestruct().nameref) { continue; } + if (link.first == concepts::name) { continue; } if (ret.size() == 0) { - ret = name().val() + ":\n"; + ret = name() + ":\n"; } - ret += " " + link.first.name().val() + ": " + link.second.name().val() + "\n"; + ret += " " + link.first.name() + ": " + link.second.name() + "\n"; } link(skipmarkertype, skipmarkertarget); for (auto & link : links()) { diff --git a/starts/meaning-vm/level-1/ref.hpp b/starts/meaning-vm/level-1/ref.hpp index 8a7394e..3f2214d 100644 --- a/starts/meaning-vm/level-1/ref.hpp +++ b/starts/meaning-vm/level-1/ref.hpp @@ -1,30 +1,14 @@ #pragma once #include "common.hpp" -#include "../level-0/baseref.hpp" -#include "../level-0/ref.hpp" +#include "baseref.hpp" namespace intellect { namespace level1 { -struct ref : public level0::baseref<ref,vref,level0::concept> +struct ref : public baseref<ref> { - using level0::baseref<ref,vref,level0::concept>::baseref; - ref(level0::concept * p) : baseref(p) { } - ref(level0::ref const & other) : baseref(other.ptr()) { } - ref(std::string const & name); - ref(const char *name) : ref(std::string(name)) { } - ref(bool b) : ref(b ? "true" : "false") { } - ref() : ref("nothing") { } - - bool isa(ref group) const; - bool isan(ref group) const { return isa(group); } - - vref<std::string> name() const; - - level0::ref & l0() { return *reinterpret_cast<level0::ref*>(this); } - ref & l1() { return self; } - ref const & l1() const { return self; } + using baseref<ref>::baseref; std::string dump(ref skipmarkertype, ref skipmarkertarget); }; diff --git a/starts/meaning-vm/level-1/sugar.hpp b/starts/meaning-vm/level-1/sugar.hpp index 9d439e5..c34f013 100644 --- a/starts/meaning-vm/level-1/sugar.hpp +++ b/starts/meaning-vm/level-1/sugar.hpp @@ -2,7 +2,6 @@ #include "common.hpp" #include "ref.hpp" -#include "vref.hpp" #include <string> #include <sstream> @@ -10,10 +9,17 @@ namespace intellect { namespace level1 { +ref a(ref group); +ref an(ref group); +ref a(ref group, ref name); +ref an(ref group, ref name); + +/* inline std::string operator+(vref<std::string> a, char const * b) { return std::string(a) + b; } inline std::string operator+(vref<std::string> a, std::string b) { return std::string(a) + b; } inline std::string operator+(char const * a, vref<std::string> b) { return a + std::string(b); } inline std::string operator+(std::string a, vref<std::string> b) { return a + std::string(b); } +*/ namespace internal { template <typename... T> @@ -38,10 +44,5 @@ namespace internal { ref operator-(ref a, ref b); -ref a(ref group); -ref an(ref group); -ref a(ref group, ref name); -ref an(ref group, ref name); - } } diff --git a/starts/meaning-vm/level-1/vref.hpp b/starts/meaning-vm/level-1/vref.hpp deleted file mode 100644 index fb7ba5d..0000000 --- a/starts/meaning-vm/level-1/vref.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "../level-0/vref.hpp" -#include "ref.hpp" - -#include <sstream> - -namespace intellect { -namespace level1 { - -template <typename T> -struct vref : public level0::vref<T> -{ - vref(level0::vref<T> other) : level0::vref<T>((level0::concept*)other.ref) { } - vref(level0::value<T> *p) : level0::vref<T>(p) { } - vref(ref const & other) : level0::vref<T>(other) { } - vref(T const & val) - : level0::vref<T>(val) - { - std::stringstream ss; - ss << "v:" << val; - ref(self).set(ref("name"), (level0::ref)level0::vref<std::string>(ss.str())); - } - - using level0::vref<T>::operator->; - using level0::vref<T>::operator T const &; - - vref<std::string> name() - { - return ref(self).name(); - } - - operator ref() { return level0::ref(level0::vref<T>::ptr); } -}; - - -} -} diff --git a/starts/meaning-vm/level-2/common.hpp b/starts/meaning-vm/level-2/common.hpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/starts/meaning-vm/level-2/common.hpp diff --git a/starts/meaning-vm/level-2/ref.hpp b/starts/meaning-vm/level-2/ref.hpp new file mode 100644 index 0000000..5b7dc4f --- /dev/null +++ b/starts/meaning-vm/level-2/ref.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "common.hpp" +#include "../level-0/baseref.hpp" + +namespace intellect { +namespace level2 { + +struct ref ; public level0::baseref<ref,level1::vref,level0::concept> +} + ref(level0::concept *p): baseref(p) { } + +} diff --git a/starts/meaning-vm/level0.cpp b/starts/meaning-vm/level0.cpp index a69da91..1efd33e 100644 --- a/starts/meaning-vm/level0.cpp +++ b/starts/meaning-vm/level0.cpp @@ -12,8 +12,8 @@ int main() ref c = alloc(); ref d = alloc(); ref e = alloc(); - vref<int> num(3); - vref<std::function<void()>> code([](){ + ref num = valloc<int>(3); + ref code = valloc<std::function<void()>>([](){ std::cout << "Hello, world." << std::endl; }); auto numlink = alloc(); @@ -31,8 +31,8 @@ int main() std::cout << "Num: " << ref(num).dump(skip, skip); std::cout << "Code: " << ref(code).dump(skip, skip); std::cout << a.dump(skip, skip); - std::cout << "Num: " << a.vget<int>(numlink).val() << std::endl; - std::cout << "Code: "; a.vget<std::function<void()>>(codelink).val()(); + std::cout << "Num: " << a.vget<int>(numlink) << std::endl; + std::cout << "Code: "; a.vget<std::function<void()>>(codelink)(); std::cout << allocated() << " allocated" << std::endl; diff --git a/starts/meaning-vm/level1.cpp b/starts/meaning-vm/level1.cpp index 2e7833c..1002c7b 100644 --- a/starts/meaning-vm/level1.cpp +++ b/starts/meaning-vm/level1.cpp @@ -3,7 +3,7 @@ #include <iostream> using namespace intellect::level1; -using namespace concepts; +using namespace intellect::level1::concepts; int main() { @@ -20,7 +20,7 @@ int main() ref ret = a(structure); ret.link(is, function-argument); - ret.set(argument-position, vref<int>(1)), + ret.set(argument-position, ref(1)), ret.set(a(variable, A), provide); ret.set(a(variable, B), provide); ret.set(a(variable, C), provide); |