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/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 ---------------- 10 files changed, 155 insertions(+), 129 deletions(-) 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 (limited to 'starts/meaning-vm/level-1') 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); } -}; - - -} -} -- cgit v1.2.3