CS 5510 Homework 5

Due: Friday, October 1st, 2010 11:59pm

Start with the RCFAE interpreter.

Part 1 – Records

Extend your interpreter to support the construction of records with named fields, and to support field selection from a record:

  <RCFAE> = ...
          | {record {<id> <RCFAE>}*}
          | {get <RCFAE> <id>}

Extend parse to support record and get expressions. Your revised parse should check that each <id> is distinct in a record expression.

Adding records means that the language now has three kinds of values: numbers, function, and records. At run-time, an error may occur because a record is misused as a number or function, a number or function is supplied to get, or a record supplied to get does not have the named field. Your error message for the last case should include the words "no such field", otherwise you can make up your own error messages (or just let primitive error checking handle problems, such as trying to add a record to a number).

Since records are now values, the result of interp can be a record instead of a number. For homework purposes, we don't want to nail down the representation of a record result, because there are many choices. The examples below therefore use interp-expr, which you should define as a wrapper on interp that takes just an RCFAE and produces just a number if interp produces a number, the symbol 'function if interp produces a closure, or the symbol 'record if interp produces a record value.

Examples:

  (test (interp-expr (parse '{record {a 10} {b {+ 1 2}}}))
        'record)
  (test (interp-expr (parse '{get {record {a 10} {b {+ 1 2}}} b}))
        3)
  (test/exn (interp-expr (parse '{get {record {a 10}} b})))
            "no such field")
  (test (interp-expr (parse '{get {record {r {record {z 0}}}} r}))
        'record)
  (test (interp-expr (parse '{get {get {record {r {record {z 0}}}} r} z}))
        0)

Last update: Thursday, September 23rd, 2010
mflatt@cs.utah.edu