diff options
author | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-12-09 06:51:41 -0800 |
---|---|---|
committer | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-12-09 06:51:41 -0800 |
commit | e6f2b95fb543b535b1914bd4954e240dbd724275 (patch) | |
tree | dcd081c1098489bc5c2172e5e535abb56094e763 /starts/meaning-vm/level-2/sugar.hpp | |
parent | 8ceeb5f83f22ed3db06fc02bb23710ccc1dbbb90 (diff) | |
download | standingwithresilience-e6f2b95fb543b535b1914bd4954e240dbd724275.tar.gz standingwithresilience-e6f2b95fb543b535b1914bd4954e240dbd724275.zip |
positional argument sugar for habits
Diffstat (limited to 'starts/meaning-vm/level-2/sugar.hpp')
-rw-r--r-- | starts/meaning-vm/level-2/sugar.hpp | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/starts/meaning-vm/level-2/sugar.hpp b/starts/meaning-vm/level-2/sugar.hpp index 6734eae..3b0e2c0 100644 --- a/starts/meaning-vm/level-2/sugar.hpp +++ b/starts/meaning-vm/level-2/sugar.hpp @@ -4,45 +4,46 @@ #include <time.h> // int time(0); int clock_gettime(CLOCK_REALTIME, struct timespec *tp{tv_sec,tv_nsec}) #include <unistd.h> // usleep(unsigned int usecs) -#define habitdelay \ - static int thisdelay = (double(rand()) / RAND_MAX * 400000 + 200000); \ - usleep(thisdelay) - #undef self -#define ahabit(name, ...) \ - a(habit, name); \ - (name).fun((std::function<void()>) \ - [=]() -> ref\ + +// habits have a structure such that they contain information about their positional +// arguments. they are made with a macro that turns the args into local variables. +// the function to call them takes any number of arguments, and these are placed in the +// thread context according to the information in the habit. + +// idea: preprocessor for level3 runs with habits +// runs after C preprocessor and responds to output produced by macros e.g. +// SET SYMBOL: <any string> +// UNSET SYMBOL: <any string> +// between the two <any string> is converted to valid c symbol when not double quoted. +// removes much of the need for individual word declarations, +// and starts to pave way towards user/intellect participation +// here, could remove the 'tok' for local refnames. + +#define ahabit(name, argnametoklist, ...) \ + intellect::level2::makehabit( \ + name, \ + {_macro_call(_macro_for_each_parens, _macro_habit_argnameref _macro_comma_remove_parens(argnametoklist))}, \ + (std::function<void()>) \ + [=]() \ { \ - habitdelay; \ + { \ + static int delay = (double(rand()) / RAND_MAX * 400000 + 200000); \ + usleep(delay); \ + } \ ref self = name; (void)self; \ ref ctx = intellect::level2::ref::context(); (void) ctx;\ + _macro_call(_macro_for_each_parens, _macro_habit_set_posarg _macro_comma_remove_parens(argnametoklist)); \ __VA_ARGS__ \ - return intellect::level1::concepts::nothing; \ }); - -// thinking on handling arguments with less boilerplate, more positional sugar -// transfer argslist to a context for calling? -// possibly transfer argslist to a function signature for the call -// transfer context to the argslist for handling call -// for each item of argslist, make a local variable, and set it to its value -/* -#define _positionalhabitarg(local, arg) \ - ref local = ctx.get(arg); -#define _ph - -#define apositionalhabit(name, argslist, ...) \ - a(habit, name); \ - (name).fun((std::function<ref(ref)>) \ - [=](ref ctx) \ - { \ - \ - }); -*/ - -// seed random number generator statically, for habitdelay -namespace __internal { -static struct timespec __tp; -static int __timeres = clock_gettime(CLOCK_REALTIME, &__tp); -static int __seed = (srand(__tp.tv_nsec), __tp.tv_nsec); -} + #define _macro_habit_argnameref(name, tok) \ + ref(#name) + #define _macro_habit_set_posarg(name, tok) \ + ref tok = ctx[ref(#name)]; + + // seed random number generator statically, for habit delay + namespace _macro_habit { + static struct timespec tp; + static int timeres = clock_gettime(CLOCK_REALTIME, &tp); + static int seed = (srand(tp.tv_nsec), tp.tv_nsec); + } |