Programming Languages: Chapter 12: Statements and Side-effects



Make ChAmElEoN statement-oriented

  • so far ChAmElEoN has been expression-oriented meaning it works by returning values
  • we instrument our ChAmElEoN interpreter define a statement-oriented language meaning a program is a statement and works by side effect (printing values)
  • all else is same as previous version of ChAmElEoN
  • retain
      denoted value = ref(expressed value)
      expressed value = number + procval
  • see new grammar and abstract-syntax in Fig. 3.23 on p. 121
  • see examples of blocks on p. 120
  • see execute-program and execute-statement in Fig. 3.24 on p. 123


Notables

  • uses Scheme for-each to implement compound statements
  • uses named let to implement while statement
  • (let name ((var val) ...)
       exp_1 exp_2 ...)
    
    is semantically equivalent to
    ((letrec ((name (lambda (var ...) exp_1 exp_2 ...)))
       name) val ...)
    
    for example,
    (let ((n 0))
      (let loop ()
        (if (< n 10)
            (begin
              (display n)
              (newline)
              (set! n (+ n 1))
              (loop)))))
    
    (newline)
    
    (let ((n 0))
      (letrec ((loop (lambda ()
                       (if (< n 10)
                           (begin
                             (display n)
                             (newline)
                             (set! n (+ n 1))
                             (loop))))))
        (loop)))
    
    both outer let's have the same effect; this code prints the numbers from 0 to 9 (one per line) twice with a newline in between

    see [TSPL3] Chapter 5, § 5.4, p. 100 for more information

    therefore,
    (while-statement (exp statement)
       ;; called the "named let"
       (let loop ()
          ;; one-armed if helps us avoid returning an arbitrary value
          (if (true-value? (eval-expression exp env))
             (begin
                (execute-statement statement env)
                   (loop)))))
    
    is equivalent to
    (while-statement (exp statement)
       ((letrec ((loop (lambda ()
          (if (true-value? (eval-expression exp env))
             (begin
                (execute-statement statement env)
                   (loop))))))
          loop)))
    
  • implementation of while statement also uses the one-armed if to avoid returning an arbitrary value


References

    [EOPL2] D.P. Friedman, M. Wand, and C.T. Haynes. Essentials of Programming Languages. MIT Press, Cambridge, MA, Second edition, 2001.
    [SICP] H. Abelson and G.J. Sussman. Structure and Interpretation of Computer Programs. MIT Press, Cambridge, MA, Second edition, 1996.
    [TSPL3] R.K. Dybvig. The Scheme Programming Language. MIT Press, Cambridge, MA, Third edition, 2003.

Return Home