Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.

Similar presentations


Presentation on theme: "Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand."— Presentation transcript:

1 Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand evaluation order –side effects –short-circuit evaluation

2 Arithmetic expressions expressions involving arithmetic operators such as +, -, * and / –most are binary only (taking two arguments) –some are unary (taking one argument) most languages write binary arithmetic operators infix (between their operands), as in mathematics: x + y some don’t (e.g. Scheme): (+ x y) Scheme’s operators are of arbitrary arity: (+ 1 2 3 4)  (+ (+ (+ 1 2) 3) 4)  10 (- 1 2 3 4)  (- (- (- 1 2) 3) 4)  -8 (* 1 2 3 4)  (* (* (* 1 2) 3) 4)  24 (/ 1 2 3 4)  (/ (/ (/ 1 2) 3) 4)  1/24

3 Precedence Without parenthesization, in what order are different operators evaluated? E.g. what is the value of 1 + 2 * 3? Is it 9 or 7? Most languages adopt precedence rules of mathematics (e.g. * and / have higher precedence than + and -). Unary operators typically have higher precedence than binary ones (e.g. unary – has higher precedence than binary -). In C-like languages, postfix ++ and -- have higher precedence than unary -. Suppose x has value 2, then -x++ has value -2 (but leaves x with value 3).

4 Associativity Determines whether unparenthesized expressions involving operators of the same precedence group to the left or to the right: 5 – 3 – 4  (5 – 3) – 4  –2 (left associativity) 5 – 3 – 4  5 – (3 – 4)  6 (right associativity) In most languages +,-,*,/ are left associative. In Scheme the question of associativity does not come up (since all expressions must be fully parenthesized)

5 Associative operators Some operators are, mathematically speaking, associative. For example, + is mathematically associative, meaning that x+y = y+x. For example, - is not: x-y != y-x (unless x and y are the same). Compilers can take advantage of this to reorder expressions for increased execution speed, but… Operators like + are not generally associative in programming languages due to the limited precision with which numbers are represented. With floating point operations + and * are not associative: 10000000.0 + 0.000001 + 0.000001 + … + 0.000001 != 0.000001 + 0.000001 + … + 0.000001 + 10000000.0

6 Conditional expressions A conditional expression has a value. In C-like languages the if-statement is not an expression, and has no value: if (count==0) { average = 0; } else { average = sum/count; }

7 Conditional expressions in C-like languages In C-like languages we cannot write: average = if (count==0) 0; else sum/count; In C-like languages we must write: average = (count==0) ? 0 : sum/count; In functional languages conditionals are expressions: val average = if (count=0)then 0 else sum/count; Orthogonality in language design favors NOT having different syntax for conditional statements and conditional expressions.

8 Implications of conditional expressions A conditional expression requires that all branches of the conditional: –have a value –have the same type An if-then without an else is semantically incoherent as an expression.

9 Operand evaluation order In an expression like +, is or evaluated first? Without side-effects or short-circuit evaluation, order is irrelevant. Aside: if an expression has the same value no matter what its context of evaluation, then it is termed referentially transparent.

10 Side-effects & operand evaluation If expressions have side effects (e.g. mutate variables) then order of evaluation becomes very important. Operators like ++ are side-effecting: ++x is equivalent to x = x + 1.

11 Short-circuit evaluation and operand evaluation If expression evaluation is short-circuited then order of evaluation also becomes very important. In C-like languages && is typically short- circuiting.

12 Operator overloading Positives: can make code more clear –in C++ ‘<<’ can be overloaded for different types to handle stream output –in C++ ‘+’ can be overloaded to provide addition of novel types Negatives: can make code less clear –in C ‘&’ is overloaded with unrelated meanings –nothing stops a user from overloading ‘+’ to do something other than addition

13 unary/binary overloading - is overloaded with unary and binary meanings in most languages In ML - is binary subtraction only, and ~ is unary negation. –Is benefit worth the trouble?

14 Type coercions and type checking Type coercions weaken type checking.


Download ppt "Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand."

Similar presentations


Ads by Google