Exemplo n.º 1
0
Token scanner( FILE *source )
{
    char c;
    Token token;

    if(!tokenBufferEmpty) {
        tokenBufferEmpty = 1;
        return tokenBuffer;
    }

    while( !feof(source) ){
        c = fgetc(source);
        while( isspace(c) )
            c = fgetc(source);

        if( isdigit(c) )
            return getNumericToken(source, c);
        else if( isalpha(c) ){
            token = getWordToken(source, c);
            return checkKeyword(token);
        }
        else
            return getOperatorToken(source, c);
    }
    token.tok[0] = '\0';
    token.type = EOFsymbol;
    return token;
}
Exemplo n.º 2
0
Token scanner( FILE *source )
{
    char c;
    Token token;

    while( !feof(source) ){
        c = fgetc(source);
		
        while( isspace(c) ) c = fgetc(source);
		if( isdigit(c) )
            return getNumericToken(source, c);
		token.tok[0] = c;
        token.tok[1] = '\0';
        if( islower(c) ){
			char next= fgetc(source);	// see next char
        	if( c == 'f' && isspace(next))
                token.type = FloatDeclaration;
            else if( c == 'i' && isspace(next))
                token.type = IntegerDeclaration;
            else if( c == 'p' && isspace(next)){
                token.type = PrintOp;
				strcat(token.tok, " "); 
			}
            else{
				ungetc(next, source);
				token =  getAlphabetToken(token, source, c);
				token.type = Alphabet;	
			}
			return token;
        }

        switch(c){
            case '=':
                token.type = AssignmentOp;
                return token;
            case '+':
                token.type = PlusOp;
                return token;
            case '-':
                token.type = MinusOp;
                return token;
            case '*':
                token.type = MulOp;
                return token;
            case '/':
                token.type = DivOp;
                return token;
            case EOF:
                token.type = EOFsymbol;
                token.tok[0] = '\0';
                return token;
            default:
                printf("Invalid character : %c\n", c);
                exit(1);
        }
    }

    token.tok[0] = '\0';
    token.type = EOFsymbol;
    return token;
}
Exemplo n.º 3
0
Token scanner( FILE *source )//changed
{
    char c;
    Token token;

    while( !feof(source) ){
        c = fgetc(source);

        while( isspace(c) ) c = fgetc(source);

        if( isdigit(c) )
            return getNumericToken(source, c);

        token.tok[0] = c;
        token.tok[1] = '\0';
        if( islower(c) || isupper(c) ){//changed
	        int len=1;
            c = fgetc(source);
          	while( islower(c) || isupper(c)){
				token.tok[len++]=c;
				c = fgetc(source);
			}
			ungetc(c, source);
			c=token.tok[len-1];
			token.tok[len]='\0';
			if( len>1)
				token.type = Alphabet;
			else if( c == 'f' )
                token.type = FloatDeclaration;
            else if( c == 'i' )
                token.type = IntegerDeclaration;
            else if( c == 'p' )
                token.type = PrintOp;
            else
                token.type = Alphabet;
            return token;
        }

        switch(c){
            case '=':
                token.type = AssignmentOp;
                return token;
            case '+':
                token.type = PlusOp;
                return token;
            case '-':
                token.type = MinusOp;
                return token;
            case '*':
                token.type = MulOp;
                return token;
            case '/':
                token.type = DivOp;
                return token;
            case EOF:
                token.type = EOFsymbol;
                token.tok[0] = '\0';
                return token;
            default:
                printf("Invalid character : %c\n", c);
                exit(1);
        }
    }

    token.tok[0] = '\0';
    token.type = EOFsymbol;
    return token;
}
Exemplo n.º 4
0
void MapLoader::ParseVertices(MeshData * mesh) {
	int components = 0, formatComponents = mesh->structure.getComponentCount();
	int totalComponentsSize = 0;
	
	std::vector<std::pair<std::string, TGen::FormatType> > data;
	data.reserve(100);
	
	while (currentToken != endIter && currentToken->first != MapTokenBlockEnd) {
		while (currentToken->first == MapTokenEndOfLine && currentToken->first != MapTokenBlockEnd)
			StepToken();
		
		if (currentToken->first == MapTokenBlockEnd)
			break;
		
		std::string value = getNumericToken("mesh.vertices: expecting numeric value for component", false, false);
		int componentPos = components % formatComponents;

		TGen::VertexElement vel = mesh->structure.getElementAtComponent(componentPos);
		std::cout << "VALUE: " << value << " (" << componentPos << ") TYPE " << int(vel.dataType) << std::endl;
	
		totalComponentsSize += TGen::FormatTypeSize(vel.dataType);
		
		data.push_back(std::pair<std::string, TGen::FormatType>(value, vel.dataType));
		
		components++;
		StepToken();
	}
	
	if (components % formatComponents)
		throw TGen::RuntimeException("MapLoader::ParseVertices", "number of components and buffer format mismatch");
	
	std::cout << "Total components: " << data.size() << " bytes: " << totalComponentsSize << std::endl;
	
	void * vertexData = static_cast<void *>(malloc(totalComponentsSize));
	void * pos = vertexData;
	
	for (int i = 0; i < data.size(); ++i) {
		std::stringstream ss;
		
		if (reinterpret_cast<uchar *>(pos) >= reinterpret_cast<uchar *>(vertexData) + totalComponentsSize)
			throw TGen::RuntimeException("MapLoader::ParseVertices", "out of bounds when writing vertex data");
		
		switch (data[i].second) {
			case TGen::TypeFloat: {
				float value = 0.0f;
				ss << data[i].first;
				ss >> value;
				
				memcpy(pos, &value, sizeof(float));
				break;
			}
				
			case TGen::TypeDouble: {
				float value = 0.0;
				ss << data[i].first;
				ss >> value;
				
				memcpy(pos, &value, sizeof(double));
				break;
			}
				
			case TGen::TypeUnsignedInt:
			case TGen::TypeInt:
			{
				unsigned int value = 0;
				ss << data[i].first;
				ss >> value;
				
				memcpy(pos, &value, sizeof(int));
				break;
			}
				
			case TGen::TypeUnsignedShort:
			case TGen::TypeShort:
			{
				unsigned short value = 0;
				ss << data[i].first;
				ss >> value;
				
				memcpy(pos, &value, sizeof(short));
				break;
			}
				
			case TGen::TypeUnsignedByte:
			case TGen::TypeByte:
			{
				unsigned char value = 0;
				ss << data[i].first;
				ss >> value;
				
				memcpy(pos, &value, sizeof(char));
				break;
			}

			default:
				throw TGen::RuntimeException("MapLoader::ParseVertices", "invalid datatype");
		}
		
		pos = reinterpret_cast<uchar *>(pos) + TGen::FormatTypeSize(data[i].second);
	}
	
	std::cout << "written " << reinterpret_cast<uchar *>(pos) - reinterpret_cast<uchar *>(vertexData) << " bytes" << std::endl;

	mesh->data = vertexData;
	// TODO: data, vad gör man med den va. spara alla MeshData i en lista som betas av när man länkar, då skapar den alla vbs osv
}