- Concepts of Programming Languages

L-Values

Instructor:

Learning Objectives

When do we read and when write to a memory location?

  • Distinguish l-values from r-values

Variables

  • Given declaration:
    1. var x:Int = 0
  • What does x mean below?
    1. x = x + 1

L-Values

  • Expression for which l-mode evaluation succeeds
  • Effectively, has an address
  • In C, l-value has address:
    1. int x = 5;
    2. int y = 6;
    3. int *p = &x;
    4. int *q = &y;
    5. int **r = &p;
    6. r = &q;
    7. q = p;
    8. **r = 7;
  • In Scala, we can assign to l-values
    1. val x = 5
    2. val l = List(1, 2)
    3. val (a, b) = (1, 2)
    4. val y :: z :: Nil = l

Not L-Values

  • L-mode evaluation sometimes disallowed
  • In C, cannot take address of temporary expressions
    1. int x = 5;
    2. int y = 6;
    3. int *p = &(x + y); /* not allowed */
    4. (x + y) = 7; /* not allowed */
  • (x + y) not an l-value
    • although it might be stored temporarily

Further L-Values

  • L-values may require r-mode evaluation
  • Array access in C, Java, etc.
    1. arr[n + 2] = 7;
  • Field access in Java, Scala, etc.
    1. obj.f1 = 7

Summary

L-values

  • Write to a memory location
  • C: take address &x
  • Scala, Java: assignable
    1. val x = 5
    2. val y :: tail = List(1, 2, 3)
  • May need r-value arr[1+2]

R-values

  • Read value at a memory location
  • C, Scala, Java: expressions x, 1, x+y, ...