From 684ac69af192670bb6547ec01df19a3159e7d8e6 Mon Sep 17 00:00:00 2001 From: olpc user Date: Mon, 25 Nov 2019 08:42:45 -0800 Subject: separate functionality out a bit --- starts/meaning-vm/DESIGN.txt | 15 ++++-- starts/meaning-vm/level-0/baseref.hpp | 24 ++++++--- starts/meaning-vm/level-0/common.hpp | 5 ++ starts/meaning-vm/level-0/level-0.hpp | 1 - starts/meaning-vm/level-0/memorystore.cpp | 8 +-- starts/meaning-vm/level-0/memorystore.hpp | 7 ++- starts/meaning-vm/level-0/ref.hpp | 6 +-- starts/meaning-vm/level-0/vref.hpp | 33 ------------- starts/meaning-vm/level-1/baseref.hpp | 30 ++++++++++++ starts/meaning-vm/level-1/common.hpp | 7 ++- starts/meaning-vm/level-1/concepts.cpp | 1 + starts/meaning-vm/level-1/funcs.cpp | 81 +++++++++++++++++++++++++++++++ starts/meaning-vm/level-1/funcs.hpp | 23 +++++++++ starts/meaning-vm/level-1/level-1.hpp | 1 - starts/meaning-vm/level-1/ref.cpp | 68 ++------------------------ starts/meaning-vm/level-1/ref.hpp | 22 ++------- starts/meaning-vm/level-1/sugar.hpp | 13 ++--- starts/meaning-vm/level-1/vref.hpp | 38 --------------- starts/meaning-vm/level-2/common.hpp | 0 starts/meaning-vm/level-2/ref.hpp | 13 +++++ starts/meaning-vm/level0.cpp | 8 +-- starts/meaning-vm/level1.cpp | 4 +- 22 files changed, 216 insertions(+), 192 deletions(-) delete mode 100644 starts/meaning-vm/level-0/vref.hpp create mode 100644 starts/meaning-vm/level-1/baseref.hpp create mode 100644 starts/meaning-vm/level-1/funcs.cpp create mode 100644 starts/meaning-vm/level-1/funcs.hpp delete mode 100644 starts/meaning-vm/level-1/vref.hpp create mode 100644 starts/meaning-vm/level-2/common.hpp create mode 100644 starts/meaning-vm/level-2/ref.hpp (limited to 'starts/meaning-vm') 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 vref, typename concept> -class baseref { +template +class baseref +{ struct array; struct links_t; public: baseref(concept *p) @@ -35,15 +36,22 @@ public: links_t links() const; template - vref vget(ref const & type) const { return p->template vget(type.p); } + T& vget(ref const & type) const { return p->vget(type.p)->data; } template - vref val() { return p->template val(); } + T& val() { return p->val()->data; } operator concept*() const { return p; } concept*& ptr() { return p; } concept* const & ptr() const { return p; } + level0::ref & r0() { return *reinterpret_cast(this); } + level1::ref & r1() { return *reinterpret_cast(this); } + level2::ref & r2() { return *reinterpret_cast(this); } + level3::ref & r3() { return *reinterpret_cast(this); } + level4::ref & r4() { return *reinterpret_cast(this); } + level5::ref & r5() { return *reinterpret_cast(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 vref, typename concept> -typename baseref::array baseref::getAll(ref const & type) const +template +typename baseref::array baseref::getAll(ref const & type) const { return {p->getAll(type.p)}; } -template typename vref, typename concept> -typename baseref::links_t baseref::links() const +template +typename baseref::links_t baseref::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 struct value; template struct vref; } +namespace level1 { struct ref; template struct vref; } +namespace level2 { struct ref; template struct vref; } +namespace level3 { struct ref; template struct vref; } +namespace level4 { struct ref; template struct vref; } +namespace level5 { struct ref; template 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> concepts; + static std::unordered_set> 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 +value * valloc(T const & v) { return static_cast*>(alloc(new value(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 +struct ref : public baseref { 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 -struct vref -{ - vref(value *p) : ptr(p) { } - value* operator->() { return ptr; } - operator T const &() const { return *ptr; } - - vref(T const & val) : vref(alloc(new value(val))) { } - - vref(ref const & other) : ptr(static_cast*>((concept*)other)) { } - operator ref() { return ptr; } - T const & val() { return *ptr; } - - // for use by containers - bool operator<(vref const & other) const { return self.ptr < other.ptr; } - -protected: - value * 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 +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(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 struct baseref; struct ref; -template 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 + +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::equal_to> conceptsByName; + ref nameref; + name_t() + : nameref(level0::alloc()) + { + auto namestr = valloc("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* namestr = level0::valloc(name); + ns.conceptsByName.emplace(namestr->data, con); + con.set(ns.nameref.r0(), namestr); + return con.ptr(); + } +} + +value* getname(concept* r) +{ + try { + return r->vget(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 +value* valloc(T const & val) +{ + auto ret = level0::valloc(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* valloc(std::string const & val); +template value* valloc(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 +#include +#include + +namespace intellect { +namespace level1 { + +concept* getnamed(std::string const & name); +value* getname(concept* r); + +bool isa(concept* member, concept* group); + +template +value* 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 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::ref,std::hash,std::equal_to> 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 ref::name() const -{ - try { - return vget(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 +struct ref : public baseref { - using level0::baseref::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 name() const; - - level0::ref & l0() { return *reinterpret_cast(this); } - ref & l1() { return self; } - ref const & l1() const { return self; } + using baseref::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 #include @@ -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 a, char const * b) { return std::string(a) + b; } inline std::string operator+(vref a, std::string b) { return std::string(a) + b; } inline std::string operator+(char const * a, vref b) { return a + std::string(b); } inline std::string operator+(std::string a, vref b) { return a + std::string(b); } +*/ namespace internal { template @@ -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 - -namespace intellect { -namespace level1 { - -template -struct vref : public level0::vref -{ - vref(level0::vref other) : level0::vref((level0::concept*)other.ref) { } - vref(level0::value *p) : level0::vref(p) { } - vref(ref const & other) : level0::vref(other) { } - vref(T const & val) - : level0::vref(val) - { - std::stringstream ss; - ss << "v:" << val; - ref(self).set(ref("name"), (level0::ref)level0::vref(ss.str())); - } - - using level0::vref::operator->; - using level0::vref::operator T const &; - - vref name() - { - return ref(self).name(); - } - - operator ref() { return level0::ref(level0::vref::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 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(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 num(3); - vref> code([](){ + ref num = valloc(3); + ref code = valloc>([](){ 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(numlink).val() << std::endl; - std::cout << "Code: "; a.vget>(codelink).val()(); + std::cout << "Num: " << a.vget(numlink) << std::endl; + std::cout << "Code: "; a.vget>(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 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(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); -- cgit v1.2.3