------------------------------------------------------------------------------
MC logo
Semantics
[^] Chapter Outlines
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Copy v. Reference Assignment] [Sort Using Goto] [Return Value and Exceptional I/O]
<<Types and Type Systems Functions>>
  1. Expressions.
    1. Precedence and associativity.
    2. Postfix notation used occasionally.
    3. Short-circuit evaluation.
      1. Used by most languages for and and or.
      2. Pascal often used regular.
      3. Ada has both
        1. Regular operators: and, or
        2. Short circuit: and then, or else
      4. Violates eval-arguments-first.
        May feel strange with functional notation.
        Lisp: (or (null? x) (= (car x) 0))
    4. Practical limits on data sizes — not like math
      1. Values can overflow: may make order matter more.
      2. Some scripting languages have unbounded integer types.
    5. Side-effects.
      1. Certain operators, like ++
      2. Function calls that update object or global data.
  2. State
    1. Collection of all variables and their values.
    2. Assigning a variable updates state.
  3. Assignments
    1. Variations
      1. Chained assignment: a = b = c = 0;
      2. Parallel assignment: a, b, c = 1, 2*y, 3+z;
        Usually all rhs values computed before any lhs variables set.
      3. Statement or expression
        1. Often assignment is a statement: Pascal, Ada, Python.
        2. Expression in C family. a = (b = 10) + 1.
          More common while(ch = getchar()) { . . . }
    2. Semantics
      1. Type transfer
        1. Statically-typed languages: r-value converted to target type. Types must conform, or the conversion must be legal.
        2. Dyamically-typed languages: both type and value are assigned.
      2. Copy v. reference: Assignment may copy the l-value or make the target a new reference to the l-value object.
        1. Reference assignment is typical of interpreted languages and object-oriented languages.
        2. Some language may use each on different types.
        3. Java uses reference semantics for objects, and copy semantics for base types like int. C++ uses copy semantics. For Instance.
  4. Control flow
    1. Sequence.
    2. Conditional.
      1. Algol, Pascal, C/C++, Java: Block statement.
        1. BEGIN/END
        2. { }
      2. Ada, others, block is part of if.
    3. Switch or case
      1. Earliest form was a computed goto:
        GO TO (80, 100, 10, 35), IDING - JBAT
      2. C, C++, Java: Cases must be broken. C switch
      3. Pascal, Ada, others: Cases end at the next case. Ada case
    4. Loops
      1. While loop -- most general.
      2. Counted for loop: for a := 1 to 10 do
      3. For-each style loops. Python, Ruby.
      4. Break statement.
    5. Goto.
      1. Primary control structure in early languages.
      2. Algol and later languages provided an alternative.
      3. Eventually, goto considered a bad thing.
  5. I/O
    1. Binary or character.
      1. Binary: Data are written exactly as stored.
      2. Character: Data are written as character strings.
        1. Free format (unformatted)
        2. Formatted: Fixed-width fields
    2. Standard streams.
    3. Sequential or random access.
    4. Syntactic class
      1. Statement (Fortran)
      2. Library call (Most common).
      3. Privileged function call (Pascal).
    5. Error handling
      1. Pascal: aborts on bad conversions.
      2. Reporting through a return value (C).
      3. Current trend is to use exceptions.
        Some languages allow both
  6. Exceptions
    1. An error condition which the operation cannot handle itself.
    2. Opinions differ about what events are exceptions.
    3. Exceptionless languages.
      1. Return an error value (typically -1 or nil).
      2. Extra output parameter describing success.
    4. Exception support.
      1. Program aborts can be thought of as uncatchable exceptions.
      2. Exception types.
        1. A type of its own (Ada).
        2. An object with a certain base class (Java Throwable).
        3. Any type (C++).
      3. Raising an exception transfers directly to a handler.
      4. Options
        1. How are handlers associated with exceptions?
          1. The try block is familiar.
          2. PL/I on statement assigns the block to the exception type.
          3. C signals are similar.
        2. Does the operation resume after the handler completes?
          1. No: Most languages today.
          2. Yes: PL/I used this. C signals likewise.
            1. Must somehow make the operation become legal after restart.
            2. Really tough for bad input.
            3. Handlers often jump away or exit program to prevent restart.
    5. Examples.
      1. I/O returning and throwing
      2. Ada
      3. Python
      4. Ruby
    6. Assert.
      1. A condition which must be true, else throws an exception.
      2. Some older languages have assertions which abort on failure.
<<Types and Type Systems Functions>>