CS 3723/3721
Programming Languages

Variables & Procedures

 


Read Bluebook, pages 27-29 carefully. The syntax of Postscript and its implementation are remarkably simple. Concepts to be acquainted with:

  • The system dictionary and the user dictionary.
  • How the Postscript interpreter handles a name (an identifier): search first the system and then the user dictionary. An error if not found.
  • How the interpreter handles an identifier with a slash in front. (Pushes the name on the stack without looking it up)
  • How Postscript handles def.
  • What Postscript variables, assignments, and procedures look like.
  • How an assignment or a procedure is inserted into the user dictionary.
  • What Parameters of a procedure are like. (Stored on the system stack, and retrieved from there inside the procedure.)


Use pstack to examine the stack for debugging. (See Bluebook, page 13.) Here is a table contrasting ordinary assignments and procedures with Postscript ones. In this table, the Postscript operator pstack or stack is shown. When you convert from .ps to .pdf, any occurrence of pstack causes the values of the Postscript stack to be printed at the terminal, with the top value first. Unlike other Postscript operators, pstack doesn't delete the stack contents as it retrieves values from the stack. At any point in your code, you can insert pstack to see what the stack looks like at that point.

The definition of a user procedure has to come before its use, as in the C language.

Construct "Ordinary"
Language
Postscript Stack
Assignmentx = 14;
print x;
/x 14 def
x pstack
14
Reassignmentx = 37;
print x;
/x 37 def
x pstack
37
Increment by 1x = x + 1;
print x;
/x x 1 add def
x pstack
38
Function
with params
double inch(double x) {
  return 72*x;
}

a = 2;
b = inch(a);
print a, b;
/inch { # Stack: n
  72 mul
} def

/a 2 def
/b a inch def
a b pstack
144
2
Function
with params
double sqr(double x) {
  return x*x;
}

a = 14;
b = sqr(a);
print a, b;
/sqr { # Stack: n
  dup mul
} def

/a 14 def
/b a sqr def
a b pstack
196
14

( Revision date: 2015-01-03. Please use ISO 8601, the International Standard Date and Time Notation.)