Exemplo n.º 1
0
/**
 * Creates an expression wrapping an identifier
 */
struct memorycontainer* createIdentifierExpression(char * identifier) {
    struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
    if (doesVariableExist(identifier)) {
        memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short);
        memoryContainer->data=(char*) malloc(memoryContainer->length);
        memoryContainer->lineDefns=NULL;

        int location=appendStatement(memoryContainer, IDENTIFIER_TOKEN, 0);
        appendVariable(memoryContainer, getVariableId(identifier, 0), location);
    } else {
        memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short);
        memoryContainer->data=(char*) malloc(memoryContainer->length);
        appendStatement(memoryContainer, FN_ADDR_TOKEN, 0);

        struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition));

        defn->next=NULL;
        defn->type=4;
        defn->linenumber=line_num;
        defn->name=(char*) malloc(strlen(identifier)+1);
        strcpy(defn->name, identifier);
        defn->currentpoint=sizeof(unsigned char);

        memoryContainer->lineDefns=defn;

        if (currentCall==NULL) {
            mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(identifier)+1);
            strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], identifier);
        } else {
            currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(identifier)+1);
            strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], identifier);
        }
    }
	return memoryContainer;
}
Exemplo n.º 2
0
/**
 * Appends and returns a for iteration. This puts in the assignment of the initial value to the variable, the normal stuff and then
 * termination check at each iteration along with jumping to next iteration if applicable
 */
struct memorycontainer* appendForStatement(char * identifier, struct memorycontainer* exp, struct memorycontainer* block) {
	struct memorycontainer* initialLet=appendLetStatement("epy_i_ctr", createIntegerExpression(0));
	struct memorycontainer* variantLet=appendLetStatement(identifier, createIntegerExpression(0));
	struct memorycontainer* incrementLet=appendLetStatement("epy_i_ctr", createAddExpression(createIdentifierExpression("epy_i_ctr"), createIntegerExpression(1)));

	struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	memoryContainer->length=sizeof(unsigned char)*2+sizeof(unsigned short) * 4 + exp->length + (block != NULL ? block->length : 0) +
			initialLet->length + variantLet->length + incrementLet->length;
	memoryContainer->data=(char*) malloc(memoryContainer->length);
	memoryContainer->lineDefns=NULL;

	struct memorycontainer* combinedBlock=block != NULL ? concatenateMemory(block, incrementLet) : incrementLet;

	unsigned int position=0;

	struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition));
	defn->next=memoryContainer->lineDefns;
	defn->type=0;
	defn->linenumber=currentForLine;
	defn->currentpoint=initialLet->length;
	memoryContainer->lineDefns=defn;

	position=appendMemory(memoryContainer, initialLet, position);
	position=appendMemory(memoryContainer, variantLet, position);
	position=appendStatement(memoryContainer, FOR_TOKEN, position);
	position=appendVariable(memoryContainer, getVariableId("epy_i_ctr", 0), position);
	position=appendVariable(memoryContainer, getVariableId(identifier, 0), position);
	position=appendMemory(memoryContainer, exp, position);
	unsigned short length=(unsigned short) combinedBlock->length;
	memcpy(&memoryContainer->data[position], &length, sizeof(unsigned short));
	position+=sizeof(unsigned short);
	position=appendMemory(memoryContainer, combinedBlock, position);
	position=appendStatement(memoryContainer, GOTO_TOKEN, position);
	defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition));
	defn->next=memoryContainer->lineDefns;
	defn->type=1;
	defn->linenumber=currentForLine;
	defn->currentpoint=position;
	memoryContainer->lineDefns=defn;
	currentForLine--;
	return memoryContainer;
}
Exemplo n.º 3
0
void Shader::setFloatArray(const std::string &uniformVarName,
		const std::vector<float>& f) {
	float *farray;
	farray = new float[f.size()];
	int i = 0;
	for (int i = 0; i < f.size(); i++) {
		farray[i] = f[i];
	}
	glUniform1fv(getVariableId(uniformVarName), f.size(), farray);
	std::cerr << "fuite de memoire??"<<std::endl;
}
Exemplo n.º 4
0
void Shader::setVec3Array(const std::string &uniformVarName,
		const std::vector<sf::Vector3f>& f) {
	GLfloat *farray;
	farray = new float[f.size() * 3];
	int glIndex = 0;
	for (int i = 0; i < f.size(); i++) {
		farray[glIndex++] = f[i].x;
		farray[glIndex++] = f[i].y;
		farray[glIndex++] = f[i].z;
	}
	glUniform3fv(getVariableId(uniformVarName), 1, farray);
	std::cerr << "fuite de memoire??"<<std::endl;
}
Exemplo n.º 5
0
static struct memorycontainer* appendLetIfNoAliasStatement(char * identifier, struct memorycontainer* expressionContainer) {
	struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	memoryContainer->length=sizeof(unsigned char)+sizeof(unsigned short) + expressionContainer->length;
	memoryContainer->data=(char*) malloc(memoryContainer->length);
	memoryContainer->lineDefns=NULL;

