1 / 15

Speed part 3

Speed part 3. Barb Ericson Georgia Institute of Technology May 2006. Learning Goals. Computing Concepts Write a compiler for a simple graphical language Extend the compiler Understand why compiled code is faster than interpreted code. Writing a Compiler.

stephanyr
Download Presentation

Speed part 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Speedpart 3 Barb Ericson Georgia Institute of Technology May 2006 Georgia Institute of Technology

  2. Learning Goals • Computing Concepts • Write a compiler for a simple graphical language • Extend the compiler • Understand why compiled code is faster than interpreted code Georgia Institute of Technology

  3. Writing a Compiler • To write a graphics compiler we need to • Write the code for a java class that draws the picture for the given graphics commands • We need to start the class definition including starting the main method – the prologue • Then we need to open the input file and output a line per command in the main • Then we need to finish the main method and class definition – the epilogue Georgia Institute of Technology

  4. Creating a Graphics Compiler import java.io.*; /** * Class that reads in a file of graphics instructions, and * then generates a NEW Java Program that * does the same thing as the instructions. Default picture size is 640x480. * * Format of the file is a bunch of lines of the form: * Command X Y <parameters> * Commands are "line" with parameters of end X,Y and * "circle" with a parameter of diameter. * * For example: * line 10 10 50 70 * circle 10 20 30 * * Which draws a line from (10,10) to (50,70) and a circle at (10,20) * with a diameter of 30. * * @author Barb Ericson * @author Mark Guzdial */ Georgia Institute of Technology

  5. Start of Class and writePrologue public class GraphicsCompiler { /** Method to write out the prologue for the new program: * All the imports, the class definition, main, etc. * @param file BufferedWriter to write the prologue to **/ public void writePrologue(BufferedWriter file){ try { // Write out the prologue lines file.write("import java.awt.*;"); file.newLine(); file.write("public class GeneratedDrawing{"); file.newLine(); file.write(" public static void main(String args[]){"); file.newLine(); file.write(" Picture frame = new Picture(640,480);"); file.newLine(); file.write(" Graphics g = frame.getGraphics();"); file.newLine(); file.write(" g.setColor(Color.black);"); file.newLine();} catch (Exception ex) { System.out.println("Error during write of prologue"); } } Georgia Institute of Technology

  6. writeEpilogue Method /** Method to write out the epilogue for the new program: * Show the picture. Close the main and the class. * @param file BufferedWriter to write the epilogue to **/ public void writeEpilogue(BufferedWriter file){ try { // Write out the epilogue lines file.write(" frame.show();"); file.newLine(); file.write(" } // end main()"); file.newLine(); file.write("} // end class"); file.newLine();} catch (Exception ex) { System.out.println("Error during write of epilogue"); } } Georgia Institute of Technology

  7. Start of CompileCommands Method /** * Method to compile the commands in the given file */ public void compileCommands(String fileName) { String line = null; String [] params = null; int x1, y1, x2, y2, diameter; // try the following try { // read from the file BufferedReader reader = new BufferedReader(new FileReader(fileName)); BufferedWriter writer = new BufferedWriter(new FileWriter( FileChooser.getMediaPath("GeneratedDrawing.java"))); writePrologue(writer); Georgia Institute of Technology

  8. Loop and handle line command // loop till end of file while ((line = reader.readLine()) != null) { // what command is this? if (line.startsWith("line")) { // Get the parameters for drawing the line params = line.split(" "); // params[0] should be "line" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); x2 = Integer.parseInt(params[3]); y2 = Integer.parseInt(params[4]); // Now, write the line that will LATER // draw the line writer.write(" g.drawLine("+x1+","+y1+","+x2+","+y2+");"); writer.newLine(); } Georgia Institute of Technology

  9. Handle circle command else if (line.startsWith("circle")) { // Get the parameters for drawing the circle params = line.split(" "); // params[0] should be "circle" x1 = Integer.parseInt(params[1]); y1 = Integer.parseInt(params[2]); diameter = Integer.parseInt(params[3]); // Now, draw the circle in writer.write(" g.drawOval("+x1+","+y1+","+diameter+","+ diameter+");"); writer.newLine(); } Georgia Institute of Technology

  10. Finish compileCommand Method else { System.out.println("Uh-oh! Invalid command! "+line); return;} } writeEpilogue(writer); writer.close(); } catch (FileNotFoundException ex) { System.out.println("Couldn't find file " + fileName); fileName = FileChooser.pickAFile(); compileCommands(fileName); } catch (Exception ex) { System.out.println("Error during read or write"); ex.printStackTrace(); } } Georgia Institute of Technology

  11. Main for Testing public static void main(String[] args) { GraphicsCompiler compiler = new GraphicsCompiler(); String fileName = FileChooser.getMediaPath( "graphics-commands.txt"); compiler.compileCommands(fileName); } } // end class Georgia Institute of Technology

  12. How it Works • Open the same file for input as the interpreter • But don't create a blank picture to write to • Instead write to GeneratedDrawing.java • Write out a class definition • And a main that creates the blank picture, gets the graphics context on that picture, and does the drawing. • Compile GeneratedDrawing and execute it to see the picture Georgia Institute of Technology

  13. Exercise • Add the code to handle triangles to the GraphicsCompiler class • triangle x1 y1 x2 y2 x3 y3 • It will draw a triangle with points at (x1,y1), (x2,y2), and (x3, y3) Georgia Institute of Technology

  14. Compilers • Compilers still have to interpret the language they are compiling • But, they only do this one time • When you compile • Then you can execute the compiled code many times • Applications like Word or Photoshop are written in C or C++ and then compiled to machine language programs • The generated machine language program is faster than an interpreted one Georgia Institute of Technology

  15. Summary • Compilers read a language and output the required code in another language • Like from our graphical language to Java commands • Or from C to machine language • You compile code once to turn it into the desired language • You can run the compiled code many times • Running compiled code is faster than running code that must be interpreted Georgia Institute of Technology

More Related