jingoro2112 / wrench

practical embedded script interpreter
MIT License
108 stars 10 forks source link
arduino c-like compiler esp32 interpreter optimized script scripting-language virtual-machine vm

https://home.workshopfriends.com/wrench/www

A full-featured compiler+interpreter that uses a bare minimum of ram and program space.

How little? The wrench Virtual Machine compiles to ~28k on an Arduino, and uses less than 700 Bytes of RAM to operate, it is fully functional on an Uno Mini.

Highlights:

----- Step 1: The entire source tree is included, but is wrapped up in two files, src/wrench.h and src/wrench.cpp simply include these in your build and you have everything.

----- Step 2: Here is a complete source-code example:

include

include

include

void print( WRContext c, const WRValue argv, const int argn, WRValue& retVal, void* usr ) { char buf[1024]; for( int i=0; i<argn; ++i ) { printf( "%s", argv[i].asString(buf,1024) ); } }

const char* wrenchCode = "print( \"Hello World!\n\" );" "for( var i=0; i<10; i++ ) " "{ " " print( i ); " "} " "print(\"\n\"); ";

int main( int argn, char* argv ) { WRState w = wr_newState(); // create the state

wr_registerFunction( w, "print", print ); // bind a function

unsigned char* outBytes; // compiled code is alloc'ed
int outLen;

int err = wr_compile( wrenchCode, strlen(wrenchCode), &outBytes, &outLen ); // compile it
if ( err == 0 )
{
    wr_run( w, outBytes, outLen ); // load and run the code!
    free( outBytes ); // clean up 
}

wr_destroyState( w );

return 0;

}

---- Step 3: compile the above with something like:

        g++ -o example example.c wrench.cpp

---- Step 4: done!