jingoro2112 / wrench

practical embedded script interpreter
MIT License
106 stars 9 forks source link

loop and setup callback help #17

Closed rjjrbatarao closed 9 months ago

rjjrbatarao commented 9 months ago

I am doing something like arduino programming by registering function like below

void wr_loop(WRState* w, const WRValue* argv, const int argn, WRValue& retVal, void* usr) {
  if ( argn > 0 )
  {
    // dont pass any params here
    return;
  }
}

void wr_setup(WRState* w, const WRValue* argv, const int argn, WRValue& retVal, void* usr) {
  if ( argn > 0 )
  {
    // dont pass any params here
    return;
  }
}

void setup(){
  ........
  wr_registerFunction( w, "loop", wr_loop );
  wr_registerFunction( w, "setup", wr_setup );
  wr_callFunction( w,, "setup" );
  wr_run( w, code1, len1);
  wr_run( w, code2, len2);
  .......
}

void loop(){
   wr_callFunction( w,, "loop" ); // context must not be destroyed to run the loop
}

there would be multiple wrench code that runs on setup with this format like

code1

data = 0;
function setup(){
data = 1;
}
function loop(){
data = data + 1;
print(data);
}

code2

data = 0;
function setup(){
data = 2;
}
function loop(){
data = data + 2;
print(data);
}

now on the console we should see 1 2 2 4 3 6 4 8 5 20 6 like every loop should remain

other solution would be to use while loop but im afraid it could block other wrench code if i do this way

data = 2;
while(1){
data = data + 2;
print(data);
}

there may be some other approach but i want things to be simplier

rjjrbatarao commented 9 months ago

I thought of other way of doing this