better README

master
Avril 4 years ago
parent 5cf02cb7ca
commit eaf20457ea
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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.

@ -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 <sm/state.h>
#include <sm/gen.h>
#include <cstdio>
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;
}
```

@ -1,6 +1,8 @@
#include <gen.h>
#include "dev.h"
#include <cstdio>
struct sm_generator
{
sm_generator* prev;

@ -1,66 +1,34 @@
#include <state.h>
#include <gen.h>
#include <sm/state.h>
#include <sm/gen.h>
#include <cstdio>
struct _test {
int a, b;
};
SM_GENERATOR(sm_test_2)
SM_GENERATOR(powers_of_two)
{
int* a = SM_VAR(-10);
int* a = SM_VAR(1);
SM_BEGIN;
while( (*a) < 0 ) {
printf("(2) a = %d\n", *a);
{
while( (*a) > 0 ) {
SM_YIELD_VALUE(*a);
(*a)+=1;
(*a) <<= 1;
}
}
printf("Done!\n");
SM_END;
}
SM_GENERATOR(sm_test)
{
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 main()
{
auto state = sm_new_state();
auto gen = sm_generate(&sm_test);
sm_state_setopt(state, INTERMEDIATE_RETURNS, false);
auto gen = sm_generate(&powers_of_two);
sm_output output;
int out_int=0;
int out_int;
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?
printf("Power of two: %d\n", out_int);
}
printf("\n\nFinal output: %d\n", out_int);
sm_free_output(&output);
sm_free_generator(gen);

Loading…
Cancel
Save