コード例 #1
0
ファイル: cow.cpp プロジェクト: BigZaphod/COW
int main( int argc, char** argv )
{
	if( argc < 2 )
	{
		printf( "Usage: %s program.cow\n\n", argv[0] );
		exit( 1 );
	}

	FILE* f = fopen( argv[1], "rb" );

	if( f == NULL )
	{
		printf( "Cannot open source file [%s].\n", argv[1] );
        exit( 1 );
	}

    char buf[3];
    memset( buf, 0, 3 );
    //int pos = 0; // tola: unused?

    while( !feof(f) )
    {
        int found = 0;
        buf[2] = fgetc( f );

        if(( found = !strncmp( "moo", buf, 3 ) ))
            program.push_back( 0 );
        else if(( found = !strncmp( "mOo", buf, 3 ) ))
            program.push_back( 1 );
        else if(( found = !strncmp( "moO", buf, 3 ) ))
            program.push_back( 2 );
        else if(( found = !strncmp( "mOO", buf, 3 ) ))
            program.push_back( 3 );
        else if(( found = !strncmp( "Moo", buf, 3 ) ))
            program.push_back( 4 );
        else if(( found = !strncmp( "MOo", buf, 3 ) ))
            program.push_back( 5 );
        else if(( found = !strncmp( "MoO", buf, 3 ) ))
            program.push_back( 6 );
        else if(( found = !strncmp( "MOO", buf, 3 ) ))
            program.push_back( 7 );
        else if(( found = !strncmp( "OOO", buf, 3 ) ))
            program.push_back( 8 );
        else if(( found = !strncmp( "MMM", buf, 3 ) ))
            program.push_back( 9 );
        else if(( found = !strncmp( "OOM", buf, 3 ) ))
            program.push_back( 10 );
        else if(( found = !strncmp( "oom", buf, 3 ) ))
            program.push_back( 11 );
        // from here down,  distributed digestion specific calls
        else if(( found = !strncmp( "MMm", buf, 3 ) ))
            program.push_back( 12 );
        else if(( found = !strncmp( "MmM", buf, 3 ) ))
            program.push_back( 13 );
        else if(( found = !strncmp( "Oom", buf, 3 ) ))
            program.push_back( 14 );
        else if(( found = !strncmp( "oOm", buf, 3 ) ))
            program.push_back( 15 );
        else if(( found = !strncmp( "OoM", buf, 3 ) ))
            program.push_back( 16 );
        else if(( found = !strncmp( "oOM", buf, 3 ) ))
            program.push_back( 17 );
        else if(( found = !strncmp( "ooo", buf, 3 ) ))
            program.push_back( 18 );
        else if(( found = !strncmp( "mmm", buf, 3 ) ))
            program.push_back( 19 );
            
        if( found )
        {
            memset( buf, 0, 3 );
        }
        else
        {
            buf[0] = buf[1];
            buf[1] = buf[2];
            buf[2] = 0;
        }
    }

	fclose( f );

	printf( "Welcome to COW!\n\nExecuting [%s]...\n\n", argv[1] );

    // init main memory.
    for(int i=0; i<num_stomachs; ++i)
    {
        memory[i].push_back(0);
        mem_poses[i] = memory[i].begin();
    }
    stomach = 0;

    prog_pos = program.begin();
    while( prog_pos != program.end() )
        if( !exec( *prog_pos ) )
            break;

    quit( false );

	return 0;
}
コード例 #2
0
bool op_ring::execute()
{
    if( pos != 0 ) switch( pos )
    {
        // One
        case 2:
            value=1;
            break;

        // Zero
        case 3:
            value=0;
            break;

        // Load
        case 4:
            value=*mem_pos;
            break;

        // Store
        case 5:
            *mem_pos = value;
            break;

        // DAdd
        case 7:
            {
                int t = mem_pos - memory.begin();
                t += value;
                if( t < 0 )
                    return false;   // out of bounds!
                if( t < memory.size() )
                    mem_pos = memory.begin() + t;
                else
                {
                    while( memory.size() <= t )
                        memory.push_back(0);
                    mem_pos = memory.begin() + t;
                }
            }
            break;

        // Logic
        case 8:
            if( *mem_pos != 0 )
                value = value && 1;
            else
                value = 0;
            break;

        // If
        case 9:
            if( *mem_pos == 0 )
                break;
            // else fall through!

        // PAdd
        case 6:
            {
                int t = prog_pos - program.begin();
                t += value;
                if( t < 0 || t >= program.size() )
                    return false;   // out of bounds!
                prog_pos = program.begin() + t;
                next_instruction = false;
            }
            break;

        // IntIO
        case 10:
            if( value == 0 )
            {
                // read integer
                char buf[100];
                int c = 0;
                while( c < sizeof(buf)-1 )
                { 
                    buf[c] = getchar();
                    c++;
                    buf[c] = 0;
            
                    if( buf[c-1] == '\n' )
                        break;
                }
                // swallow, just in case.
                if( c == sizeof(buf) )
                    while( getchar() != '\n' );
        
                (*mem_pos) = atoi( buf );
            }
            else
                printf( "%d", *mem_pos );
            break;
        
        // AscIO
        case 11:
            if( value == 0 )
            {
                // read character
                (*mem_pos) = getchar();
                while( getchar() != '\n' );
            }
            else
                printf( "%c", *mem_pos );
            break;

        // Exit
        case 1:
        default:
            return false;
    }

    return true;
}
コード例 #3
0
ファイル: cowcomp.cpp プロジェクト: BigZaphod/COW
int main( int argc, char** argv )
{
	if( argc < 2 )
	{
		printf( "Usage: %s program.cow\n\n", argv[0] );
		exit( 1 );
	}

	FILE* f = fopen( argv[1], "rb" );

	if( f == NULL )
	{
		printf( "Cannot open source file [%s].\n", argv[1] );
        exit( 1 );
	}

    char buf[3];
    memset( buf, 0, 3 );
    int pos = 0;

    while( !feof(f) )
    {
        int found = 0;
        buf[2] = fgetc( f );

        if( found = !strncmp( "moo", buf, 3 ) )
            program.push_back( 0 );
        else if( found = !strncmp( "mOo", buf, 3 ) )
            program.push_back( 1 );
        else if( found = !strncmp( "moO", buf, 3 ) )
            program.push_back( 2 );
        else if( found = !strncmp( "mOO", buf, 3 ) )
            program.push_back( 3 );
        else if( found = !strncmp( "Moo", buf, 3 ) )
            program.push_back( 4 );
        else if( found = !strncmp( "MOo", buf, 3 ) )
            program.push_back( 5 );
        else if( found = !strncmp( "MoO", buf, 3 ) )
            program.push_back( 6 );
        else if( found = !strncmp( "MOO", buf, 3 ) )
            program.push_back( 7 );
        else if( found = !strncmp( "OOO", buf, 3 ) )
            program.push_back( 8 );
        else if( found = !strncmp( "MMM", buf, 3 ) )
            program.push_back( 9 );
        else if( found = !strncmp( "OOM", buf, 3 ) )
            program.push_back( 10 );
        else if( found = !strncmp( "oom", buf, 3 ) )
            program.push_back( 11 );
            
        if( found )
        {
            memset( buf, 0, 3 );
        }
        else
        {
            buf[0] = buf[1];
            buf[1] = buf[2];
            buf[2] = 0;
        }
    }

	fclose( f );

	printf( "Compiling [%s]...\n", argv[1] );

    // init main memory.
    /*
    memory.push_back( 0 );
    mem_pos = memory.begin();
    */
    
    output = fopen( "cow.out.cpp", "wb" );
    fprintf( output, "#include <stdio.h>\n" );
    fprintf( output, "#include <vector>\n" );
    fprintf( output, "typedef std::vector<int> t_;t_ m;t_::iterator p;\n" );
    fprintf( output, "bool h;int r;\n" );
    fprintf( output, "void rterr(){puts(\"Runtime error.\\n\");}\n" );
    fprintf( output, "int main(int a,char** v){\n" );
    fprintf( output, "m.push_back(0);p=m.begin();h=false;\n" );

    prog_pos = program.begin();
    while( prog_pos != program.end() )
        if( !compile( *prog_pos, true ) )
        {
            printf( "ERROR!\n" );
            break;
        }
        
    fprintf( output, "x:return(0);}\n" );        
    fclose( output );

    printf( "C++ source code: cow.out.cpp\n" );

    #ifdef COMPILER
        std::string path( (const char*)COMPILER );
        path.append( " " );
        path.append( (const char*)NAME_FLAG );
        path.append( " " );
        path.append( (const char*)OUTPUT_EXEC );
        path.append( " " );
        path.append( (const char*)FLAGS );
        path.append( " " );
        path.append( (const char*)OUTPUT_CPP );
        if( system( path.c_str() ) )
            printf( "\n\nCould not compile.  Possible causes:  C++ compiler is not installed, not in path, or not named '%s' or there is a bug in this compiler.\n\n", COMPILER );
        else
            printf( "Executable created: cow.out\n" );
    #endif

	return 0;
}
コード例 #4
0
int main( int argc, char** argv )
{
	if( argc < 2 )
	{
		printf( "Usage: %s program.wrl\n\n", argv[0] );
		exit( 1 );
	}

	FILE* f = fopen( argv[1], "rb" );

	if( f == NULL )
	{
		printf( "Cannot open source file [%s].\n", argv[1] );
        exit( 1 );
    }

    while( !feof(f) )
    {
        char buf = fgetc( f );
        if( buf == '1' )
            program.push_back( true );
        else if( buf == '0' )
            program.push_back( false );
    }

	fclose( f );

	printf( "Welcome to Whirl!\n\nExecuting [%s]...\n\n", argv[1] );

    // init main memory.
    memory.push_back( 0 );
    mem_pos = memory.begin();

    prog_pos = program.begin();
    bool execute = false;
    while( prog_pos != program.end() )
    {
        next_instruction = true;

        if( *prog_pos )
        {
            cur->rotate();
            execute = false;
        }
        else
        {
            cur->switch_dir();
            if( execute )
            {
                if( !cur->execute() )
                    return 0;
                (cur == &ops)? cur = &math: cur = &ops;                
                execute = false;
            }
            else
                execute = true;
        }
        
        if( next_instruction )
            prog_pos++;
    }

	printf( "\n" );

	return 0;
}