Exemple #1
0
int 
main(int argc, char *argv[], char *envp[]) {

  string ccCmd;
  int optionsOK = parseOptions(argc, argv);
  if (optionsOK < 0)
	return -1;

  cppShellCmd += inputFile;
  cppShellCmd += " ";

  /* ccCmd = "mv ";
  ccCmd += outputFile;
  ccCmd += " ";
  ccCmd += outputFile;
  ccCmd += ".c; ";
  ccCmd += ccShellCmd;
  if (genSharedLib)
	ccCmd += "-shared ";
  ccCmd += "-o ";
  ccCmd += outputFile;*/

  if ((yyin = popen(cppShellCmd.c_str(), "r")) == NULL) {
    cerr << "Unexpected error in reading input file\n";
    return 1;
  }

#ifdef TEST_LEXER
  int token;

  while (token = yylex()) {
	if (token == TOK_SYNTAX_ERROR) {
	  cout << yyfilename << ":" << yylinenum 
		   << ": Error: Unrecognized token `" << yytext << "'\n";
	}
	else {
      cout << "Token: " << token << " ";
      switch(token)
        {
        case TOK_UINTNUM:
          cout << "Attribute: (int) " << yylval.uVal;
          break;

        case TOK_DOUBLENUM:
          cout << "Attribute: (float) " << yylval.dVal;
          break;

        case TOK_STRCONST:
          cout << "Attribute: (string) " << yylval.cVal;
          break;

        case TOK_ID:
          cout << "Attribute: (id) " << yylval.cVal;
          break;

        default:
          break;
        }
      cout << endl;
	}
  }

  return 0;
#else
  //   yydebug = 1;
  any = new EventEntry("any");
  if (stm.insert(any) == OK) {
    Type *te = new Type((vector<Type*>*)NULL, Type::EVENT);
    any->type(te);
  }
  yyparse();
  stm.leaveToScope(SymTabEntry::Kind::GLOBAL_KIND);
  GlobalEntry *ge = (GlobalEntry*)(stm.currentScope());
  if (ge != NULL) {
	cout << "Finished parsing, here is the AST\n";
  
  if(errCount() == 0){
    cout << "Type checking" << endl;
    ge->typeCheck();
    //ge->typePrint(cout,0);
  }
  if(errCount() == 0){
    cout << "Memory alloaction" << endl;
    MemoryMgr mm = MemoryMgr();
    ge->memAlloc(mm);
  }

  if(errCount() == 0){
    cout << "Code Generation" << endl;

    CodeBlock* prog = ge->codeGen();
    prog->Optimize();
    if(outputFile.size() == 0){
      cout << endl << endl;
      prog->print(cout);  
      cout << endl << endl;
    } else {
      ofstream ofs(outputFile,ofstream::out);
      prog->print(ofs);
    }
  }

  }
#endif
}
int
main(int argc, char *argv[], char *envp[]) {

    string ccCmd;
    int optionsOK = parseOptions(argc, argv);
    if (optionsOK < 0)
        return -1;

    cppShellCmd += inputFile;
    cppShellCmd += " ";

    /* ccCmd = "mv ";
    ccCmd += outputFile;
    ccCmd += " ";
    ccCmd += outputFile;
    ccCmd += ".c; ";
    ccCmd += ccShellCmd;
    if (genSharedLib)
    ccCmd += "-shared ";
    ccCmd += "-o ";
    ccCmd += outputFile;*/

    if ((yyin = popen(cppShellCmd.c_str(), "r")) == NULL) {
        cerr << "Unexpected error in reading input file\n";
        return 1;
    }

#ifdef TEST_LEXER
    int token;

    while (token = yylex()) {
        if (token == TOK_SYNTAX_ERROR) {
            cout << yyfilename << ":" << yylinenum
                 << ": Error: Unrecognized token `" << yytext << "'\n";
        }
        else {
            cout << "Token: " << token << " ";
            switch(token)
            {
            case TOK_UINTNUM:
                cout << "Attribute: (int) " << yylval.uVal;
                break;

            case TOK_DOUBLENUM:
                cout << "Attribute: (float) " << yylval.dVal;
                break;

            case TOK_STRCONST:
                cout << "Attribute: (string) " << yylval.cVal;
                break;

            case TOK_ID:
                cout << "Attribute: (id) " << yylval.cVal;
                break;

            default:
                break;
            }
            cout << endl;
        }
    }

    return 0;
#else
    //   yydebug = 1;
    any = new EventEntry("any");
    if (stm.insert(any) == OK) {
        Type *te = new Type((vector<Type*>*)NULL, Type::EVENT);
        any->type(te);
    }
    yyparse();
    stm.leaveToScope(SymTabEntry::Kind::GLOBAL_KIND);
    GlobalEntry *ge = (GlobalEntry*)(stm.currentScope());

    if (ge != NULL) {
        //cout << "Finished parsing, here is the AST\n";
        ge->checkType();
	ge->print(cout, 0);
	string fileName = string(yyfilename);
	int dotIndex = fileName.find('.');
	if(dotIndex > -1)
	    fileName = fileName.substr(0, dotIndex);
	if (errCount() == 0) {
	    qos.open(fileName + ".q");
	    ge->genFinalCode("test");
	    ofstream fos(fileName + ".i");
	    ge->serializeAsm(fos);
	    fos.close();
	    qos.close();
	}
    }
#endif
}