	unsigned int position=0;

	position=appendStatement(memoryContainer, LETNOALIAS_TOKEN, position);
	position=appendVariable(memoryContainer, getVariableId(identifier, 1), position);
	appendMemory(memoryContainer, expressionContainer, position);
	return memoryContainer;
}
Exemplo n.º 6
0
/**
 * Creates an expression wrapping an identifier array access
 */
struct memorycontainer* createIdentifierArrayAccessExpression(char* identifier, struct stack_t* index_expressions) {
    int lenOfIndexes=getStackSize(index_expressions);

	struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	memoryContainer->length=sizeof(unsigned short)+(sizeof(unsigned char)*2);
	memoryContainer->data=(char*) malloc(memoryContainer->length);
	memoryContainer->lineDefns=NULL;

	unsigned int position=0;

	position=appendStatement(memoryContainer, ARRAYACCESS_TOKEN, position);
	position=appendVariable(memoryContainer, getVariableId(identifier, 1), position);
	unsigned char packageNumDims=(unsigned char) lenOfIndexes;
    memcpy(&memoryContainer->data[position], &packageNumDims, sizeof(unsigned char));
    position+=sizeof(unsigned char);

    int i;
	for (i=0;i<lenOfIndexes;i++) {
        memoryContainer=concatenateMemory(memoryContainer, getExpressionAt(index_expressions, i));
	}
	return memoryContainer;
}
Exemplo n.º 7
0
/**
 * Appends and returns the setting of an array element (assignment) statement
 */
struct memorycontainer* appendArraySetStatement( char* identifier, struct stack_t* indexContainer,
		struct memorycontainer* expressionContainer) {
	struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	memoryContainer->length=(sizeof(unsigned char)*2)+sizeof(unsigned short);
	memoryContainer->data=(char*) malloc(memoryContainer->length);
	memoryContainer->lineDefns=NULL;

	unsigned int position=0;

	position=appendStatement(memoryContainer, ARRAYSET_TOKEN, position);
	position=appendVariable(memoryContainer, getVariableId(identifier, 1), position);

	unsigned char numIndexes=(unsigned char) getStackSize(indexContainer);
	memcpy(&memoryContainer->data[position], &numIndexes, sizeof(unsigned char));
    position+=sizeof(unsigned char);
	int i;
	for (i=0;i<numIndexes;i++) {
        memoryContainer=concatenateMemory(memoryContainer, getExpressionAt(indexContainer, i));
	}
    memoryContainer=concatenateMemory(memoryContainer, expressionContainer);
	return memoryContainer;
}
Exemplo n.º 8
0
void Shader::setMat4(const std::string &uniformVarName, const glm::mat4& mat) {
	glUniformMatrix4fv(getVariableId(uniformVarName), 1, GL_FALSE,
			glm::value_ptr(mat));
}
Exemplo n.º 9
0
void Shader::setVec4(const std::string &uniformVarName, const glm::vec4& vector) {
	glUniform4f(getVariableId(uniformVarName), vector.x, vector.y, vector.z, vector.t);
}
Exemplo n.º 10
0
void Shader::setVec2(const std::string &uniformVarName,
		const glm::vec2& vector) {
	glUniform2f(getVariableId(uniformVarName), vector.x, vector.y);
}
Exemplo n.º 11
0
void Shader::setInt(const std::string &uniformVarName, int value) {
	glUniform1i(getVariableId(uniformVarName), value);
}
Exemplo n.º 12
0
/**
 * Adds a variable to the symbol table if it is not already present
 */
