Exemplo n.º 1
0
static void cellToQString( const UCell* val, QString& str )
{
    switch( ur_type(val) )
    {
        case UT_NONE:
            str.clear();
            break;

        case UT_CHAR:
            str = QChar( ur_int(val) );
            break;

        case UT_INT:
            str.setNum( ur_int(val) );
            break;

        case UT_DECIMAL:
            str.setNum( ur_decimal(val) );
            break;

        case UT_BIGNUM:
            str.setNum( bignum_l(val) );
            break;

        case UT_STRING:
        case UT_FILE:
        {
            USeriesIter si;
            int len;
            ur_seriesSlice( UT, &si, val );
            len = si.end - si.it;
            switch( si.buf->form )
            {
                case UR_ENC_LATIN1:
                    str = QString::fromLatin1( si.buf->ptr.c + si.it, len );
                    break;
                case UR_ENC_UTF8:
                    str = QString::fromUtf8( si.buf->ptr.c + si.it, len );
                    break;
                case UR_ENC_UCS2:
                    str = QString::fromUtf16( si.buf->ptr.u16 + si.it, len );
                    break;
            }
        }
            break;

        default:
        {
            UBuffer buf;
            ur_strInit( &buf, UR_ENC_LATIN1, 0 );
            ur_toStr( UT, val, &buf, 0 );
            str = QString::fromLatin1( buf.ptr.c, buf.used );
            ur_strFree( &buf );
        }
            break;
    }
}
Exemplo n.º 2
0
int main( int argc, char** argv )
{
    char cmd[ 2048 ];
    BoronApp app( argc, argv );
    UThread* ut;
    UBuffer rstr;
    int fileN = 0;
    int ret = 0;


    {
    UEnvParameters param;
    ut = boron_makeEnv( boron_envParam(&param) );
    }
    if( ! ut )
    {
        printf( "boron_makeEnv failed\n" );
        return -1;
    }

    ur_freezeEnv( ut );
    boron_initQt( ut );


    if( argc > 1 )
    {
        int i;
        char* arg;

        for( i = 1; i < argc; ++i )
        {
            arg = argv[i];
            if( arg[0] == '-' )
            {
                switch( arg[1] )
                {
                    case 's':
                        //ur_disable( env, UR_ENV_SECURE );
                        break;

                    case 'h':
                        usage( argv[0] );
                        return 0;
                }
            }
            else
            {
                fileN = i;
                break;
            }
        }
    }

    ur_strInit( &rstr, UR_ENC_UTF8, 0 );

#ifdef _WIN32
    {
    WORD wsver;
    WSADATA wsdata;
    wsver = MAKEWORD( 2, 2 );
    WSAStartup( wsver, &wsdata );
    }
#endif

    if( fileN )
    {
        char* pos;

        pos = cmd;
        cmd[ sizeof(cmd) - 1 ] = -1;

        // Create args block for any command line parameters.
        if( (argc - fileN) > 1 )
        {
            int i;
            pos = str_copy( pos, "args: [" );
            for( i = fileN + 1; i < argc; ++i )
            {
                *pos++ = '"';
                pos = str_copy( pos, argv[i] );
                *pos++ = '"';
                *pos++ = ' ';
            }
            *pos++ = ']';
        }
        else
        {
            pos = str_copy( pos, "args: none " );
        }

        pos = str_copy( pos, "do load {" );
        pos = str_copy( pos, argv[fileN] );
        *pos++ = '}';

        assert( cmd[ sizeof(cmd) - 1 ] == -1 && "cmd buffer overflow" );

        if( ! boron_evalUtf8( ut, cmd, pos - cmd ) )
        {
            UCell* ex = ur_exception( ut );
            if( ur_is(ex, UT_ERROR) )
            {
                OPEN_CONSOLE
                reportError( ut, ex, &rstr );
                goto prompt;
            }
            else if( ur_is(ex, UT_WORD) )
            {
                switch( ur_atom(ex) ) 
                {
                    case UR_ATOM_QUIT:
                        goto quit;

                    case UR_ATOM_HALT:
                        goto prompt;
                        break;
                }
            }
        }
    }
    else
    {
        OPEN_CONSOLE
        printf( APPNAME " %s (%s)\n", UR_VERSION_STR, __DATE__ );

prompt:

        while( 1 )
        {
            printf( ")> " );
            fflush( stdout );   /* Required on Windows. */
            fgets( cmd, sizeof(cmd), stdin ); 
#if 0
            {
                char* cp = cmd;
                while( *cp != '\n' )
                    printf( " %d", (int) *cp++ );
                printf( "\n" );
            }
#endif

            if( cmd[0] == ESC )
            {
                // Up   27 91 65
                // Down 27 91 66
                printf( "\n" );
            }
            else if( cmd[0] != '\n' )
            {
#if 0
                if( cmd[0] == 'q' )
                    goto quit;
#endif
                UCell* val = boron_evalUtf8( ut, cmd, -1 );
                if( val )
                {
                    if( ur_is(val, UT_UNSET) ||
                        ur_is(val, UT_CONTEXT) )
                        goto prompt;

                    rstr.used = 0;
                    ur_toStr( ut, val, &rstr, 0 );
                    if( rstr.ptr.c )
                    {
                        ur_strTermNull( &rstr );
                        if( rstr.used > PRINT_MAX )
                        {
                            char* cp = str_copy( rstr.ptr.c + PRINT_MAX - 4,
                                                 "..." );
                            *cp = '\0';
                        }
                        printf( "== %s\n", rstr.ptr.c );
                    }
                }
                else
                {
                    UCell* ex = ur_exception( ut );
                    if( ur_is(ex, UT_ERROR) )
                    {
                        reportError( ut, ex, &rstr );
                    }
                    else if( ur_is(ex, UT_WORD) )
                    {
                        switch( ur_atom(ex) ) 
                        {
                            case UR_ATOM_QUIT:
                                goto quit;

                            case UR_ATOM_HALT:
                                printf( "**halt\n" );
                                break;

                            default:
                                printf( "**unhandled excepetion %s\n",
                                        ur_atomCStr(ut,ur_atom(ex)) );
                                break;
                        }
                    }
                    boron_reset( ut );
                }
            }
        }
    }

quit:

    ur_strFree( &rstr );
    boron_freeQt();
    boron_freeEnv( ut );

#ifdef _WIN32
    WSACleanup();
#endif

    return ret;
}
Exemplo n.º 3
0
int main( int argc, char** argv )
{
    UThread* ut;
    char cmd[ 2048 ];
    double result;
    (void) argc;
    (void) argv;


    printf( "Urlan Calculator Example %s (%s)\n", UR_VERSION_STR, __DATE__ );

    ut = ur_makeEnv( 256, 0, 0, 0, 0 );
    if( ! ut )
    {
        printf( "ur_makeEnv failed\n" );
        return 255;
    }

    defineWords( ut );

    while( 1 )
    {
        printf( ")> " );
        fflush( stdout );   /* Required on Windows. */
        fgets( cmd, sizeof(cmd), stdin );

        if( cmd[0] < ' ' )
        {
            printf( "\n" );
        }
        else if( cmd[0] == 'q' )
        {
            break;
        }
        else
        {
            if( calc_evalCStr( ut, cmd, &result ) )
            {
                printf( "= %f\n", result );
            }
            else
            {
                UBuffer* blk = ur_errorBlock(ut);
                if( blk->used )
                {
                    UBuffer str;

                    ur_strInit( &str, UR_ENC_UTF8, 0 );
                    ur_toStr( ut, blk->ptr.cell, &str, 0 );
                    ur_strTermNull( &str );
                    printf( "%s\n", str.ptr.c );
                    ur_strFree( &str );

                    blk->used = 0;
                }
                else
                    break;
            }
        }
    }

    ur_freeEnv( ut );
    return 0;
}