RodrigoDornelles / 3bc-lang

Low-level language, tiny virtual machine, minimal runtime, intermediate representation, embeddable, easy for beginners. (Friendly Punched cards)
https://3bc-lang.org
GNU General Public License v3.0
232 stars 25 forks source link

standardize virtual machine access api (cpp class) (c posix) #349

Open RodrigoDornelles opened 1 year ago

RodrigoDornelles commented 1 year ago

It would be interesting to standardize a virtual machine access api, in order to avoid using the internal functions.

Posix C Style

a posix style, like unistd working with ids so they are integers, makes it easy to integrate with another programming language.

besides of course protecting the struct's internal structure.

int main()
{
    /** create vm **/
    int app_id = lang_3bc_init();

    /** config vm**/
    lang_3bc_set(app_id, TTY_SOURCE_LOAD_FILE, argc[1]);

    /** add some code **/
    lang_3bc_line(app_id, MODE, NILL, 2);
    lang_3bc_line(app_id, STRI, NILL, 3);
    lang_3bc_line(app_id, STRC, NILL, 'b');
    lang_3bc_line(app_id, STRC, NILL, 'c');

    /** run vm **/
    while(lang_3bc_running(app_id)) {
       lang_3bc_update(app_id);
    }

    lang_3bc_destroy(app_id);
}

Class C++ Style

an alternative for C++ users as a basic and easier wrapper.

int main()
{
    /** create vm **/
    Tbc app_obj;

    /** config vm**/
    app_obj.set(TTY_SOURCE, TTY_LOAD_FILE, argc[1]);

    /** add some code **/
    app_obj
       .line(MODE, NILL, 2)
       .line(STRI, NILL, 3)
       .line(STRC, NILL, 'b')
       .line(STRC, NILL, 'c');

    /** run vm **/
    while(app_obj.is_running()) {
      app_obj.update();
    }
}
RodrigoDornelles commented 1 year ago

368

#include "../src/3bc.h"

int main(int argc, char** argv)
{
    uint8_t appid = app_3bc_temp_init();

    app_3bc_install(appid, PACKAGE_DESKTOP_DEFAULTS);
    app_3bc_install(appid, PACKAGE_DESKTOP_CLI);
    app_3bc_set_cli(appid, argc, argv);

    while (app_3bc_running(appid)){
        app_3bc_update(appid);
    }

    app_3bc_destroy(appid);
    return 0;
}