public void callMetaobjects(String filename, Program program, PrintWriter outError) { boolean foundCE = false; boolean foundNCE = false; for (MetaobjectCall moCall : program.getMetaobjectCallList()) { if (moCall.getName().equals("ce")) { this.numSourceFilesWithErros++; String message = (String) moCall.getParamList().get(2); int lineNumber = (Integer) moCall.getParamList().get(0); if (!program.hasCompilationErrors()) { // there was no compilation error. There should be no call // @ce(...) // the source code, through calls to "@ce(...)", informs // that // there are errors String whatToCorrect = ""; if (moCall.getParamList().size() >= 4) { whatToCorrect = (String) moCall.getParamList().get(3); whatToCorrect = " (" + whatToCorrect + ")"; } this.shouldButWereNotList.add( filename + ", " + lineNumber + ", " + message + whatToCorrect); if (foundCE) outError.println( "More than one 'ce' metaobject calls in the same source file '" + filename + "'"); foundCE = true; } else { // there was a compilation error. Check it. int lineOfError = program.getCompilationErrorList().get(0).getLineNumber(); String ceMessage = (String) moCall.getParamList().get(2); String compilerMessage = program.getCompilationErrorList().get(0).getMessage(); if (lineNumber != lineOfError) { String whatToCorrect = ""; if (moCall.getParamList().size() >= 4) { whatToCorrect = (String) moCall.getParamList().get(3); whatToCorrect = "(" + whatToCorrect + ")"; } this.wereButWrongLineList.add( filename + "\n" + " correto: " + lineNumber + ", " + ceMessage + " " + whatToCorrect + "\n" + " sinalizado: " + lineOfError + ", " + compilerMessage); } else { // the compiler is correct. Add to correctList the // message // that the compiler signalled and the message of the // test, given in @ce correctList.add( filename + "\r\n" + "The compiler message was: \"" + compilerMessage + "\"\r\n" + "The 'ce' message is: \"" + ceMessage + "\"\r\n"); } } } else if (moCall.getName().equals("nce")) { if (foundNCE) outError.println( "More than one 'nce' metaobject calls in the same source file '" + filename + "'"); foundNCE = true; if (program.hasCompilationErrors()) { int lineOfError = program.getCompilationErrorList().get(0).getLineNumber(); String message = program.getCompilationErrorList().get(0).getMessage(); this.wereButShouldNotList.add(filename + ", " + lineOfError + ", " + message); } } } if (foundCE && foundNCE) outError.println( "Calls to metaobjects 'ce' and 'nce' in the same source code '" + filename + "'"); }
/** * @param args * @param stream * @param numChRead * @param outError * @param printWriter * @throws IOException */ private void compileProgram(File file, String filename, PrintWriter outError) { Program program; FileReader stream; int numChRead; try { stream = new FileReader(file); } catch (FileNotFoundException e) { String msg = "Something wrong: file does not exist anymore"; outError.println(msg); return; } // one more character for '\0' at the end that will be added by the // compiler char[] input = new char[(int) file.length() + 1]; try { numChRead = stream.read(input, 0, (int) file.length()); if (numChRead != file.length()) { outError.println("Read error"); stream.close(); return; } stream.close(); } catch (IOException e) { String msg = "Error reading file " + filename; outError.println(msg); try { stream.close(); } catch (IOException e1) { } return; } Compiler compiler = new Compiler(); program = null; // the generated code goes to a file and so are the errors program = compiler.compile(input, outError); callMetaobjects(filename, program, outError); if (!program.hasCompilationErrors()) { String outputFileName; int lastIndex; if ((lastIndex = filename.lastIndexOf('.')) == -1) lastIndex = filename.length(); outputFileName = filename.substring(0, lastIndex); if ((lastIndex = filename.lastIndexOf('\\')) != -1) outputFileName = filename.substring(lastIndex + 1); FileOutputStream outputStream; try { outputStream = new FileOutputStream(outputFileName + ".c"); } catch (IOException e) { String msg = "File " + outputFileName + " was not found"; outError.println(msg); return; } PrintWriter printWriter = new PrintWriter(outputStream); PW pw = new PW(); pw.set(printWriter); program.genC(pw); if (printWriter.checkError()) { outError.println("There was an error in the output"); } printWriter.close(); } }