void addVariableIfNeeded( char * name) {
	getVariableId(name, 1);
}
Exemplo n.º 13
0
/**
 * Appends a new function statement to the function list which is held by the memory manager.
 * This also appends a return statement to the end of the function body and registers
 * the current goto point as the function name
 */
void appendNewFunctionStatement(char* functionName, struct stack_t * args, struct memorycontainer* functionContents) {
	struct functionDefinition * fn=(struct functionDefinition*) malloc(sizeof(struct functionDefinition));
	fn->name=(char*) malloc(strlen(functionName) + 1);
	strcpy(fn->name, functionName);
	fn->called=0;

	unsigned short numberArgs=(unsigned short) getStackSize(args);
	struct memorycontainer* numberArgsContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	numberArgsContainer->length=sizeof(unsigned short) * (numberArgs + 1);
	numberArgsContainer->data=(char*) malloc(sizeof(unsigned short) * (numberArgs + 1));
	numberArgsContainer->lineDefns=NULL;

	((unsigned short *) numberArgsContainer->data)[0]=numberArgs;

	struct memorycontainer* assignmentContainer=NULL;

	int i;
	for (i=0;i<numberArgs;i++) {
		if (getTypeAt(args, i) == 2) {
			((unsigned short *) numberArgsContainer->data)[i+1]=getVariableId(getIdentifierAt(args, i), 1);
		} else {
			struct identifier_exp * idexp=getExpressionIdentifierAt(args, i);
			if (assignmentContainer == NULL) {
				assignmentContainer=appendLetIfNoAliasStatement(idexp->identifier, idexp->exp);
			} else {
				assignmentContainer=concatenateMemory(assignmentContainer, appendLetIfNoAliasStatement(idexp->identifier, idexp->exp));
			}
			((unsigned short *) numberArgsContainer->data)[i+1]=getVariableId(idexp->identifier, 1);
		}
	}

	clearStack(args);

	if (assignmentContainer != NULL) numberArgsContainer=concatenateMemory(numberArgsContainer, assignmentContainer);

	struct memorycontainer* completedFunction=concatenateMemory(concatenateMemory(numberArgsContainer, functionContents),
			appendReturnStatement());

	struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition));
	defn->next=completedFunction->lineDefns;
	defn->type=2;
	defn->currentpoint=0;
	defn->name=(char*) malloc(strlen(functionName) + 1);
	strcpy(defn->name, functionName);
	completedFunction->lineDefns=defn;

	fn->contents=completedFunction;
	fn->numberEntriesInSymbolTable=current_var_id - currentSymbolTableId;
	fn->recursive=isFnRecursive;
	fn->number_of_fn_calls=currentCall->number_of_calls;
	if (currentCall->number_of_calls == 0) {
		fn->functionCalls=NULL;
	} else {
		fn->functionCalls=(char**) malloc(sizeof(char*) * currentCall->number_of_calls);
		memcpy(fn->functionCalls, currentCall->calledFunctions, sizeof(char*) * currentCall->number_of_calls);
	}
	free(currentFunctionName);
	currentFunctionName=NULL;
	currentCall=NULL;
	addFunction(fn);
}
Exemplo n.º 14
0
/**
 * @brief Sets a texture from an OpenGL texture ID
 *
 * @param uniformLocation
 * @param texture
 */
void Shader::setTexture(const std::string &uniformLocation, GLuint texture) {
	int uniformID = getVariableId(uniformLocation);
	//cout << "setTexture " << texture << " for uniform " << uniformLocation << "("<<uniformID<<")" << endl;
	m_textures[uniformID] = shared_ptr<Texture>(new Texture(texture));
}
Exemplo n.º 15
0
/**
 * @brief Sets an uniform GLSL float value
 * WARNING: This has to be called after the shader has been enabled (by calling the enable() function)!
 *
 * @param uniformVarName
 *      The uniform variable name in the GLSL shader
 * @param value
 *      The value to set
 */
void Shader::setFloat(const std::string &uniformVarName, float value) {
//	std::cout << getVariableId(uniformVarName)<<std::endl;
	glUniform1f(getVariableId(uniformVarName), value);
}
Exemplo n.º 16
0
/**
 * @brief Sets a texture from a Texture object
 *
 * @param uniformLocation
 * @param texture
 *     A valid Texture object
 */
