예제 #1
0
파일: gopt.c 프로젝트: abbaspour/gopt
/**
 * Invoked when we see a goto in the code
 * Invoked when we see a goto in the code, hence basic block structure have to be changed
 * Note that goto differs from if/label, since it is a terminator, not a beginner of basic-blocks.
 * @param g goto name found(is where goto goes)
 */
void found_goto(char *g)
{
   short found = 1;
   // where goto g goes
   struct block *label_block; 
   struct block *swap_block; 
   // where goto g continues, there should be a label, to aviod unreachable code

   if(!after_goto)
      current_block->end = linenum - 1;

   if((label_block = fetch_block(g)) == NULL)
   {
      found = 0;
      label_block =  (struct block*) malloc(sizeof(struct block));
      label_block->index = lastid++;
      label_block->next = NULL;
      label_block->end = LINE_END;
      label_block->to = BLOCK_NONE;
      label_block->to2 = BLOCK_NONE;
      sprintf(label_block->id,"%s", g);
   }

   current_block->to = label_block->index;
   current_block->top = label_block;

   current_block->to2 = BLOCK_NONE;
   current_block->to2p = NULL;

   // goto differs from if/label, cuz goto is finished not starter
   current_block->end = linenum - 1;

   after_goto = 1;
   
   current_block = gotoend();

   if(!found) {
      current_block->next = label_block;
      current_block = label_block;
      return;
   } 
}
예제 #2
0
파일: gopt.c 프로젝트: abbaspour/gopt
/**
 * Invoked when we see a label in the code
 * Invoked when we see a label in the code, hence basic block structure have to be changed
 * @param l label name found
 */
void found_label(char *l)
{
   struct block *new_block;
   short found = 1;

   // if after goto current is g not here, but this is not the case all times
   // since it can be after goto, but current be here(when g is allocated before) 

   if(!after_goto)
      current_block->end = linenum - 2;

   // we have seen this, or there was a 'goto x' where this label comes after it. 
   if((new_block = fetch_block(l)) == NULL) { // no such a thing
      found = 0;
      new_block =  (struct block*) malloc(sizeof(struct block));
      new_block->index = lastid++;
      new_block->next = NULL;
      new_block->end = LINE_END;
      new_block->to = BLOCK_NONE;
      new_block->to2 = BLOCK_NONE;
      sprintf(new_block->id, "%s", l);
   }

   new_block->begin = linenum - 1;

   if(!after_goto) {
      current_block->to = new_block->index;
      current_block->top = new_block;
   } else {
      after_goto = 0;
   }

   current_block->to2 = BLOCK_NONE;
   current_block->to2p = NULL;

   current_block = gotoend();

   if(!found) {
      current_block->next = new_block;
   }
   current_block = new_block;
}
예제 #3
0
파일: gopt.c 프로젝트: abbaspour/gopt
/**
 * Invoked when we see a if in the code
 * Invoked when we see a if in the code, hence basic block structure have to be changed
 * @param i if name found(is where if goes)
 */
void found_if(char *i)
{
   struct block *cont_block =  (struct block*) malloc(sizeof(struct block)); /* Continued Block (!cond)*/
   struct block *go_block; /* Goto Block(cond) */
   short found = 1;

   if(!after_goto)
      current_block->end = linenum - 1;

   cont_block->begin = linenum;
   cont_block->end = LINE_END;
   cont_block->index = lastid++;
   cont_block->to = BLOCK_NONE;
   cont_block->to2 = BLOCK_NONE;
   sprintf(cont_block->id, "_if_%s", i);

   current_block->to = cont_block->index;
   current_block->top = cont_block;

   if((go_block = fetch_block(i)) == NULL) {
      found = 0;
      go_block =  (struct block*) malloc(sizeof(struct block));
      go_block->index = lastid++;
      go_block->next = NULL;
      go_block->end = LINE_END;
      go_block->to = BLOCK_NONE;
      go_block->to2 = BLOCK_NONE;
      sprintf(go_block->id,"%s", i);
   }

   current_block->to2 = go_block->index;
   current_block->to2p = go_block;

   current_block = gotoend();
   if(!found) {
      current_block->next = go_block;
      current_block = go_block;
   }
   current_block->next = cont_block;
   current_block = cont_block;
}
예제 #4
0
static void
lcmd(int type)
{
	switch(curChar) {
		case '0':
			gotobegin();
			return;
		case '$':
			gotoend();
			return;
		case 'A':
			gotoend();
			putchar(*curPos++);
			lMode = INSERT;
			return;
		case 'a':
			if (curPos != startOfLine)
				putchar(*curPos++);
			lMode = INSERT;
			return;
		case 'i':
			lMode = INSERT;
			return;
		case 'D':
			delete_to_end();
			return;
		case 'd':
			delete_something();
			return;
		case 'c':
			delete_something();
			lMode = INSERT;
			return;
		case 'x':
			ldelete();
			return;
		case ' ':
		case 'l':
			if (curPos < startOfLine+lineLen-1) {
				putchar(*curPos);
				curPos++;
			}
			return;
		case '\b':
		case 'h':
			if (curPos > startOfLine) {
				putchar('\b');
				curPos--;
			}
			return;
		case 'r':
			lMode = EDIT1;
			return;
		case 'R':
			lMode = EDIT;
			return;
	}

	/* The remaining commands should only be processed if we are editing
	 * a command line.  For editing a line in a file, the commands that
	 * relate to command line history are not applicable and should be
	 * blocked.
	 */
	if (type == EDITCMDLINE) {
		switch(curChar) {
			case '/':
				putchar('/');
				historysearch();
				return;
			case '+':
			case 'j':
				shownext();
				return;
			case '-':
			case 'k':
				showprev();
				return;
		}
	}

	/* Beep to indicate an error. */
	putchar(0x07);
}