Speed-Mod18-part3 - Coweb - Georgia Institute of Technology
Download
Report
Transcript Speed-Mod18-part3 - Coweb - Georgia Institute of Technology
Speed
part 3
Barb Ericson
Georgia Institute of Technology
May 2006
Georgia Institute of Technology
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
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
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
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
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
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
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
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
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
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
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
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
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
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