From cd03e291664cb102bde61d86a15f0add11809766 Mon Sep 17 00:00:00 2001 From: olpc user Date: Sun, 24 Nov 2019 19:23:37 -0800 Subject: messy but works. rushing to get this done with may have been a poor investment. --- starts/meaning-vm/level-1/common.hpp | 10 ++++ starts/meaning-vm/level-1/concepts.cpp | 13 +++++ starts/meaning-vm/level-1/concepts.hpp | 20 ++++++++ starts/meaning-vm/level-1/level-1.hpp | 7 +++ starts/meaning-vm/level-1/ref.cpp | 91 ++++++++++++++++++++++++++++++++++ starts/meaning-vm/level-1/ref.hpp | 34 +++++++++++++ starts/meaning-vm/level-1/sugar.cpp | 41 +++++++++++++++ starts/meaning-vm/level-1/sugar.hpp | 47 ++++++++++++++++++ starts/meaning-vm/level-1/vref.hpp | 38 ++++++++++++++ 9 files changed, 301 insertions(+) create mode 100644 starts/meaning-vm/level-1/common.hpp create mode 100644 starts/meaning-vm/level-1/concepts.cpp create mode 100644 starts/meaning-vm/level-1/concepts.hpp create mode 100644 starts/meaning-vm/level-1/level-1.hpp create mode 100644 starts/meaning-vm/level-1/ref.cpp create mode 100644 starts/meaning-vm/level-1/ref.hpp create mode 100644 starts/meaning-vm/level-1/sugar.cpp create mode 100644 starts/meaning-vm/level-1/sugar.hpp create mode 100644 starts/meaning-vm/level-1/vref.hpp (limited to 'starts/meaning-vm/level-1') diff --git a/starts/meaning-vm/level-1/common.hpp b/starts/meaning-vm/level-1/common.hpp new file mode 100644 index 0000000..2977077 --- /dev/null +++ b/starts/meaning-vm/level-1/common.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace intellect { +namespace level1 { + +struct ref; +template struct vref; + +} +} diff --git a/starts/meaning-vm/level-1/concepts.cpp b/starts/meaning-vm/level-1/concepts.cpp new file mode 100644 index 0000000..4c098de --- /dev/null +++ b/starts/meaning-vm/level-1/concepts.cpp @@ -0,0 +1,13 @@ +#include "concepts.hpp" + +namespace intellect { +namespace level1 { +namespace concepts { + +ref is("is"); +ref anonymous("anonymous"); +ref link("link"); + +} +} +} diff --git a/starts/meaning-vm/level-1/concepts.hpp b/starts/meaning-vm/level-1/concepts.hpp new file mode 100644 index 0000000..7aa7394 --- /dev/null +++ b/starts/meaning-vm/level-1/concepts.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include "ref.hpp" +#include "sugar.hpp" + +namespace intellect { +namespace level1 { + +namespace concepts { + +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 true, false; <-- casting provides as if these were declared + +} + +} +} diff --git a/starts/meaning-vm/level-1/level-1.hpp b/starts/meaning-vm/level-1/level-1.hpp new file mode 100644 index 0000000..d13ba9f --- /dev/null +++ b/starts/meaning-vm/level-1/level-1.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include "common.hpp" +#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 new file mode 100644 index 0000000..045e879 --- /dev/null +++ b/starts/meaning-vm/level-1/ref.cpp @@ -0,0 +1,91 @@ +#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) +: ref0((level0::concept*)getnamed(name)) +{ } + +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)) { + return {}; + } + std::string ret; + for (auto & link : links()) { + if (link.first == namestruct().nameref) { continue; } + if (ret.size() == 0) { + ret = name().val() + ":\n"; + } + ret += " " + link.first.name().val() + ": " + link.second.name().val() + "\n"; + } + link(skipmarkertype, skipmarkertarget); + for (auto & link : links()) { + if (link.first == skipmarkertype && link.second == skipmarkertarget) { + continue; + } + ret += link.second.dump(skipmarkertype, skipmarkertarget); + } + return ret; +} diff --git a/starts/meaning-vm/level-1/ref.hpp b/starts/meaning-vm/level-1/ref.hpp new file mode 100644 index 0000000..40eee20 --- /dev/null +++ b/starts/meaning-vm/level-1/ref.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "common.hpp" +#include "../level-0/ref.hpp" +#include "../level-0/ref-mixin.hpp" + +namespace intellect { +namespace level1 { + +struct ref : public level0::refmixin +{ + ref(level0::ref const & other) : ref0(other) { } + 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 ref0; } + ref & l1() { return self; } + ref const & l1() const { return self; } + + std::string dump(ref skipmarkertype, ref skipmarkertarget); +private: + + level0::ref ref0; +}; + +} +} diff --git a/starts/meaning-vm/level-1/sugar.cpp b/starts/meaning-vm/level-1/sugar.cpp new file mode 100644 index 0000000..eeb2eb0 --- /dev/null +++ b/starts/meaning-vm/level-1/sugar.cpp @@ -0,0 +1,41 @@ +#include "sugar.hpp" + +#include "concepts.hpp" + +using namespace intellect::level1; +using namespace concepts; + +namespace intellect { +namespace level1 { + +ref operator-(ref a, ref b) +{ + return ref(a.name() + "-" + b.name()); +} + +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); +} + +} +} diff --git a/starts/meaning-vm/level-1/sugar.hpp b/starts/meaning-vm/level-1/sugar.hpp new file mode 100644 index 0000000..c2fa4f9 --- /dev/null +++ b/starts/meaning-vm/level-1/sugar.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include "common.hpp" +#include "ref.hpp" +#include "vref.hpp" + +#include +#include + +namespace intellect { +namespace level1 { + +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 + void init_ref_names(std::string names, T &... refrefs) + { + std::stringstream ss(names); + ref* refptrs[] = {&refrefs...}; + for (std::size_t i = 0; i < sizeof...(refrefs); ++ i) { + std::string name; + ss >> name; + if (name[name.size() - 1] == ',') { + name = name.substr(0, name.size() - 1); + } + refptrs[i]->l0() = ref(name).l0(); + } + } +} + +#define decl(...) \ + intellect::level1::ref __VA_ARGS__; \ + intellect::level1::internal::init_ref_names(#__VA_ARGS__, __VA_ARGS__) + +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 new file mode 100644 index 0000000..fb7ba5d --- /dev/null +++ b/starts/meaning-vm/level-1/vref.hpp @@ -0,0 +1,38 @@ +#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