diff options
-rw-r--r-- | starts/meaning-vm/concept.hpp | 10 | ||||
-rw-r--r-- | starts/meaning-vm/helpers.cpp | 10 | ||||
-rw-r--r-- | starts/meaning-vm/helpers.hpp | 3 | ||||
-rw-r--r-- | starts/meaning-vm/main.cpp | 7 | ||||
-rw-r--r-- | starts/meaning-vm/makefile | 2 |
5 files changed, 22 insertions, 10 deletions
diff --git a/starts/meaning-vm/concept.hpp b/starts/meaning-vm/concept.hpp index ce76f5d..a2dcc2c 100644 --- a/starts/meaning-vm/concept.hpp +++ b/starts/meaning-vm/concept.hpp @@ -11,12 +11,14 @@ template <typename T> struct value; struct ref { ref(concept *p) : ptr(p) { } - operator concept*() const { return ptr; } + concept* operator->() { return ptr; } + bool operator<(ref const & other) const { return ptr < other.ptr; } // helper names - ref(std::string); - operator std::string(); + ref(std::string const &); ref(char const * str) : ref(std::string(str)) { } + value<std::string> & name() const; + operator const char *() const; concept * ptr; }; @@ -24,7 +26,7 @@ struct ref struct concept { // a concept is made of concept-typed links to other concepts - std::multimap<ref,ref,std::less<concept*>> links; + std::multimap<ref,ref> links; using array = std::vector<ref>; ref id(); diff --git a/starts/meaning-vm/helpers.cpp b/starts/meaning-vm/helpers.cpp index e4a7689..fb17f6c 100644 --- a/starts/meaning-vm/helpers.cpp +++ b/starts/meaning-vm/helpers.cpp @@ -9,7 +9,7 @@ concept namesByConcept; std::unordered_map<value<std::string>,concept,std::hash<std::string>> conceptsByName; -ref::ref(std::string s) +ref::ref(std::string const & s) { try { ptr = &conceptsByName.at(value<std::string>(s)); @@ -18,11 +18,15 @@ ref::ref(std::string s) ref con = &insertion.first->second; ref nam = const_cast<concept *>((concept const *)&insertion.first->first); namesByConcept.link(con, nam); - ptr = con; + ptr = con.ptr; } } -ref::operator std::string() +value<std::string> & ref::name() const { return value<std::string>::of(namesByConcept.get(*this)); } + +ref::operator const char *() const { + return name().c_str(); +} diff --git a/starts/meaning-vm/helpers.hpp b/starts/meaning-vm/helpers.hpp index 6659019..d69519c 100644 --- a/starts/meaning-vm/helpers.hpp +++ b/starts/meaning-vm/helpers.hpp @@ -1,3 +1,6 @@ #pragma once #include "concept.hpp" + +#define r(concept) \ + ref concept = #concept diff --git a/starts/meaning-vm/main.cpp b/starts/meaning-vm/main.cpp index 7381da2..a82a155 100644 --- a/starts/meaning-vm/main.cpp +++ b/starts/meaning-vm/main.cpp @@ -7,6 +7,9 @@ using namespace std; int main() { - ref concept = "concept"; - cout << string(concept) << endl; + r(fruit); + r(banana); + r(is); + banana->link(is, fruit); + cout << banana << "-" << is << "-" << fruit << endl; } diff --git a/starts/meaning-vm/makefile b/starts/meaning-vm/makefile index 9796f88..ebbb00e 100644 --- a/starts/meaning-vm/makefile +++ b/starts/meaning-vm/makefile @@ -2,6 +2,6 @@ CXXFLAGS=-std=c++14 -ggdb LINK.o=$(LINK.cc) main: main.o concept.o helpers.o -%.o: *.hpp +*.o: *.hpp clean: -rm *.o main |