Exemplo n.º 1
0
/* CW_WriteCode:  translates current parser command cmd into asm and writes it to file fp */
char *CW_WriteCode(Parser_command *cmd, char *output, FILE *fp){
	*output = '\0';
	switch(cmd->commandType){
	case C_PUSH:
	case C_POP:
		WritePushPop(cmd, output);
		break;
	case C_ARITHMETIC:
		WriteArithmetic(cmd, output);
		break;
	default:
		break;
	}
	fputs(output, fp);
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{

  FILE *inP, *outP; // input and output file
  Command *vmCommands; // all VM Commands
  int count=0; // count of instructions
  int i; // loop index
  Command *currentCommand = NULL;
  
  if(argc != 2) {
    perror("Usage : VMTranslator <filename>");
    return -1;
  }
  
  // make surce the file extension is .vm
  if(!CheckFileExtension(argv[1])) {
    perror("File extension must be .vm");
    return -1;  
  }
  
  vmCommands = (Command*)malloc(sizeof(Command) * MAX_COMMAND_NUM);

  // open the input file
  inP = fopen(argv[1], "r");
  if(!inP) {
    perror("File open failed!");
    goto failed;
  }
  
  count = Parse(inP, &vmCommands);
  //Show(&vmCommands, count);
  fclose(inP);

  // create the output file
  outP = SetFileName(argv[1]);
  if(!outP) {
    perror("File open failed!");
    goto failed;
  }


  for( i = 0; i < count; i++) {
    currentCommand = vmCommands + i;
    switch(currentCommand->commandType){
      case C_ARITHMETIC:
	       WriteArithmetic(outP, currentCommand->arg1);
           break;
      case C_PUSH:
      case C_POP:
           WritePushPop(outP, currentCommand->commandType, currentCommand->arg1, currentCommand->arg2);
           break;
      default:
           break;
	}
  }

  Close(outP);

failed:
  free(vmCommands);

  return 0;
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
  struct dirent *pDirent;
  DIR *pDir;
  char *dirName;
  char inputFilename[128];

  FILE *inP, *outP; // input and output file
  Command *vmCommands; // all VM Commands
  int count=0; // count of instructions
  int i; // loop index
  Command *currentCommand = NULL;
  
  if(argc != 2) {
    printf("Usage : VMTranslator <directory>");
    return -1;
  }
  
  dirName = argv[1];

  // open directory  
  pDir = opendir (dirName);
  if(pDir == NULL) {
    printf("Can not open directory '%s'\n", dirName);
  }

  vmCommands = (Command*)malloc(sizeof(Command) * MAX_COMMAND_NUM);

  // create the output file
  outP = Open(dirName, basename(dirName));
  if(!outP) {
    printf("File open failed!\n");
    goto failed;
  }
  WriteInit(outP);

  
  while ((pDirent = readdir(pDir)) != NULL) {
    // only open the *.vm files in directory
    if( CheckFileExtension(pDirent->d_name) ) {
        sprintf(inputFilename, "%s/%s", dirName, pDirent->d_name);
        // open the input file
        inP = fopen(inputFilename, "r");
        if(!inP) {
          printf("File '%s' open failed!\n", inputFilename);
          continue;
        }

        SetFileName(basename(inputFilename));

        memset(vmCommands, 0, MAX_COMMAND_NUM);

		count = Parse(inP, &vmCommands);
        for( i = 0; i < count; i++) {
           currentCommand = vmCommands + i;
           switch(currentCommand->commandType){
              case C_ARITHMETIC:
	               WriteArithmetic(outP, currentCommand->arg1);
                   break;
              case C_LABEL:
                   WriteLabel(outP, currentCommand->arg1);
                   break;
              case C_GOTO:
                   WriteGoTo(outP, currentCommand->arg1);
                   break;
              case C_IF:
                   WriteIf(outP, currentCommand->arg1);
                   break;
              case C_PUSH:
              case C_POP:
                   WritePushPop(outP, currentCommand->commandType, currentCommand->arg1, currentCommand->arg2);
                   break;
              case C_FUNCTION:
                   WriteFunction(outP, currentCommand->arg1, currentCommand->arg2);
                   break;
              case C_CALL:
                   WriteCall(outP, currentCommand->arg1, currentCommand->arg2);
                   break;
              case C_RETURN:
                   WriteReturn(outP);
                   break;
              default:
                   break;
	        }
        }
        fclose(inP);
    }
  }
  closedir (pDir);
  Close(outP);

failed:
  free(vmCommands);

  return 0;
}