Adding Functions |
![]() ![]() |
Once you have initialized the MyBASIC2 environment you can create and open an interpreter and start adding your script functions.
Opening the Interpreter
int mb_open( mb_interpreter_t** s );
This function opens an interpreter structure to get ready for parsing and running.
Closing the Interpreter
int mb_close(mb_interpreter_t** s);
This function closes an interpreter structure when it is no longer used. mb_open and mb_close must be matched in pair.
Adding Functions
int mb_register_func( mb_interpreter_t* s, const char* n, mb_func_t f );
This function registers a function pointer into an interpreter structure using a given name. The function to be registered must be a pointer as int (*mb_func_t)( struct mb_interpreter_t*, void** ). A registered function can be called in script.
int mb_remove_func( mb_interpreter_t* s, const char* n );
This function removes a registered function out of an interpreter structure by a given name.
Example
///////////////////////////////////////////////////////////////////////////// // Forward Declare our callback functions int _hello_func( mb_interpreter_t* s, void** l ); int _bye_func( mb_interpreter_t* s, void** l );
. . // Initialize MyBASIC2 mb_init();
// Create and Open an interpreter mb_interpreter_t* bas = NULL; mb_open( &bas );
// Register our functions mb_register_func( bas, "HELLO", _hello_func ); mb_register_func( bas, "BYE", _bye_func );
// Load and run the script if ( mb_load_file( bas, "MY_SCRIPT.BAS" ) == MB_FUNC_OK ) { // Note, mb_run will return MB_FUNC_END when normal termination if ( mb_run( bas ) != MB_FUNC_END ) { printf( "Error Running Script File MY_SCRIPT.BAS\n" ); } } else { printf( "Error Loading Script File MY_SCRIPT.BAS\n" ); }
// Remove functions and close interpreter mb_remove_func( "HELLO" ); mb_remove_func( "BYE );
mb_close( &bas );
// Terminate MyBASIC2 mb_exit(); . .
///////////////////////////////////////////////////////////////////////////// // int _hello_func( mb_interpreter_t* s, void** l ) { // Parse Empty Parameters BEGIN_PARSE( s, l ) END_PARSE
// Create output String COPY_STRING( pHello, "HELLO" );
// Return the string "HELLO" RETURN_STRING( pHello ); }
///////////////////////////////////////////////////////////////////////////// // int _bye_func( mb_interpreter_t* s, void** l ) { // Parse Empty Parameters BEGIN_PARSE( s, l ) END_PARSE
// Terminate Interpreter RETURN_INT_BYE( 1 ); }
|