From 55f586437b52bda32793a376f8ea5df9fb1b89e2 Mon Sep 17 00:00:00 2001 From: olpc user Date: Thu, 21 Nov 2019 17:31:16 -0500 Subject: helpers --- starts/meaning-vm/concept.hpp | 10 ++++++---- starts/meaning-vm/helpers.cpp | 10 +++++++--- starts/meaning-vm/helpers.hpp | 3 +++ starts/meaning-vm/main.cpp | 7 +++++-- starts/meaning-vm/makefile | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) (limited to 'starts') 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 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 & 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> links; + std::multimap links; using array = std::vector; 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,concept,std::hash> conceptsByName; -ref::ref(std::string s) +ref::ref(std::string const & s) { try { ptr = &conceptsByName.at(value(s)); @@ -18,11 +18,15 @@ ref::ref(std::string s) ref con = &insertion.first->second; ref nam = const_cast((concept const *)&insertion.first->first); namesByConcept.link(con, nam); - ptr = con; + ptr = con.ptr; } } -ref::operator std::string() +value & ref::name() const { return value::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 -- cgit v1.2.3