diff --git a/README b/README deleted file mode 100644 index 1804942..0000000 --- a/README +++ /dev/null @@ -1,3 +0,0 @@ -Example of a contextual state machine (generator methods) implementation in basic C++, using mostly C constructs. - -See `src/test/main.cpp` for example. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecca814 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# libsm + +Contextual state machine (generator methods) implementation in basic C++, using mostly C constructs. + +(TODO: Explain what is and isn't supported) + +## Example + +Output values of any type +``` c++ +#include +#include + +#include + +SM_GENERATOR(powers_of_two) +{ + int* a = SM_VAR(1); + SM_BEGIN; + { + while( (*a) > 0 ) { + SM_YIELD_VALUE(*a); + (*a) <<= 1; + } + } + SM_END; +} + +int main() +{ + auto state = sm_new_state(); + auto gen = sm_generate(&powers_of_two); + + sm_output output; + int out_int; + while(sm_next(&gen, state, &output)) { + if(!sm_output_value(output, &out_int)) + continue; + printf("Power of two: %d\n", out_int); + } + + sm_free_output(&output); + + sm_free_generator(gen); + sm_free_state(state); + + return 0; +} +``` diff --git a/src/gen.cpp b/src/gen.cpp index 3d3ec71..b500ed2 100644 --- a/src/gen.cpp +++ b/src/gen.cpp @@ -1,6 +1,8 @@ #include #include "dev.h" +#include + struct sm_generator { sm_generator* prev; diff --git a/src/test/main.cpp b/src/test/main.cpp index 2c8ab7b..e11e26c 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,70 +1,38 @@ -#include -#include +#include +#include #include -struct _test { - int a, b; -}; - -SM_GENERATOR(sm_test_2) -{ - int* a = SM_VAR(-10); - - SM_BEGIN; - while( (*a) < 0 ) { - printf("(2) a = %d\n", *a); - SM_YIELD_VALUE(*a); - (*a)+=1; - } - printf("Done!\n"); - SM_END; -} - -SM_GENERATOR(sm_test) +SM_GENERATOR(powers_of_two) { - int* a = SM_VAR(10); - - auto c = SM_SLOT(_test); - float* d = SM_SLOT(float); - - *c = { 200, 300 }; - *d = 10.f; - - SM_BEGIN; - *a = 5; - SM_YIELD_VALUE(*a); - printf("IN a = %d\n", *a); - *a = 1; - SM_YIELD_VALUE(*a); - printf("IN a = %d\n", *a); - //printf("Starting function 2\n"); - SM_YIELD(sm_test_2); - //printf("2 done\n"); - - SM_END; + int* a = SM_VAR(1); + SM_BEGIN; + { + while( (*a) > 0 ) { + SM_YIELD_VALUE(*a); + (*a) <<= 1; + } + } + SM_END; } int main() { - auto state = sm_new_state(); - auto gen = sm_generate(&sm_test); - - sm_state_setopt(state, INTERMEDIATE_RETURNS, false); + auto state = sm_new_state(); + auto gen = sm_generate(&powers_of_two); - sm_output output; - int out_int=0; - while(sm_next(&gen, state, &output)) { - if(!sm_output_value(output, &out_int)) - continue; - printf("OUT \t\ta = %d\n", out_int); // prints the last one, is this a good or bad thing? - } + sm_output output; + int out_int; + while(sm_next(&gen, state, &output)) { + if(!sm_output_value(output, &out_int)) + continue; + printf("Power of two: %d\n", out_int); + } - printf("\n\nFinal output: %d\n", out_int); - sm_free_output(&output); + sm_free_output(&output); - sm_free_generator(gen); - sm_free_state(state); + sm_free_generator(gen); + sm_free_state(state); - return 0; + return 0; }