From f752a0e252e217660467ebcc412ddc62495a260c Mon Sep 17 00:00:00 2001 From: user Date: Sun, 15 Dec 2019 08:16:43 -0800 Subject: phone work --- starts/meaning-vm/.gitignore | 1 + starts/meaning-vm/habit-starts/learning-parts.cpp | 15 +++++++++++++-- starts/meaning-vm/level-2/baseref.hpp | 6 +++++- starts/meaning-vm/level-2/funcs.cpp | 22 ++++++++++++++++++++++ starts/meaning-vm/level-2/funcs.hpp | 1 + starts/meaning-vm/level-2/sugar.hpp | 2 +- 6 files changed, 43 insertions(+), 4 deletions(-) diff --git a/starts/meaning-vm/.gitignore b/starts/meaning-vm/.gitignore index 285fc08..ff3f1d3 100644 --- a/starts/meaning-vm/.gitignore +++ b/starts/meaning-vm/.gitignore @@ -1,3 +1,4 @@ +.* *.ii *.o *.a diff --git a/starts/meaning-vm/habit-starts/learning-parts.cpp b/starts/meaning-vm/habit-starts/learning-parts.cpp index 2ff53b3..db65ffe 100644 --- a/starts/meaning-vm/habit-starts/learning-parts.cpp +++ b/starts/meaning-vm/habit-starts/learning-parts.cpp @@ -105,18 +105,29 @@ static int __init = ([]()->int{ // list-item has item // item has action and context ref i = li.get(item); + // i think below we are proposing that handlers + // take one context, which is the one prepared + // in the list, then we inject our context + // into that, inside a "happened" property. + i.get(action)(hapctx, i.get(action-context)); }); ahabit(whenever-habit, ((happens, ev), (action, act), (action-context, actctx)), { + if ((action-context).linked(happened)) { + throw std::logic_error("happened on action-context"); + } if (!ev.linked(whenever-list)) { ev.set(whenever-list, (make-list)(nothing)); } + ref list = ev.get(whenever-list); // happens gets the list ref item = a(whenever-action); - item.link( - (add-to-list)( + item.set(action, act); + item.set(action-context, actctx); + + (add-to-list)(item, list); // store ctx[action] on ctx[happens] as behavior to do // store ctx[action-context] as context for behavior // PROPOSE: automatically place [happened] inside [action-context] as a stub diff --git a/starts/meaning-vm/level-2/baseref.hpp b/starts/meaning-vm/level-2/baseref.hpp index c66d1a7..8fbcc91 100644 --- a/starts/meaning-vm/level-2/baseref.hpp +++ b/starts/meaning-vm/level-2/baseref.hpp @@ -20,7 +20,11 @@ struct baseref : public level1::baseref static ref & context() { return level2::context(); } template - ref operator()(Refs... args) { return level2::dohabit(self, {args.ptr()...}); } + ref operator()(ref first, Refs... rest) { return level2::dohabit(self, {first, rest...}); } + template + ref operator()(std::initializer_list first, Pairs... rest) { return level2::dohabit(self, {first, rest...}); } + + ref operator()() { return level2::dohabit(self); } }; } diff --git a/starts/meaning-vm/level-2/funcs.cpp b/starts/meaning-vm/level-2/funcs.cpp index 11b3184..e4fc090 100644 --- a/starts/meaning-vm/level-2/funcs.cpp +++ b/starts/meaning-vm/level-2/funcs.cpp @@ -60,5 +60,27 @@ ref dohabit(ref habit, std::initializer_list args) return nothing; } +ref dohabit(ref habit, std::initializer_list> pairs) +{ + using namespace concepts; + // TODO: subcontexts or call instances + ref ctx = ref::context(); + for (auto pair : pairs) { + auto second = pair.begin(); ++ second; + ctx.link(pair.begin(), second); + } + habit.fun()(ctx); + for (auto pair : pairs) { + auto second = pair.begin(); ++ second; + ctx.unlink(pair.begin(), second); + } + if (ctx.linked(result)) { + ref ret = ctx.get(result); + ctx.unlink(result, ret); + return ret; + } + return nothing; +} + } } diff --git a/starts/meaning-vm/level-2/funcs.hpp b/starts/meaning-vm/level-2/funcs.hpp index 2a92bfd..85e1af8 100644 --- a/starts/meaning-vm/level-2/funcs.hpp +++ b/starts/meaning-vm/level-2/funcs.hpp @@ -11,6 +11,7 @@ namespace level2 { ref & context(); ref makehabit(ref name, std::initializer_list argnames, std::function code); ref dohabit(ref habit, std::initializer_list args); +ref dohabit(ref habit, std::initializer_list> args = {}); } } diff --git a/starts/meaning-vm/level-2/sugar.hpp b/starts/meaning-vm/level-2/sugar.hpp index 5b3b150..b44b282 100644 --- a/starts/meaning-vm/level-2/sugar.hpp +++ b/starts/meaning-vm/level-2/sugar.hpp @@ -35,7 +35,7 @@ ref result("nothing"); (void)result; \ _macro_call(_macro_for_each_parens, _macro_habit_set_posarg, _macro_habit_set_posarg _macro_comma_remove_parens(argnametoklist)); \ __VA_ARGS__ \ - if (result != ref("nothing")) { ctx.link(ref("result"), result); } + if (result != ref("nothing")) { ctx.link(ref("result"), result); } \ }); #define _macro_habit_argnameref(name, tok) \ ref(#name) -- cgit v1.2.3