lecture 10: expression evaluation

24
The University of North Carolina at Chapel Hill Lecture 10: Expression Evaluation COMP 524 Programming Language Concepts Stephen Olivier February 17, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Upload: others

Post on 15-May-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Lecture 10: Expression Evaluation

COMP 524 Programming Language Concepts Stephen OlivierFebruary 17, 2009

Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Page 2: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Goal of Talk

•The goal of this talk is to talk about expressions and the flow of programs

2

Page 3: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Control Flow

•Control flow is the order in which a program executes.

•For imperative languages (e.g., Java), this is fundamental.

•For other programing paradigms (e.g., functional), the compilers/interpreters take care of ordering.

3

Page 4: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expression Evaluation

•An expressions consist of a simple object (e.g., a variable), an operator, or a function applied to a collection of objects and/or operators.

•Expression evaluation is a crucial component of functional languages.

4

Page 5: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expression Evaluation

•An expressions consist of a simple object (e.g., a variable), an operator, or a function applied to a collection of objects and/or operators.

•Expression evaluation is a crucial component of functional languages.

5

Functional languages are very “math-like” and in math a primary concept is

evaluating expressions.

Page 6: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Operators

•Operators are used in

• Prefix notation: operators come first

• (* (+ 1 3) 2 )

• Infix notation: operators in middle

• (1+3)*2

• Postfix notation: operators last

• a++

6

Page 7: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Operators-Precedence

•Precedence rules specify the order in which operators of different precedence levels are evaluated.

• e.g. Multiplication before addition.

•Precedence in boolean expressions very important

• The phrase “if A<B and C<D” can be read as:

•if (A<B) and (C<D)

•if (A< (B and C)) <D

7

Page 8: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Operators--Associativity

•Associativity rules specify the order in which operators of the same precedence level are evaluated.

• Usually they are evaluated “left-to-right”

•In Fortran, ** associates from right-to-left

• x ** y = x^y

• Thus 2**3**4 is read as 2^(3^4) rather than (2^3)^4.

•Also assignment in C

• a = b = c

8

Page 9: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Assignment

•The basic operation language is assignment.

•An assignment places a value into a specific memory location.

9

a = 2;

a2036

Xa

2036

2

Before After

Page 10: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Assignment

•The basic operation language is assignment.

•An assignment places a value into a specific memory location.

10

a = 2;

a2036

Xa

2036

2

Before After

As a result, assignments have longevity and can exist beyond their original

context.

Page 11: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Context

•To see the difference between context consider the two following statements.

11

int sum(int n){int val=0;for(int i=0,i<=n;i++){val+=i;

}return val;

}

int sum(int n){if (n<=0) thenreturn 0

elsereturn n+sum(n-1)

}

Imperative Functional

Page 12: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Context

•To see the difference between context consider the two following statements.

12

int sum(int n){int val=0;for(int i=0,i<=n;i++){val+=i;

}return val;

}

int sum(int n){if (n<=0) thenreturn 0

elsereturn n+sum(n-1)

}

In the imperative code the value of val changes within

the context of sum

Imperative Functional

Page 13: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Context

•To see the difference between context consider the two following statements.

13

int sum(int n){int val=0;for(int i=0,i<=n;i++){val+=i;

}return val;

}

int sum(int n){if (n<=0) thenreturn 0

elsereturn n+sum(n-1)

}

Imperative Functional

In the functional code the value of n changes but only between contexts of sum

Page 14: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Variables

•Two ways to model variables:

• Value model

• Reference model

14

Page 15: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Value Model

•Under the value model variables on the left-hand side (called l-values) of equations denote references, and variables on the right-hand side (called r-values) denote values.

15

b = 2;a = b; a

2036

X

mem(1024)= 2;mem(2036)= 2;

b1024

2

a2036

2b

1024

2

Page 16: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Value Model

•Under the value model variables on the left-hand side (called l-values) of equations denote references, and variables on the right-hand side (called r-values) denote values.

16

b = 2;a = b; a

2036

X

mem(1024)= 2;mem(2036)= 2;

b1024

2

a2036

2b

1024

2

Pascal and C use this model

Page 17: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Reference Model

•Under the reference model variables on both the left- and right-hand side are references.

17

b = 2;a = b; a

2036

X

mem(1024)= loc(2);mem(2036)= mem(1024);

b1024

48

a2036

48b

1024

48

2248

Page 18: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Reference Model

•Under the reference model variables on both the left- and right-hand side are references.

18

b = 2;a = b; a

2036

X

mem(1024)= loc(2);mem(2036)= mem(1024);

b1024

48

a2036

48b

1024

48

2248

Lisp, Clu use this model.

Page 19: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Initialization

•Variable initialization can be implicit or explicit.

• Implicit: variables are initialized as they are used (e.g., Perl).

• Explicit: variables are initialized by the programmer (e.g., C).

•Java, C# require definite assignment

• Variables must be assigned a value before they are used in expressions

19

$a += 3;

int a = 0;a += 3;

Page 20: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Orthogonality

•Orthogonality means that features can be used in any combination and the meaning is consistent regardless of the surrounding features

• Good idea in principle, but requires careful thought

• e.g. assignment as an expression

• unfortunate when combined with poor syntactic choices, as in C:

20

if(a=b){ } if(a==b){ }

Page 21: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Complication

•Execution ordering within expressions is complicated by side effects (and code improvements)

• e.g., in C

•If inc(b) is evaluated before (3*b), the final value of c is 12. If the (3*b) is evaluated first, then the value is c is 6.

21

b=1;int inc(int a) {b+=1;return a+1;

}c = (3*b) * inc(b);

Page 22: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Short-Circuit

•Expressions may be executed using short-circuit evaluation

22

p = my_list;while (p && p->key !=val)p=p->next

Page 23: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Short-Circuit

•Expressions may be executed using short-circuit evaluation

23

p = my_list;while (p && p->key !=val)p=p->next

if p = nul, then p->key is never checked. Thus, it is “short-circuited”

Page 24: Lecture 10: Expression Evaluation

The University of North Carolina at Chapel Hill

Expressions: Short-Circuit

•Expressions may be executed using short-circuit evaluation

24

p = my_list;while (p && p->key !=val)p=p->next

p := my_list;while (p<>nil) and(p^.key <> val) dop:=p^.next

Since Pascal does not have short circuiting, this will check both. Thus, if p=nil, then p^.key will

return an error.