예제 #1
0
void SinglePass(char* filename,char* symboldef, char* opcode_mnemonic, char* operand, unsigned int* opcode){

	fp=fopen(filename,"r");

	if(fp == NULL){
		printf("해당 파일을 읽을 수 없습니다.\n");
		return;
	}

	while(!feof(fp) && !isDone){
		TokenParser(symboldef,opcode_mnemonic,operand);
		if(letThisLinePass == 0){
			Decoder(symboldef,opcode_mnemonic,operand,opcode);
			Translator(symboldef,opcode_mnemonic,operand,opcode);
		}
		//Initalize control signals before next line
		statusBit = 0;
		whereIsOnOPTAB = -1;
		whereIsOnASMTAB = -1;
	}
	if(whichPass == 1){
		DeployLiteral(&PC);
	}
	if(whichPass == 2){
		PrintModificationRecord(); //Print modification record
		ht_print(SYMTAB,"SYMTAB");
	}

	fclose(fp);
	letThisLinePass=0;
	isDone = 0;
	whereIsOnOPTAB =0;
	whereIsOnASMTAB =0;

}
예제 #2
0
파일: main.cpp 프로젝트: slak44/test-lang
int main(int argc, const char* argv[]) {
  try {
    TCLAP::CmdLine cmd("Xylene", ' ', "pre-release");
    
    TCLAP::SwitchArg asXML("", "xml", "Read file using the XML parser", cmd);
    
    TCLAP::SwitchArg printTokens("", "tokens", "Print token list", cmd);
    TCLAP::SwitchArg printAST("", "ast", "Print AST (if applicable)", cmd);
    TCLAP::SwitchArg printIR("", "ir", "Print LLVM IR (if applicable)", cmd);
    
    TCLAP::SwitchArg doNotParse("", "no-parse", "Don't parse the token list", cmd);
    TCLAP::SwitchArg doNotRun("", "no-run", "Don't execute the AST", cmd);
    
    std::vector<std::string> runnerValues {"llvm-lli", "llvm-llc"};
    TCLAP::ValuesConstraint<std::string> runnerConstraint(runnerValues);
    TCLAP::ValueArg<std::string> runner("r", "runner", "How to run this code", false,
      "llvm-lli", &runnerConstraint, cmd, nullptr);
    
    TCLAP::ValueArg<std::string> code("e", "eval", "Code to evaluate", false,
      std::string(), "string", cmd, nullptr);
    TCLAP::ValueArg<std::string> filePath("f", "file", "Load code from this file",
      false, std::string(), "path", cmd, nullptr);
    cmd.parse(argc, argv);
    
    if (code.getValue().empty() && filePath.getValue().empty()) {
      TCLAP::ArgException arg("Must specify either option -e or -f");
      cmd.getOutput()->failure(cmd, arg);
    }
    
    std::unique_ptr<AST> ast;
    
    if (asXML.getValue()) {
      if (doNotParse.getValue()) return NORMAL_EXIT;
      if (filePath.getValue().empty()) {
        TCLAP::ArgException arg("XML option only works with files");
        cmd.getOutput()->failure(cmd, arg);
      }
      ast = std::make_unique<AST>(XMLParser().parse(rapidxml::file<>(filePath.getValue().c_str())).getTree());
    } else {
      std::string input;
      if (!filePath.getValue().empty()) {
        std::ifstream file(filePath.getValue());
        std::stringstream buffer;
        buffer << file.rdbuf();
        input = buffer.str();
      } else {
        input = code.getValue();
      }
      
      auto lx = Lexer();
      lx.tokenize(input, filePath.getValue().empty() ? "<cli-eval>" : filePath.getValue());
      if (printTokens.getValue()) for (auto tok : lx.getTokens()) println(tok);
      
      if (doNotParse.getValue()) return NORMAL_EXIT;
      ast = std::make_unique<AST>(TokenParser().parse(lx.getTokens()).getTree());
    }
    
    if (printAST.getValue()) ast->print();
    if (doNotRun.getValue()) return NORMAL_EXIT;
    
    CompileVisitor::Link v = CompileVisitor::create("Command Line Module", *ast);
    v->visit();
    if (printIR.getValue()) v->getModule()->dump();
        
    if (runner.getValue() == "llvm-lli") {
      return Runner(v).run();
    } else if (runner.getValue() == "llvm-llc") {
      println("Not yet implemented!");
      // TODO
    }
  } catch (const TCLAP::ExitException& arg) {
    return CLI_ERROR;
  } catch (const TCLAP::ArgException& arg) {
    println("TCLAP error", arg.error(), "for", arg.argId());
    return TCLAP_ERROR;
  } catch (const Error& err) {
    println(err.what());
    return USER_PROGRAM_ERROR;
  }
  // If we're debugging, crash the program on InternalError
  #ifndef CRASH_ON_INTERNAL_ERROR
  catch (const InternalError& err) {
    println(err.what());
    return INTERNAL_ERROR;
  }
  #endif
  return 0;
}