Exemplo n.º 1
0
  /**
   * Start one module after the other and try to compile the input file. Then generate the assembler
   * file and create this file for jasmin.
   */
  private static void parseCompile(String input, String fileName)
      throws ParserException, LexerException, IOException {
    String output; // Init output string
    StringReader reader =
        new StringReader(input); // Standard routine to start the parser, lexer, ...
    PushbackReader r = new PushbackReader(reader, 100);
    Lexer l = new Lexer(r);
    Parser parser = new Parser(l);
    Start start = parser.parse();

    //        ASTPrinter printer = new ASTPrinter();
    //        start.apply(printer);

    TypeChecker typeChecker = new TypeChecker(); // Starting TypeChecker
    start.apply(typeChecker);

    CodeGenerator codeGenerator = new CodeGenerator(typeChecker.getSymbolTable());
    copySymbolTable(
        typeChecker, codeGenerator); // To get all the identifiers copied with an index to CodeGen
    start.apply(codeGenerator);

    output = createOutput(codeGenerator, fileName);

    Writer wout =
        new BufferedWriter( // Write everything to the outputfile.j
            new OutputStreamWriter(new FileOutputStream(fileName + ".j"), "UTF8"));
    wout.append(output);
    wout.close();
  }
Exemplo n.º 2
0
 /**
  * Copy SymbolTable from the TypeChecker to the CodeGenerator and give it a new index (not
  * necessary to know the types any more - they are all correct!)
  */
 private static void copySymbolTable(TypeChecker typeChecker, CodeGenerator codeGenerator) {
   int i = typeChecker.getSymbolTable().size();
   for (String id : typeChecker.getSymbolTable().keySet())
     codeGenerator.getSymbolTable().put(id, i--);
 }