Compilers (Fall 2003)
please Reference Manual




Contents

please is an strongly-typed, garbage-collected imperative programming language. The name of the language is an acronym for "Programming Language Expecting Abstact Set Expressions". (It is also known, perhaps somewhat more affectionately, as "Please Let Expressions About Sets Execute".)


Programs

please programs consists of zero or more declarations, followed by precisely one statement. (That may sounds trivial, but declarations, as made apparent below, can consist of arbitrarily nested functions and procedures, and the solo statement can itself be a block which can consist of more declarations and statements.)

please programs do not take any arguments.


Declarations

A declaration can be in one of four forms:

  1. variable declarations are of the form as in these examples:
      int $count;
      pair<string,bool> $p;
      set{set{int}} $setOfSets;
      array[string] $alias;
    
    Note the last example, an array declaration without an allocation refers to an array that will be used as pointer.

  2. array allocations are of the form where the type is an array type, as in these examples:
      array[int] $a[13];
      array[string] $beta[$i];
      array[set{bool}] $c3po[2*(sizeof $s)];
    
  3. function declarations are of the form as in this example:
      fun add(int $i, int $j) : $int {
        int $sum;
        $sum := $i + $j;
        return $sum;
      }
    
  4. procedure declarations are of the form as in this example:
      proc reset(array[int] $a) {
        int $i;
        $i := 0;
        while $i < sizeof $a do {
          $a[$i] := 0;
          $i := $i + 1;
        }
      }
    
    Observe, procedures are just like functions except they have no return type.

Formal arguments

The formal-argument list of a function or procedure declaration is a sequence of zero or more comma-separated formal arguments. Each argument indicates the type and name of an input parameter to the function. A formal argument is of the form:


Statements

A statement may take one of several forms:

  1. assignments are of the form: as in these examples:
      $x := "hello";
      $a[$i] := $y * 3;
      $matrix[$i][$j] := 7;
    
  2. conditional statements are of the form: as in:
      if $a < $b then {
        $max := $b;
      } else {
        $max := $a;
      }
    
  3. while statements are of the form: as in:
      while $n > 0 {
        $fact := $n * $fact;
        $n := $n - 1;
      }
    
  4. foreach statements are of the form: as in:
      foreach $x in $s do {
        $a[$i] := $x;
        $i := $i + 1;
      }
    
  5. return statements are of the form:

  6. break statements are of the form:

  7. procedure calls are of the form: where actual-arguments represents zero or more, comma separated expressions. For example:
      reset($a);
      copyArray($arr0, $arr1);
    
  8. block statements are of the form: where declaration-list represents zero or more declarations and statement-list represents zero or more statements. For example:
      { }   // empty blocks are okay!
    
      {
        int $temp;
        $temp := $x;
        $x := $y;
        $y := $temp;
      }  
    



Expressions

An expression may take one of many forms:
  1. boolean literal: true and false, each of which is of type bool.

  2. integer literal: of type int.

  3. string literal: of type string.

  4. variable: an identifier either defined in a variable declaration or as a formal argument to a function.

  5. unary-operator expressions, of the form:   unop   expr
    where unop is one of: ~, sizeof, grab, first, second

  6. binary-operator expressions, of the form:   expr   unop   expr
    where binop is one of: +, -, *, /, %, =, <, >, <=, >=, <>, in, and, or

  7. parenthesized expressions, of the form:   ( expr )

  8. array expressions, of the form:   expr [ expr ]

  9. function calls are of the form:   function-identifier ( actual-arguments )   ;
    where actual-arguments represents zero or more, comma separated expressions.

  10. pair expressions are of the form:   < expr , expr >

  11. set expressions are of the form:   { expr-list }
    where expr-list represents zero or more, comma separated expressions.




Types

A type expression can be in one of four forms:
  1. base types: bool, int, or string

  2. pairs are of the form: as in these examples:
      pair<string,bool>
      pair<set{int},array[set{string}]>
    
  3. arrays are of the form: as in these examples:
      array[int]
      array[string]
      array[set{bool}]
    
  4. set are of the form: and represent a homogenous (in terms of type) unordered, collection of values (with no duplicates). Examples of set types include:
      set{string} $names;
      set{set{int}} $setOfSets;
      set{pair<int,int>} $points;
    



Precedence

The following expression-forming operators are not associative. Attempting to associate them should result in a syntax error (not a type error):

  =   >   <   >=   <=   first   second   sizeof   grab   in

Two unary operators, ~ and not associate to the right.

All other operators associate to the left.

The order of operators is, from highest to lowest, as follows:

  []
  ~
  *   /   %
  +   -
  sizeof
  first   second
  grab
  =   >   <   >=   <=   in
  not
  and
  or



Identifiers

Identifiers are case-sensitive strings of one or more characters where the first character is a letter or an underscore, and each subsequent character is a letter, underscore, or digit.