/*------------------------------------------------------------------------------

 Example source code of a mini BASIC interpreter; compile with SAS/C (smake)

*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <exec/exec.h>
#include <utility/tagitem.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <pragmas/exec_pragmas.h>

// includes for BASIC.library

#include "include/basic.h"
#include "clib/basiclib_protos.h"
#include "pragmas/basiclib_pragmas.h"

// globals

struct Library *BASICBase;

/*------------------------------------------------------------------------------

 Main entry point. Note that this program should be started with the stack set
 to 16 KB or higher (because the interpreter is executed synchronously, ie. it
 uses our stack).

*/

int
main(int argc, UBYTE **argv)
{
    int rc = 20;

    if (BASICBase = OpenLibrary("BASIC.library", BASIC_VERSION))
    {
        static struct TagItem ctags[] = { BASIC_CONSOLE, (ULONG)NULL, BASIC_INTERACTIVE, (ULONG)TRUE, TAG_END };

        void *context;

        // create interactive interpreter; use current console (BASIC_CONSOLE, NULL) if not started via wb, ie. if argc != 0

        if (argc)
            context = basic_context(&ctags[0]);
        else
            context = basic_context(&ctags[1]);

        if (context)
        {
            basic_message(context, "BASIC interpreter, enter QUIT to exit");

            // main loop: get commands and execute them until user issues QUIT command

            do
            {
                basic_iterate(context);

            } while (basic_chkabort(context) == FALSE);

            // dispose of interpreter context

            rc = basic_dispose(context);
        }
        else
            puts("Failed to create interpreter context");

        CloseLibrary(BASICBase);
    }
    else
        puts("Can not open BASIC.library");

    return(rc);
}
