void aplicaAcoesSemanticas(FILE* arquivo, int acao, char** info, char* nomeArquivo, int* flag){

	switch(acao){
		case 1: //Cria o arquivo intermediario
			strcpy(nomeArquivo, info[2]);
			strcat(nomeArquivo, ".c");
			arquivo = fopen(nomeArquivo, "w");
			fprintf(arquivo, "#include <stdio.h>\n");
			fprintf(arquivo, "#include <stdlib.h>\n");
			fprintf(arquivo, "#include <string.h>\n\n\n");
			fprintf(arquivo, "int main(void){\n\n");
			fclose(arquivo);
			break;
		
		case 2: //Se
			*flag = 1;
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo,"	if(");
			fclose(arquivo);
			break;
		case 3: //VAIDEN dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "%s ", info[2]);
			fclose(arquivo);
			break;
		case 4: //NUMB dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			if(*flag == 1){
				fprintf(arquivo, "%s ", info[2]);
			}else{
				fprintf(arquivo, "%s", info[2]);
			}
			fclose(arquivo);
			break;
		case 5: // ( dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "(");
			fclose(arquivo);
			break;
		case 6: // ) dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ")");
			fclose(arquivo);
			break;
		case 7: // not dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "!");
			fclose(arquivo);
			break;
		case 8: // > dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ">");
			fclose(arquivo);
			break;
		case 9: // = dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "=");
			fclose(arquivo);
			break;
		case 10: // < dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "<");
			fclose(arquivo);
			break;
		case 11: // <> dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "!=");
			fclose(arquivo);
			break;
		case 12: // >= dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ">=");
			fclose(arquivo);
			break;
		case 13: // <= dentro do SE
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "<=");
			fclose(arquivo);
			break;
		case 14: // entao
			*flag = 0;
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ")");
			fclose(arquivo);
			break;
			
		case 15: //VAIDEN statm
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "	%s ", info[2]);
			fclose(arquivo);
			break;
		case 16: // := statm
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "=");
			fclose(arquivo);
			break;
		case 17: //senao
			*flag = 2;
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "else");
			fclose(arquivo);
			break;
		case 18: //lambda statm
			if(*flag>0){
				arquivo = fopen(nomeArquivo, "a");
				//fprintf(arquivo, "}\n");
				fclose(arquivo);			
			}
			break;	
		case 19: // Enquanto
			*flag = 1;
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "	while(");
			fclose(arquivo);
			break;
		case 20: //faca
			*flag = 0;
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ")\n");
			fclose(arquivo);
			break;
		case 21: //sinal de +
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "+");
			fclose(arquivo);
			break;
		case 22: //sinal de -
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "-");
			fclose(arquivo);
			break;	
		case 23: //sinal de *
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "*");
			fclose(arquivo);
			break;	
		case 24: //sinal de /
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, "/");
			fclose(arquivo);
			break;
		case 25: //inicio abre colchete
		    arquivo = fopen(nomeArquivo, "a");
		    fprintf(arquivo,"{\n");
		    fclose(arquivo);
		    break;
		case 26: //fim fecha colchete
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo,"}\n");
			fclose(arquivo);
			break;
		case 27: //Procedimento PRIDEN
			arquivo = fopen(nomeArquivo, "a");
			if(strcmp("leiacad",info[2])==0){
				fprintf(arquivo,"scanf(\"%%s\",");
			}
			else if(strcmp("leiareal", info[2])==0){
				fprintf(arquivo,"scanf(\"%%f\",&");
			}
			else if(strcmp("leiaint", info[2])==0){
				fprintf(arquivo,"scanf(\"%%d\",&");
			}
			else if(strcmp("imprimacad", info[2])==0){
				fprintf(arquivo, "printf(\"%%s\",");
			}
			else if(strcmp("imprimanum", info[2])==0){
				fprintf(arquivo, "printf(\"%%f\",");
			}
			else if(strcmp("saltelinha", info[2])==0){
				fprintf(arquivo, "printf(\"\\n\")");
			}
			fclose(arquivo);
			break;
		case 28:
			arquivo = fopen(nomeArquivo, "a");
			//fprintf(arquivo,"%s", info[2]);
			fclose(arquivo);
			break;
		case 29:
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo,");");
			fclose(arquivo);
			break;
		case 30:
			arquivo = fopen(nomeArquivo, "a");
			fprintf(arquivo, ";\n");
			fclose(arquivo);
			break;
		case 31:
			arquivo = fopen(nomeArquivo, "a");
			int i;
			fprintf(arquivo,"\"");
			for(i=1;i<strlen(info[2])-1;i++){
				fprintf(arquivo, "%c", info[2][i]);
			}
			fprintf(arquivo,"\"");
			fclose(arquivo);
			break;
		case 32:
			arquivo = fopen(nomeArquivo, "a");
			char array[30];
			strcpy(array,"[");
			strcat(array, info[2]);
			strcat(array,"]\0"); 
			lista_alterarInfo(variaveisImprimir, array);
			break;		
		case 60: //Cria variavel
			variaveis = lista_inserir(variaveis,info[2]);
			variaveisImprimir = lista_inserir(variaveisImprimir, info[2]);
			break;
		case 69: //Escreve as variaveis no arquivo
			arquivo = fopen(nomeArquivo,"a");
			if(strcmp(info[2],"cadeia")!=0){
				if(strcmp(info[2],"inteiro")==0){
					imprimeVariaveis(variaveisImprimir, "int", arquivo);
				}else if(strcmp(info[2],"caracter")==0){
				imprimeVariaveis(variaveisImprimir, "char", arquivo);
				}	
				variaveisImprimir = NULL;
			}
			fclose(arquivo);
			break;
			
	}

}
Beispiel #2
0
int main(int argc, char **argv)
{
	struct gengetopt_args_info args_info;

	char outputDir[PATH_MAX] = ""; // the output directory where files are to be saved
	char filename[PATH_MAX] = ""; // the name of the files (without extension) 
	char capitalFilename[PATH_MAX] = ""; // the name of the files (without extension) in capital letters
	char fullPath[PATH_MAX] = ""; // the full path of the file
	char fileType[4] = ""; // the extension of the file

	char mainTemplateName[PATH_MAX] = ""; // the path to the main template file (.c or .cu)
	char headerTemplateName[PATH_MAX] = ""; // the path to the header template file (.h)
	char makefileTemplateName[PATH_MAX] = ""; // the path to the makefile template file

	char fileVarMainTemplateName[PATH_MAX] = "";// the path to the main template variables file 
	char fileVarHeaderTemplateName[PATH_MAX] = ""; // the path to the header template variables file
	char fileVarMakefileTemplateName[PATH_MAX] = ""; // the path to the makefile template variables file
	char handleErrorTemplateName[PATH_MAX] = ""; // the path to the HandleError.h
	

	HASHTABLE_T *systemVarsTable; // an hashtable containing program genereted variables
	HASHTABLE_T *fileVarsTable;
	LISTA_GENERICA_T *varsIgnoreList; // an hashtable containing file fetched variables	
	
	char userName[PATH_MAX] = "your name"; // the name of the user
	char *fileVars = NULL; // a string with the vars fetched from a variables file


	char *template; // a string with the content of a template file
	unsigned int i = 0; // a utility index
	char *currentDate = NULL; // a string representing the current date
		
	struct passwd *passwd;
	passwd=getpwuid(getuid());
	char * homedir = passwd->pw_dir;
	if(homedir[strlen(homedir) - 1] == '/'){
		homedir[strlen(homedir) - 1] = 0;
	}
	
	
	// parse input parameters
	if (cmdline_parser(argc, argv, &args_info) != 0)
		exit(1);
	
	currentDate = getDateTime();

	//creates an hashtable with system generated template variables
	systemVarsTable = tabela_criar(11, (LIBERTAR_FUNC) free);

	
	varsIgnoreList = lista_criar((LIBERTAR_FUNC) free);
	
	tabela_inserir(systemVarsTable, "$!C_DATE!$", string_clone(currentDate));	
	tabela_inserir(systemVarsTable, "$!USER_NAME!$", string_clone(userName));

	
	// --about
	if (args_info.about_given) {
		return 0;
	} 
	
	// --student
	
	for(i = 0; i < args_info.student_given; i ++){
		lista_inserir(varsIgnoreList, string_clone(args_info.student_arg[i]));
	}
	
	// --proto
	if (args_info.proto_given) {
		tabela_inserir(systemVarsTable, "$!KERNEL_PROTO!$", string_clone(args_info.proto_arg));	
	} else {
		tabela_inserir(systemVarsTable, "$!KERNEL_PROTO!$", string_clone(""));	
	}
	
	// --kernel
	if (args_info.kernel_given) {
		tabela_inserir(systemVarsTable, "$!KERNEL_NAME!$", string_clone(args_info.kernel_arg));	
	} else {
		tabela_inserir(systemVarsTable, "$!KERNEL_NAME!$", string_clone("Kernel"));	
	}
	
	// --blocks
	store_grid_geometry(systemVarsTable, &args_info);
	// --threads
	store_blocks_geometry(systemVarsTable, &args_info);
	
	// --dir
	// get filename from path (the name of the last directory)
	getFilenameFromPath(args_info.dir_arg, filename);

	// the filename in capital letters
	for (i = 0; i < strlen(filename); i++) {
		capitalFilename[i] = toupper(filename[i]);
	}
	capitalFilename[i] = 0;
	
	tabela_inserir(systemVarsTable, "$!FILENAME!$", string_clone(filename));
	tabela_inserir(systemVarsTable, "$!CAPITAL_FILENAME!$", string_clone(capitalFilename));
	

	// removes the / character
	if (args_info.dir_arg[strlen(args_info.dir_arg) - 1] == '/') {
		args_info.dir_arg[strlen(args_info.dir_arg) - 1] = 0;
	}
	//creates the output directory
	if (!createDirectory(args_info.dir_arg)) {

		if (args_info.Force_given) {
			// removes the existing directoy        
			remove_directory(args_info.dir_arg);
			sprintf(outputDir, "%s", args_info.dir_arg);
		} else {
			// adds a date to the directory name
			sprintf(outputDir, "%s%s", args_info.dir_arg,
				currentDate);
		}

		createDirectory(outputDir);
	} else {
		sprintf(outputDir, "%s", args_info.dir_arg);
	}

	sprintf(outputDir, "%s/", outputDir);


	if (!args_info.measure_given) {
		char *var = malloc(strlen("MEASURE") + 1);
		strcpy(var, "MEASURE");
		lista_inserir(varsIgnoreList, var);

	}
	

	/* defines which templates to use */

	// cuda with prototype
	if (args_info.proto_given) {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, CU_PROTO_TEMPLATE);

		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, CU_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, CU_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, CU_PROTO_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, CU_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, CU_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".cu");

		// regular c template   
	} else if (args_info.regular_code_given) {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, C_MAIN_TEMPLATE);
		
		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, C_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, C_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, C_MAIN_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, C_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, C_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".c");

		// cuda default
	} else {
		strcpy(mainTemplateName, homedir);
		strcat(mainTemplateName, CU_MAIN_TEMPLATE);

		strcpy(headerTemplateName, homedir);
		strcat(headerTemplateName, CU_HEADER_TEMPLATE);

		strcpy(makefileTemplateName, homedir);
		strcat(makefileTemplateName, CU_MAKEFILE_TEMPLATE);

		strcpy(fileVarMainTemplateName, homedir);
		strcat(fileVarMainTemplateName, CU_MAIN_TEMPLATE_VARS);

		strcpy(fileVarHeaderTemplateName, homedir);
		strcat(fileVarHeaderTemplateName, CU_HEADER_TEMPLATE_VARS);

		strcpy(fileVarMakefileTemplateName, homedir);
		strcat(fileVarMakefileTemplateName, CU_MAKEFILE_TEMPLATE_VARS);

		strcat(fileType, ".cu");

	}
	
	// create HANDLE_ERROR_H file
	if (strcmp(fileType, ".cu") == 0) {
		// reads from source file
		strcpy(handleErrorTemplateName, homedir);
		strcat(handleErrorTemplateName, HANDLE_ERROR_H);