Homework 9
Huey, A Little Language for RGB Values

Due: Monday, April 15, at 11:59 PM

Introduction

This assignment gets you started on your work for the rest of the semester: an interpreter and other tools for processing a small language. This language may seem a bit more useful to you than the little language we have worked with up to now, because it will has real numbers and real operations, and it solves real problems.

Before going any farther, read the Huey language specification. It defines the language and talks about some of the Racket you can use to implement your homework.

Code and Files

For this assignment, you will implement the initial components of a Huey interpreter, including syntax procedures that define its abstract syntax, a pre-processor to eliminate syntactic abstractions, and an evaluator. Organize your code as follows.

Make sure that each file provides all of its public functions so that other files can require them. For example, interpreter.rkt will require syntax-procs.rkt and provide only the preprocessor and evaluator. Any file may want to require utilities.rkt in order to use one or more of its functions.

We will extend the Huey language and intepreter on future homework assignments. Please comment your source code so that you and I can easily find the various pieces.

Problems

  1. Define a complete set of syntax procedures for Huey.

    For each type of expression in the BNF description, define:
    • a type predicate,
    • a constructor, and
    • one or more access procedures that retrieve the relevant parts of the expression.
    The names you choose for your constructor and type predicate should be the same except for the type predicate's trailing "?". The names your choose for your accessors may be short, but they should be descriptive.

    Finally, create a general type predicate named (color? arg) that returns true if and only if arg is a legal expression in the language: an RGB value, a unary expression, a 2-color binary expression, or a 1-colorbinary expression.

    The type predicate and constructor for RGB values should ensure that the components of the resulting value all lie in the range 0 ≤ n ≤ 255.

  2. Define a function named (preprocess sugared-exp).

    sugared-exp is a Huey expression from the full syntax, which includes the operators darker and mix.

    preprocess returns an expression in the core Huey language, with the syntactic abstractions translated into equivalent expressions, and all other expressions preserved. For example:
    > (preprocess '(invert (rgb 150 99 42)))
    '(invert (rgb 150 99 42))
    
    > (preprocess '(darker (rgb 150 99 42)))
    '((rgb 150 99 42) * 0.5)
    
    > (preprocess '((rgb 4 4 4)
                    +
                    ((rgb 150 99 42) mix (rgb 50 108 21))))
    '((rgb 4 4 4)
      +
      (((rgb 150 99 42) * 0.5) + ((rgb 50 108 21) * 0.5)))
    

  3. Define a function named (eval-exp exp).

    exp is a Huey expression from the full syntax of the language. If exp is not a legal Huey expression, eval-exp signals an error. For example:
    > (eval-exp '((rgb 0 0 0) + 3))
    huey: illegal expression -- ((rgb 0 0 0) + 3)
    
    eval-exp returns the value of exp as its result, according to the language's semantics. Note that exp can contain sugar, so eval-exp must pre-process its argument before evaluating it! You may want to make eval-exp an interface procedure that ensures its argument is a legal expression, preprocesses it, and calls a helper to evaluate the resulting core expression.

    What Racket will display as a result of eval-exp depends on how you represent RGB values internally. If you use lists of size three, for example, it might display values such as these:
    > (eval-exp '(invert (rgb 150 99 42)))
    '(105 156 213)
    
    > (eval-exp '((rgb 150 99 42) + (rgb 50 18 241)))
    '(200 117 255)
    
    > (eval-exp '((rgb 255 0 255) mix ((rgb 0 255 0) + (rgb 4 4 4))))
    '(129 127 129)
    
    If you implement RGB values differently, Racket will display different outputs, but the components of the colors will be the same.

    The language description provides some sample expressions for testing. You can also find other simple test expressions in the description of Huey's semantics. Be sure to test other expressions, including some that are more complex.

Deliverables

By the due time and date, use the course submission system to submit the following files electronically:

Be sure that your submission follows the submission requirements. As always, use the specified names for your files. This enables the autograder to find and run your code.