From 680af2ca0b1ced4c82bbce620d6c4fc97dae88d0 Mon Sep 17 00:00:00 2001 From: olpc user Date: Thu, 5 Dec 2019 04:12:23 -0800 Subject: add function syntaxes --- starts/meaning-vm/level-0/baseref.hpp | 4 +++- starts/meaning-vm/level-0/concept.hpp | 3 +++ starts/meaning-vm/level-1/baseref.hpp | 33 +++++++++++++++++++++++++++++++++ starts/meaning-vm/level-1/common.hpp | 2 ++ starts/meaning-vm/level-1/concepts.hpp | 1 - starts/meaning-vm/level1.cpp | 23 +++++++++++++++++++++++ 6 files changed, 64 insertions(+), 2 deletions(-) (limited to 'starts/meaning-vm') diff --git a/starts/meaning-vm/level-0/baseref.hpp b/starts/meaning-vm/level-0/baseref.hpp index 205b422..77c783d 100644 --- a/starts/meaning-vm/level-0/baseref.hpp +++ b/starts/meaning-vm/level-0/baseref.hpp @@ -51,10 +51,12 @@ public: template T& vget(ref const & type) const { return p->vget(type.p); } template - void vset(ref const & type, T const & v) { p->set(type.p, alloc(v)); } + void vset(ref const & type, T const & v) { p->set(type.p, level0::alloc(v)); } template T& val() { return p->val(); } + template + void val(T const & v) { p->val(v); } operator concept*() const { return p; } concept*& ptr() { return p; } diff --git a/starts/meaning-vm/level-0/concept.hpp b/starts/meaning-vm/level-0/concept.hpp index d57ccfc..d42d36b 100644 --- a/starts/meaning-vm/level-0/concept.hpp +++ b/starts/meaning-vm/level-0/concept.hpp @@ -38,6 +38,9 @@ struct concept template T & val() { return std::any_cast(data); } + + template + void val(T const & v) { data = v; } }; } diff --git a/starts/meaning-vm/level-1/baseref.hpp b/starts/meaning-vm/level-1/baseref.hpp index e8bdcb9..63045fa 100644 --- a/starts/meaning-vm/level-1/baseref.hpp +++ b/starts/meaning-vm/level-1/baseref.hpp @@ -5,6 +5,8 @@ #include "../level-0/ref.hpp" +#include + namespace intellect { namespace level1 { @@ -27,7 +29,38 @@ struct baseref : public level0::baseref ref operator-(ref other) { return hyphenate(self.ptr(), other.ptr()); } + template + void vset(ref const & type, T const & v) { self.set(type, level1::alloc(v)); } + + template + std::function & fun() { return self.template val>(); } + template + void fun(std::function const & f) { val(f); } + template + void fun(std::function const & f) { val(voidtoret(f)); } + template + void fget(ref const & type) { return self.template vget>(type); } + template + void fset(ref const & type, std::function f) { self.vset(type, f); } + template + void fset(ref const & type, std::function f) { fset(type, voidtoret(f)); } + + template + ref operator()(Ref... args) { return self.template fun()(args...); } + std::string dump(ref skipmarkertype, ref skipmarkertarget); + +private: + template + std::function voidtoret(std::function f) + { + return [f](Refs... args) -> ref + { + std::initializer_list({&args...}); + f(args...); + return concepts::nothing; + }; + } }; } diff --git a/starts/meaning-vm/level-1/common.hpp b/starts/meaning-vm/level-1/common.hpp index 73de3b4..5dd2840 100644 --- a/starts/meaning-vm/level-1/common.hpp +++ b/starts/meaning-vm/level-1/common.hpp @@ -10,5 +10,7 @@ using level0::concept; template struct baseref; struct ref; +namespace concepts { extern ref nothing; } + } } diff --git a/starts/meaning-vm/level-1/concepts.hpp b/starts/meaning-vm/level-1/concepts.hpp index 3f49eca..6dfc64b 100644 --- a/starts/meaning-vm/level-1/concepts.hpp +++ b/starts/meaning-vm/level-1/concepts.hpp @@ -1,7 +1,6 @@ #pragma once #include "ref.hpp" -#include "sugar.hpp" namespace intellect { namespace level1 { diff --git a/starts/meaning-vm/level1.cpp b/starts/meaning-vm/level1.cpp index ae70b5a..2e005e2 100644 --- a/starts/meaning-vm/level1.cpp +++ b/starts/meaning-vm/level1.cpp @@ -14,6 +14,7 @@ int main() decls(source, type, target); decls(structure, function, argument, position); decls(variable, provide); + decls(act); (make-linked).link( is, habit, @@ -35,8 +36,30 @@ int main() assumes, not-A-B-C-linked, makes, A-B-C-linked ); + (make-linked).fset( + act, + (std::function)[](ref args) + { + ref source = args.get("A"); + ref type = args.get("B"); + ref target = args.get("C"); + std::cout << "Linking " << source.name() << " by " << type.name() << " to " << target.name() << std::endl; + source.link(type, target); + } + ); std::cout << (make-linked).dump("dumped", true) << std::endl; + decls(apple, fruit); + (make-linked).get(act) + (a(function-argument) + .link( + A, apple, + B, is, + C, fruit + )); + + std::cout << apple.dump("dumped", true) << std::endl; + return 0; } -- cgit v1.2.3