Exemplo n.º 1
0
int parser() {
	printf("sega");
	initAlloc(); 
	BSTInit(&TempTree);
	Tape = allocate(sizeof(tTape));
	paramlist = allocate(sizeof(tParamList));
	initFunList();    
    IniTape();
	if(insertFunListItemEmbed("length") != E_OK) return E_INTERN;
    if(insertFunListItemEmbed("find") != E_OK) return E_INTERN;
    if(insertFunListItemEmbed("copy") != E_OK) return E_INTERN;
    if(insertFunListItemEmbed("sort") != E_OK) return E_INTERN;
	error = program();
		printf("eror je %d \n",error);
	 if (error != E_OK)
        return error;
	//free(TempVar);

	error = printTape(Tape);
//	BSTDispose(&TempTreeL);
//	BSTDispose(&TempTree);
//	printf("eror je %d \n",error);
//	printf("eror je %d \n",error);
//freeAlloc(); 
	return error;
}
Exemplo n.º 2
0
//Read in from the user and when they hit enter, if there is no mismatches in braces
//accept the input and output the BF results. If there is a mismatch, let them know
//and don't accept the input.
void interpreter(BFState * interp){
	puts("Welcome to the BrainFuck Interpreter.\n You can find the full 8 commands of the language on wikipedia.\nPress q to exit.\n\n");
	
	char quitChar = 'q';
	//ASCII enter = 13
	//int accept = 13;
	//Accepting 2000 characters seems like plenty
	int size = 2000;
	char * buffer = (char *) malloc(size);
	char incomingChar = ' ';
	int curChar=0;
	int braceCheck = 0;
    int print = 0;
	printf(">");

	while(incomingChar != quitChar){
		curChar=0;
		while((incomingChar =getchar()) != '\n' && incomingChar != quitChar){
			buffer[curChar] = incomingChar;
			if(incomingChar == '['){
				braceCheck++;
			}else if(incomingChar == ']'){
				braceCheck--;
			}
			else if (incomingChar == 'd'){
                print = 1;
            }
            curChar++;
			if(curChar > size-1){
				//We ran out of room...
				puts("Command too long! Please break your statements up or use a file");
				incomingChar = quitChar;
				continue;
			}
		}
		//Do we have any loose braces?
		if(braceCheck == 0){
		    if (print) {
                printTape(interp);
                print = 0;
            }
        	translateBF(interp,buffer);
        }
		else{
			puts("Please close your braces, your BF statement has not been executed.");
		}
		//Clear the buffer.
		memset(buffer,0,size);
		printf("\n>");
	}
}