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