void Shader::setTexture(const std::string &uniformLocation, const shared_ptr<Texture>& texture) {
	int uniformID = getVariableId(uniformLocation);
	//cout << "setTexture " << texture << " for uniform " << uniformLocation << "("<<uniformID<<")" << endl;
//	m_textures[texture] = uniformID;
	m_textures[uniformID] = texture;
}
Exemplo n.º 17
0
/**
 * Appends and returns a call function, this is added as a placeholder and then resolved at the end to point to the absolute byte code location
 * which is needed as the function might appear at any point
 */
struct memorycontainer* appendCallFunctionStatement(char* functionName, struct stack_t* args) {
	char * last_dot=strrchr(functionName,'.');
	if (last_dot != NULL) functionName=last_dot+1;
	if (currentFunctionName != NULL && strcmp(currentFunctionName, functionName) == 0) isFnRecursive=1;

	struct memorycontainer* assignmentContainer=NULL;
	unsigned short numArgs=(unsigned short) getStackSize(args);
	char *isArgIdentifier=(char*) malloc(numArgs);
	unsigned short *varIds=(unsigned short*) malloc(sizeof(unsigned short) * numArgs);
	char * varname=(char*) malloc(strlen(functionName)+5);
	int i;
	for (i=0;i<numArgs;i++) {
		struct memorycontainer* expression=getExpressionAt(args, i);
		unsigned char command=((unsigned char*) expression->data)[0];
		if (command != IDENTIFIER_TOKEN) {
			isArgIdentifier[i]=0;
			sprintf(varname,"%s#%d", functionName, i);
			if (assignmentContainer == NULL) {
				assignmentContainer=appendLetStatement(varname, getExpressionAt(args, i));
			} else {
				assignmentContainer=concatenateMemory(assignmentContainer, appendLetStatement(varname, getExpressionAt(args, i)));
			}
		} else {
			isArgIdentifier[i]=1;
			varIds[i]=*((unsigned short*) (&((char*) expression->data)[1]));
			free(expression->data);
		}
	}
	struct memorycontainer* memoryContainer = (struct memorycontainer*) malloc(sizeof(struct memorycontainer));
	memoryContainer->length=sizeof(unsigned short)*(2+numArgs)+sizeof(unsigned char);
	memoryContainer->data=(char*) malloc(memoryContainer->length);
    unsigned int position=0;

	if (doesVariableExist(functionName)) {
        memoryContainer->lineDefns=NULL;
        position=appendStatement(memoryContainer, FNCALL_BY_VAR_TOKEN, position);
        position=appendVariable(memoryContainer, getVariableId(functionName, 0), position);
	} else {
        struct lineDefinition * defn = (struct lineDefinition*) malloc(sizeof(struct lineDefinition));
        defn->next=NULL;
        defn->type=3;
        defn->linenumber=line_num;
        defn->name=(char*) malloc(strlen(functionName)+1);
        strcpy(defn->name, functionName);
        defn->currentpoint=sizeof(unsigned char);

        memoryContainer->lineDefns=defn;

        position=appendStatement(memoryContainer, FNCALL_TOKEN, position);
        position+=sizeof(unsigned short);
	}
	position=appendVariable(memoryContainer, numArgs, position);

	for (i=0;i<numArgs;i++) {
		if (isArgIdentifier[i]) {
			position=appendVariable(memoryContainer, varIds[i], position);
		} else {
			sprintf(varname,"%s#%d", functionName, i);
			position=appendVariable(memoryContainer, getVariableId(varname, 0), position);
		}
	}
	clearStack(args);
	free(varname);
	free(isArgIdentifier);
	free(varIds);
	if (currentCall==NULL) {
		mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls]=(char*)malloc(strlen(functionName)+1);
		strcpy(mainCodeCallTree.calledFunctions[mainCodeCallTree.number_of_calls++], functionName);
	} else {
		currentCall->calledFunctions[currentCall->number_of_calls]=(char*)malloc(strlen(functionName)+1);
		strcpy(currentCall->calledFunctions[currentCall->number_of_calls++], functionName);
	}
	if (assignmentContainer != NULL) {
		return concatenateMemory(assignmentContainer, memoryContainer);
	} else {
		return memoryContainer;
	}

}