From 018ced49ce429b123cbf63534d75f16ca91a96c2 Mon Sep 17 00:00:00 2001 From: olpc user Date: Fri, 22 Nov 2019 06:28:03 -0800 Subject: decl references in one expression --- starts/meaning-vm/concept.hpp | 5 ++++- starts/meaning-vm/helpers.hpp | 22 ++++++++++++++++++++-- starts/meaning-vm/main.cpp | 5 ++--- 3 files changed, 26 insertions(+), 6 deletions(-) (limited to 'starts') diff --git a/starts/meaning-vm/concept.hpp b/starts/meaning-vm/concept.hpp index a2dcc2c..a80b157 100644 --- a/starts/meaning-vm/concept.hpp +++ b/starts/meaning-vm/concept.hpp @@ -12,11 +12,14 @@ struct ref { ref(concept *p) : ptr(p) { } concept* operator->() { return ptr; } + + // for use by containers bool operator<(ref const & other) const { return ptr < other.ptr; } - // helper names + // for helpers ref(std::string const &); ref(char const * str) : ref(std::string(str)) { } + ref() : ref("nothing") { } value & name() const; operator const char *() const; diff --git a/starts/meaning-vm/helpers.hpp b/starts/meaning-vm/helpers.hpp index d69519c..f1b9e36 100644 --- a/starts/meaning-vm/helpers.hpp +++ b/starts/meaning-vm/helpers.hpp @@ -2,5 +2,23 @@ #include "concept.hpp" -#define r(concept) \ - ref concept = #concept +#include + +template +void __helper_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]->ptr = ref(name).ptr; + } +} + +#define decl(...) \ + ref __VA_ARGS__; \ + __helper_init_ref_names(#__VA_ARGS__, __VA_ARGS__) diff --git a/starts/meaning-vm/main.cpp b/starts/meaning-vm/main.cpp index a82a155..1392957 100644 --- a/starts/meaning-vm/main.cpp +++ b/starts/meaning-vm/main.cpp @@ -7,9 +7,8 @@ using namespace std; int main() { - r(fruit); - r(banana); - r(is); + decl(fruit, banana, is); + banana->link(is, fruit); cout << banana << "-" << is << "-" << fruit << endl; } -- cgit v1.2.3