logo
 
Assignment 1 - Fun with PostScript
CptS 355 - Programming Language Design
Washington State University
Home
Calendar
Syllabus
Resources
People
Project turn-in

Overview

Weight: This assignment will count 9% of your final grade.
Due Date: Friday, Sept. 9 (by 11:59PM)
Prerequisite concepts: PostScript built-in operators; definition of a function vs. evaluation of a function;

This assignment is a gentle introduction to PostScript programming.

It is to be your individual work. The problems are simple--you could probably do them in your sleep in C, and the whole assignment is considerably less than 100 lines of code (even being generous with line breaks!). However, remember that you have to do them in an alien language using alien tools and there is little debugging support.

Also, it is very important to test the code that you turn in. Every semester several people lose points because they make what they intend as a simple editorial change at the last minute, turn it in without testing, and it turns out to be broken.

Interpreter

You can download the PostScript interpreter from http://www.cs.wisc.edu/~ghost/. Downloads for both MS Windows and Linux are available.

Follow the Resources link off the course web page to web sites that discuss Postscript programming.

Turning in your work

Important: follow these directions exactly concerning file and directory names, file formats, etc. All code should be developed in a directory named one. Each problem solution should be placed in a separate file within that directory, e.g., one/functions.ps. When you are done and certain that everything is working correctly, create a tarred, gzipped copy of your directory by executing
  tar cf one.tar one; gzip one.tar
or zip the directory on a Windows system. Turn in the tarred, gzipped or zipped directory using the turn-in page. The name of the uploaded file must be one.tar.gz or one.zip. You may turn in your assignment as many times as you like. All submissions will be saved but only the last one you turn in before the deadline will be graded.

Each file should contain only function definition(s): running the program shouldn't do anything. For example, in functions.ps, there should be a defintion of the funnyfact function, but there should not be any uses of it such as

   7 funnyfact
The reason is that we will automatically test your programs by using our own calls, something like the following (on Unix)
   cat functions.ps test.ps | gs

Grading

The assignment will be marked for good programming style (indentation and appropriate comments), as well as correct functioning. Programs that cause errors in execution will receive no credit. Deductions will be made if your program leaves extra values on the stack. (See the specification for each function.) You must use the specified function names and put the functions in files with the specified file names.

functions.ps -- 45%

Put these three functions into the file functions.ps.

(1) Define an operation, ?, that mimics the ? conditional expression operator in C. In C, the conditional expression operator behaves as follows:

   expr1 ? expr2 : expr3
results in value expr2 if expr1 is non-zero and value expr3 otherwise. The operator can be used as follows:
   z = (a > b) ? a : b;  /* this implements z = max(a,b) */
The PostScript ? operator that you define will behave as follows:
   op1 op2 op3 ?  % -- if op1 is a non-zero number leave op2 on the stack
                  % -- if op1 is zero leave op3 on the stack
                  % if op1 is not a number it is an error
                  % that you don't have to explictly check for or handle
So, for example
  2 (ok) (zero) ?         % (ok) is left on the stack
  0 (ok) 3 ?              % 3 is left on the stack
HINT: You may find the roll operator to be useful. ? is said to have three operands and one result. If the stack starts with n+3 elements it must finish with n+1 elements and the n elements below the operands must not be changed in any way.

(2) Define a funnyfact function that implements the following factorial-like function with a twist.

     funnyfact(x) =
           -1                           if x is 0
           x * -funnyfact(x-1)          if x is odd
           x * funnyfact(x-1)           if x is non-zero and even
This operator has one operand and one result so the stack must contain the same number of elements before and after the operation.

Students are often tripped up on this problem. Make sure that you understand what the correct answers are for at least the first 6 integers (by evaluating the above expression by hand) before you start programming. This function has one operand and one result. The implementation of funnyfact must be done as a recursive function, not a loop.

(3) Also define the fibonacci-like function:

     funnyfib(x) =
           x+1                                 if x < 2
           funnyfib(x-1) - funnyfib(x-2)       otherwise
This function has one operand and one result and is defined on all non-negative integers. The stack should contain the same number of elements before and after the operation. Again, make sure that you know the right answers by evaluating the definition by hand before you write the program.

squares.ps -- 20%

This program should go into the file squares.ps. Write a PostScript definition for a function, squares, that takes an operand from the stack (call it n) and produces a line of n alternating light and dark squares. You should choose the size of the squares so that they are big enough to see easily (by 50+ year-old eyes) and at least 10 squares fit across the page. I don't care about the precise gray levels as long as the individual squares are identifiable. For example 4 squares would look like
pretty picture
and 8 squares would look like
pretty picture
The line of squares should be drawn with the top left of the first square at the currentpoint. That is, do not put a moveto or translate at the beginning of the definition of squares. The code that you write to test the squares function will, however, contain a moveto or translate to control where on the page your test squares are drawn.

This function has one operand and no results so after execution the stack will contain one fewer element than it started with.

chessboard.ps -- 35%

This program should go into the file chessboard.ps. Write a PostScript definition for a function named chessboard that takes one operand, n, and draws a chessboard of size n. For example 8 chessboard would produce
pretty picture
while 3 chessboard would generate a smaller chessboard:
pretty picture

Again, the top left point of the top left square should be drawn at the currentpoint. Don't execute a moveto within the chessboard function in order to position currentpoint before starting to draw. Of course you may use moveto/rmoveto to get to the correct point within the drawing. As with the squares function your testing code will have to use moveto to establish the currentpoint before calling chessboard.

Make sure your code works for all odd and even values of n greater than zero.

Hint: re-use your squares function.
                                                                                                                                                                                                                                                                                                                                             

  (c) 2003 Curtis Dyreson, (c) 2004, 2005 Carl H. Hauser           E-mail questions or comments to Prof. Carl Hauser